.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Mojo::Transaction::WebSocket 3pm" .TH Mojo::Transaction::WebSocket 3pm "2013-04-25" "perl v5.14.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Mojo::Transaction::WebSocket \- WebSocket transaction .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::Transaction::WebSocket; \& \& # Send and receive WebSocket messages \& my $ws = Mojo::Transaction::WebSocket\->new; \& $ws\->send(\*(AqHello World!\*(Aq); \& $ws\->on(message => sub { \& my ($ws, $msg) = @_; \& say "Message: $msg"; \& }); \& $ws\->on(finish => sub { \& my ($ws, $code, $reason) = @_; \& say "WebSocket closed with status $code."; \& }); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Mojo::Transaction::WebSocket is a container for WebSocket transactions as described in \s-1RFC\s0 6455. Note that 64bit frames require a Perl with 64bit integer support, or they are limited to 32bit. .SH "EVENTS" .IX Header "EVENTS" Mojo::Transaction::WebSocket inherits all events from Mojo::Transaction and can emit the following new ones. .SS "binary" .IX Subsection "binary" .Vb 4 \& $ws\->on(binary => sub { \& my ($ws, $bytes) = @_; \& ... \& }); .Ve .PP Emitted when a complete WebSocket binary message has been received. .PP .Vb 4 \& $ws\->on(binary => sub { \& my ($ws, $bytes) = @_; \& say "Binary: $bytes"; \& }); .Ve .SS "drain" .IX Subsection "drain" .Vb 4 \& $ws\->on(drain => sub { \& my $ws = shift; \& ... \& }); .Ve .PP Emitted once all data has been sent. .PP .Vb 4 \& $ws\->on(drain => sub { \& my $ws = shift; \& $ws\->send(time); \& }); .Ve .SS "finish" .IX Subsection "finish" .Vb 4 \& $ws\->on(finish => sub { \& my ($ws, $code, $reason) = @_; \& ... \& }); .Ve .PP Emitted when transaction is finished. .SS "frame" .IX Subsection "frame" .Vb 4 \& $ws\->on(frame => sub { \& my ($ws, $frame) = @_; \& ... \& }); .Ve .PP Emitted when a WebSocket frame has been received. .PP .Vb 10 \& $ws\->unsubscribe(\*(Aqframe\*(Aq); \& $ws\->on(frame => sub { \& my ($ws, $frame) = @_; \& say "FIN: $frame\->[0]"; \& say "RSV1: $frame\->[1]"; \& say "RSV2: $frame\->[2]"; \& say "RSV3: $frame\->[3]"; \& say "Opcode: $frame\->[4]"; \& say "Payload: $frame\->[5]"; \& }); .Ve .SS "message" .IX Subsection "message" .Vb 4 \& $ws\->on(message => sub { \& my ($ws, $msg) = @_; \& ... \& }); .Ve .PP Emitted when a complete WebSocket message has been received, text messages will be automatically decoded. .PP .Vb 4 \& $ws\->on(message => sub { \& my ($ws, $msg) = @_; \& say "Message: $msg"; \& }); .Ve .SS "text" .IX Subsection "text" .Vb 4 \& $ws\->on(text => sub { \& my ($ws, $bytes) = @_; \& ... \& }); .Ve .PP Emitted when a complete WebSocket text message has been received. .PP .Vb 4 \& $ws\->on(text => sub { \& my ($ws, $bytes) = @_; \& say "Text: $bytes"; \& }); .Ve .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" Mojo::Transaction::WebSocket inherits all attributes from Mojo::Transaction and implements the following new ones. .SS "handshake" .IX Subsection "handshake" .Vb 2 \& my $handshake = $ws\->handshake; \& $ws = $ws\->handshake(Mojo::Transaction::HTTP\->new); .Ve .PP The original handshake transaction, defaults to a Mojo::Transaction::HTTP object. .SS "masked" .IX Subsection "masked" .Vb 2 \& my $masked = $ws\->masked; \& $ws = $ws\->masked(1); .Ve .PP Mask outgoing frames with \s-1XOR\s0 cipher and a random 32bit key. .SS "max_websocket_size" .IX Subsection "max_websocket_size" .Vb 2 \& my $size = $ws\->max_websocket_size; \& $ws = $ws\->max_websocket_size(1024); .Ve .PP Maximum WebSocket message size in bytes, defaults to the value of the \&\s-1MOJO_MAX_WEBSOCKET_SIZE\s0 environment variable or \f(CW262144\fR. .SH "METHODS" .IX Header "METHODS" Mojo::Transaction::WebSocket inherits all methods from Mojo::Transaction and implements the following new ones. .SS "new" .IX Subsection "new" .Vb 1 \& my $ws = Mojo::Transaction::WebSocket\->new; .Ve .PP Construct a new Mojo::Transaction::WebSocket object and subscribe to \&\f(CW\*(C`frame\*(C'\fR event with default message parser, which also handles \f(CW\*(C`PING\*(C'\fR and \&\f(CW\*(C`CLOSE\*(C'\fR frames automatically. .SS "build_frame" .IX Subsection "build_frame" .Vb 1 \& my $bytes = $ws\->build_frame($fin, $rsv1, $rsv2, $rsv3, $op, $payload); .Ve .PP Build WebSocket frame. .PP .Vb 2 \& # Binary frame with FIN bit and payload \& say $ws\->build_frame(1, 0, 0, 0, 2, \*(AqHello World!\*(Aq); \& \& # Text frame with payload but without FIN bit \& say $ws\->build_frame(0, 0, 0, 0, 1, \*(AqHello \*(Aq); \& \& # Continuation frame with FIN bit and payload \& say $ws\->build_frame(1, 0, 0, 0, 0, \*(AqWorld!\*(Aq); \& \& # Close frame with FIN bit and without payload \& say $ws\->build_frame(1, 0, 0, 0, 8, \*(Aq\*(Aq); \& \& # Ping frame with FIN bit and payload \& say $ws\->build_frame(1, 0, 0, 0, 9, \*(AqTest 123\*(Aq); \& \& # Pong frame with FIN bit and payload \& say $ws\->build_frame(1, 0, 0, 0, 10, \*(AqTest 123\*(Aq); .Ve .SS "client_challenge" .IX Subsection "client_challenge" .Vb 1 \& my $success = $ws\->client_challenge; .Ve .PP Check WebSocket handshake challenge client-side, used to implement user agents. .SS "client_handshake" .IX Subsection "client_handshake" .Vb 1 \& $ws\->client_handshake; .Ve .PP Perform WebSocket handshake client-side, used to implement user agents. .SS "client_read" .IX Subsection "client_read" .Vb 1 \& $ws\->client_read($data); .Ve .PP Read data client-side, used to implement user agents. .SS "client_write" .IX Subsection "client_write" .Vb 1 \& my $bytes = $ws\->client_write; .Ve .PP Write data client-side, used to implement user agents. .SS "connection" .IX Subsection "connection" .Vb 1 \& my $connection = $ws\->connection; .Ve .PP Connection identifier or socket. .SS "finish" .IX Subsection "finish" .Vb 3 \& $ws = $ws\->finish; \& $ws = $ws\->finish(1000); \& $ws = $ws\->finish(1003 => \*(AqCannot accept data!\*(Aq); .Ve .PP Close WebSocket connection gracefully. .SS "is_websocket" .IX Subsection "is_websocket" .Vb 1 \& my $true = $ws\->is_websocket; .Ve .PP True. .SS "kept_alive" .IX Subsection "kept_alive" .Vb 1 \& my $kept_alive = $ws\->kept_alive; .Ve .PP Connection has been kept alive. .SS "local_address" .IX Subsection "local_address" .Vb 1 \& my $address = $ws\->local_address; .Ve .PP Local interface address. .SS "local_port" .IX Subsection "local_port" .Vb 1 \& my $port = $ws\->local_port; .Ve .PP Local interface port. .SS "parse_frame" .IX Subsection "parse_frame" .Vb 1 \& my $frame = $ws\->parse_frame(\e$bytes); .Ve .PP Parse WebSocket frame. .PP .Vb 8 \& # Parse single frame and remove it from buffer \& my $frame = $ws\->parse_frame(\e$buffer); \& say "FIN: $frame\->[0]"; \& say "RSV1: $frame\->[1]"; \& say "RSV2: $frame\->[2]"; \& say "RSV3: $frame\->[3]"; \& say "Opcode: $frame\->[4]"; \& say "Payload: $frame\->[5]"; .Ve .SS "remote_address" .IX Subsection "remote_address" .Vb 1 \& my $address = $ws\->remote_address; .Ve .PP Remote interface address. .SS "remote_port" .IX Subsection "remote_port" .Vb 1 \& my $port = $ws\->remote_port; .Ve .PP Remote interface port. .SS "req" .IX Subsection "req" .Vb 1 \& my $req = $ws\->req; .Ve .PP Handshake request, usually a Mojo::Message::Request object. .SS "res" .IX Subsection "res" .Vb 1 \& my $res = $ws\->res; .Ve .PP Handshake response, usually a Mojo::Message::Response object. .SS "resume" .IX Subsection "resume" .Vb 1 \& $ws = $ws\->resume; .Ve .PP Resume \f(CW\*(C`handshake\*(C'\fR transaction. .SS "send" .IX Subsection "send" .Vb 6 \& $ws = $ws\->send({binary => $bytes}); \& $ws = $ws\->send({text => $bytes}); \& $ws = $ws\->send([$fin, $rsv1, $rsv2, $rsv3, $op, $bytes]); \& $ws = $ws\->send(Mojo::ByteStream\->new($chars)); \& $ws = $ws\->send($chars); \& $ws = $ws\->send($chars => sub {...}); .Ve .PP Send message or frame non-blocking via WebSocket, the optional drain callback will be invoked once all data has been written. .PP .Vb 2 \& # Send "Ping" frame \& $ws\->send([1, 0, 0, 0, 9, \*(AqHello World!\*(Aq]); .Ve .SS "server_close" .IX Subsection "server_close" .Vb 1 \& $ws\->server_close; .Ve .PP Transaction closed server-side, used to implement web servers. .SS "server_handshake" .IX Subsection "server_handshake" .Vb 1 \& $ws\->server_handshake; .Ve .PP Perform WebSocket handshake server-side, used to implement web servers. .SS "server_read" .IX Subsection "server_read" .Vb 1 \& $ws\->server_read($data); .Ve .PP Read data server-side, used to implement web servers. .SS "server_write" .IX Subsection "server_write" .Vb 1 \& my $bytes = $ws\->server_write; .Ve .PP Write data server-side, used to implement web servers. .SH "DEBUGGING" .IX Header "DEBUGGING" You can set the \s-1MOJO_WEBSOCKET_DEBUG\s0 environment variable to get some advanced diagnostics information printed to \f(CW\*(C`STDERR\*(C'\fR. .PP .Vb 1 \& MOJO_WEBSOCKET_DEBUG=1 .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .