Mojo::Transaction

作者: JSON_NULL | 来源:发表于2017-09-29 16:40 被阅读20次

    简介

    Mojo::Transaction 是一个用于事务的抽象基类,它的子类有:Mojo::Transaction::HTTP和Mojo::Transaction::WebSocket。

    package Mojo::Transaction::MyTransaction;
    use Mojo::Base 'Mojo::Transaction';
    
    sub client_read  {...}
    sub client_write {...}
    sub server_read  {...}
    sub server_write {...}
    

    事件

    Mojo::Transaction从Mojo::EventEmitter中继承了所有事件,并实现了以下事件。

    connection

    $tx->on(connection => sub {
      my ($tx, $connection) = @_;
      ...
    });
    

    当一个连接被分配到一个事务中时触发。

    finish

    $tx->on(finish => sub {
      my $tx = shift;
      ...
    });
    

    当一个事务完成时触发。

    属性

    Mojo::Transaction实现了以下属性。

    kept_alive

    my $bool = $tx->kept_alive;
    $tx      = $tx->kept_alive($bool);
    

    获取或设置连接的keep_alive属性。

    local_address

    my $address = $tx->local_address;
    $tx         = $tx->local_address('127.0.0.1');
    

    获取或设置本地的接口的IP地址。

    local_port

    my $port = $tx->local_port;
    $tx      = $tx->local_port(8080);
    

    获取或设置本地接口的端口号。

    original_remote_address

    my $address = $tx->original_remote_address;
    $tx         = $tx->original_remote_address('127.0.0.1');
    

    获取或设置远程接口的IP地址

    remote_port

    my $port = $tx->remote_port;
    $tx      = $tx->remote_port(8081);
    

    或取或设置远程接口的端口号。

    req

    my $req = $tx->req;
    $tx     = $tx->req(Mojo::Message::Request->new);
    

    http请求对象,默认为Mojo::Message::Request对象。

    # Access request information
    my $method = $tx->req->method;
    my $url    = $tx->req->url->to_abs;
    my $info   = $tx->req->url->to_abs->userinfo;
    my $host   = $tx->req->url->to_abs->host;
    my $agent  = $tx->req->headers->user_agent;
    my $custom = $tx->req->headers->header('Custom-Header');
    my $bytes  = $tx->req->body;
    my $str    = $tx->req->text;
    my $hash   = $tx->req->params->to_hash;
    my $all    = $tx->req->uploads;
    my $value  = $tx->req->json;
    my $foo    = $tx->req->json('/23/foo');
    my $dom    = $tx->req->dom;
    my $bar    = $tx->req->dom('div.bar')->first->text;
    

    res

    my $res = $tx->res;
    $tx     = $tx->res(Mojo::Message::Response->new);
    

    HTTP响应对象,默认为Mojo::Message::Response对象。

    # Access response information
    my $code    = $tx->res->code;
    my $message = $tx->res->message;
    my $server  = $tx->res->headers->server;
    my $custom  = $tx->res->headers->header('Custom-Header');
    my $bytes   = $tx->res->body;
    my $str     = $tx->res->text;
    my $value   = $tx->res->json;
    my $foo     = $tx->res->json('/23/foo');
    my $dom     = $tx->res->dom;
    my $bar     = $tx->res->dom('div.bar')->first->text;
    

    方法

    Mojo::Transaction从Mojo::EventEmitter中继承了所有的方法,并实现了以下方法。

    client_read

    $tx->client_read($bytes);
    

    作为客户端读取数据,用于实现诸如Mojo::UserAgent之类的用户代理。需要在子类中被重载。

    client_write

    my $bytes = $tx->client_write;
    

    作为客户端写数据,用于实现诸如Mojo::UserAgent之类的用户代理。需要在子类中被重载。

    closed

    $tx = $tx->closed;
    

    除与"completed"的动作相同外,还表示所有事务的数据已经发送完了。

    completed

    $tx = $tx->completed;
    

    这是一个低层级的方法用于结束一个事务。

    connection

    my $id = $tx->connection;
    $tx    = $tx->connection($id);
    

    连接标识符。

    error

    my $err = $tx->error;
    

    获取 “请求” 和 “响应” 的 “错误信息”,如果没有“出错”就返回undef。通常与success一起使用。

     # Longer version
     my $err = $tx->req->error || $tx->res->error;
    
     # Check for 4xx/5xx response and connection errors
     if (my $err = $tx->error) {
       die "$err->{code} response: $err->{message}" if $err->{code};
       die "Connection error: $err->{message}";
     }
    

    is_finished

    my $bool = $tx->is_finished;
    

    检查事务是否已经完成。

    is_websocket

    my $bool = $tx->is_websocket;
    

    只有当前对象是一个Mojo::Transaction::WebSocket对象进才返回true,否则返回false。

    remote_address

    my $address = $tx->remote_address;
    $tx         = $tx->remote_address('127.0.0.1');
    

    original_remote_address方法相同,如果“请求”是通过反向代理转发的,则返回X-Forwarded-For头中的最后一个值。

    result

    my $res = $tx->result;
    

    从“响应”中返回Mojo::Message::Response对象,如果发生连接错误,则程序会退出。

    # Fine grained response handling (dies on connection errors)
    my $res = $tx->result;
    if    ($res->is_success)  { say $res->body }
    elsif ($res->is_error)    { say $res->message }
    elsif ($res->code == 301) { say $res->headers->location }
    else                      { say 'Whatever...' }
    

    server_read

    $tx->server_read($bytes);
    

    作为服务器读取数据,用于实现Web服务器,如Mojo::Server::Daemon。需要在一个子类中被重载。

    server_write

    my $bytes = $tx->server_write;
    

    作为服务器写数据,用于实现Web服务器,如Mojo::Server::Daemon。需要在一个子类中被重载。

    success

    my $res = $tx->success;
    

    如果事务成功则从“响应”中返回Mojo::Message::Response对象,否则返回undef。连接错误或解析错误作为错误信息代码(400 或 500)在“error”中返回。

    # Manual exception handling
    if (my $res = $tx->success) { say $res->body }
    else {
      my $err = $tx->error;
      die "$err->{code} response: $err->{message}" if $err->{code};
      die "Connection error: $err->{message}";
    }
    

    相关文章

      网友评论

        本文标题:Mojo::Transaction

        本文链接:https://www.haomeiwen.com/subject/ehmiextx.html