Mojo/Transaction/WebSocket.pm

filter: Statementstmt 84.2% 91 / 108 Branchbran 67.7% 42 / 62 Conditioncond 44.1% 15 / 34 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 66.6% 20 / 30 Podpod 100% 22 / 22 totaltotal 71.1% 195 / 274 CC 43 SCAR 21.1
CC
43
Cov
73%
with_compression
20.0
parse_message
19.4
with_protocols
12.0
CRAP 8.3
« Mojolicious/Plugin/NotYAMLConfig.pm ↑ Summary Mojo/SSE.pm »
1 package Mojo::Transaction::WebSocket;
2 25 ▶use Mojo::Base 'Mojo::Transaction';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 25 ▶use Compress::Raw::Zlib qw(Z_SYNC_FLUSH);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 ▶use Mojo::JSON qw(encode_json j);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use Mojo::Util qw(decode encode trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use Mojo::WebSocket qw(WS_BINARY WS_CLOSE WS_CONTINUATION WS_PING WS_PONG WS_TEXT);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 25 has [qw(compressed established handshake masked)];
11 5 ▶has max_websocket_size => sub { $ENV{MOJO_MAX_WEBSOCKET_SIZE} || 262144 };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"MOJO_MAX_WEBSOCKET_SIZE"} or 262144 0 5 5
12  
13 12 ▶sub build_message {
Subroutine build_message: called CC=6 SCAR=18.0
Pod documented
14 12 my ($self, $frame) = @_;
15  
16 # Text
17 12 ▶ $frame = {text => encode('UTF-8', $frame)} if ref $frame ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $frame ne "HASH" 2 10 12
18  
19 # JSON
20 12 ▶ $frame->{text} = encode_json($frame->{json}) if exists $frame->{json};
Branch: 1 branch
BranchTrueFalseTotal
if exists $frame->{"json"} 0 12 12
21  
22 # Raw text or binary
23 12 ▶ if (exists $frame->{text}) { $frame = [1, 0, 0, 0, WS_TEXT, $frame->{text}] }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $frame->{'text'}) { } 9 3 12
24 3 else { $frame = [1, 0, 0, 0, WS_BINARY, $frame->{binary}] }
25  
26 # "permessage-deflate" extension
27 12 ▶ return $frame unless $self->compressed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->compressed 4 8 12
28 my $deflate = $self->{deflate}
29 8 ▶ ||= Compress::Raw::Zlib::Deflate->new(AppendOutput => 1, MemLevel => 8, WindowBits => -15);
Condition66%2/3
$self->{"deflate"} ||= "Compress::Raw::Zlib::Deflate"->new("AppendOutput", 1, "MemLevel", 8, "WindowBits", -15)
l!l&&r!l&&!r
440
MC/DC0%0/2
$self->{"deflate"} ||= "Compress::Raw::Zlib::Deflate"->new("AppendOutput", 1, "MemLevel", 8, "WindowBits", -15)
$self->{"deflate"} "Compress::Raw::Zlib::Deflate"->new("AppendOutput", 1, "MemLevel", 8, "WindowBits", -15)
Truth table66%2/3
A ||= B
A= $self->{"deflate"}B= "Compress::Raw::Zlib::Deflate"->new("AppendOutput", 1, "MemLevel", 8, "WindowBits", -15)
ABresult
1X1
011
000
30 8 $deflate->deflate($frame->[5], my $out);
31 8 $deflate->flush($out, Z_SYNC_FLUSH);
32 8 $deflate->deflateReset;
33 8 @$frame[1, 5] = (1, substr($out, 0, length($out) - 4));
34  
35 8 return $frame;
36 }
37  
38 3 ▶sub client_read { shift->server_read(@_) }
Subroutine client_read: called CC=1 SCAR=0.0
Pod documented
39 8 ▶sub client_write { shift->server_write(@_) }
Subroutine client_write: called CC=1 SCAR=0.0
Pod documented
40  
41 2 ▶sub closed {
Subroutine closed: called CC=3 SCAR=11.4
Pod documented
42 2 my $self = shift->completed;
43 2 ▶ my @args = $self->{close} ? (@{$self->{close}}) : (1006);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'close'} ? : 2 0 2
44 2 ▶ return $self->emit(finish => @args > 1 ? @args : (@args, undef));
Branch: 1 branch
BranchTrueFalseTotal
@args > 1 ? : 0 2 2
45 }
46  
47 0 ▶sub connection { shift->handshake->connection }
Subroutine connection: not called CC=1 SCAR=6.9
Pod documented
48  
49 3 ▶sub finish {
Subroutine finish: called CC=4 SCAR=14.3
Pod documented
50 3 my $self = shift;
51  
52 3 my $close = $self->{close} = [@_];
53 3 ▶ my $payload = $close->[0] ? pack('n', $close->[0]) : '';
Branch: 1 branch
BranchTrueFalseTotal
$close->[0] ? : 0 3 3
54 3 ▶ $payload .= encode 'UTF-8', $close->[1] if defined $close->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $close->[1] 0 3 3
55 3 ▶ $close->[0] //= 1005;
Condition50%1/2
$close->[0] //= 1005
l!l
03
MC/DC0%0/1
$close->[0] //= 1005
$close->[0]
Truth table50%1/2
A
A= $close->[0]
Aresult
11
00
56 3 $self->send([1, 0, 0, 0, WS_CLOSE, $payload])->{closing} = 1;
57  
58 3 return $self;
59 }
60  
61 10 ▶sub is_websocket {1}
Subroutine is_websocket: called CC=1 SCAR=0.0
Pod documented
62  
63 0 ▶sub kept_alive { shift->handshake->kept_alive }
Subroutine kept_alive: not called CC=1 SCAR=6.9
Pod documented
64 0 ▶sub local_address { shift->handshake->local_address }
Subroutine local_address: not called CC=1 SCAR=6.9
Pod documented
65 0 ▶sub local_port { shift->handshake->local_port }
Subroutine local_port: not called CC=1 SCAR=6.9
Pod documented
66  
67 13 ▶sub parse_message {
Subroutine parse_message: called CC=17 SCAR=29.7
Pod documented
68 13 my ($self, $frame) = @_;
69  
70 13 $self->emit(frame => $frame);
71  
72 # Ping/Pong
73 13 my $op = $frame->[4];
74 13 ▶ return $self->send([1, 0, 0, 0, WS_PONG, $frame->[5]]) if $op == WS_PING;
Branch: 1 branch
BranchTrueFalseTotal
if $op == 9 0 13 13
75 13 ▶ return undef if $op == WS_PONG;
Branch: 1 branch
BranchTrueFalseTotal
if $op == 10 0 13 13
76  
77 # Close
78 13 ▶ if ($op == WS_CLOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 8) 2 11 13
79 2 ▶ return $self->finish unless length $frame->[5] >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless length $frame->[5] >= 2 2 0 2
80 0 return $self->finish(unpack('n', substr($frame->[5], 0, 2, '')), decode('UTF-8', $frame->[5]));
81 }
82  
83 # Append chunk and check message size
84 11 ▶ @{$self}{qw(op pmc)} = ($op, $self->compressed && $frame->[1]) unless exists $self->{op};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"op"} 7 4 11
Condition66%2/3
$self->compressed && $frame->[1]
!ll&&!rl&&r
304
MC/DC50%1/2
$self->compressed && $frame->[1]
$self->compressed $frame->[1]
Truth table66%2/3
A && B
A= $self->compressedB= $frame->[1]
ABresult
0X0
100
111
85 11 $self->{message} .= $frame->[5];
86 11 my $max = $self->max_websocket_size;
87 11 ▶ return $self->finish(1009) if length $self->{message} > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"message"} > $max 0 11 11
88  
89 # No FIN bit (Continuation)
90 11 ▶ return undef unless $frame->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $frame->[0] 4 7 11
91  
92 # "permessage-deflate" extension (handshake and RSV1)
93 7 my $msg = delete $self->{message};
94 7 ▶ if ($self->compressed && $self->{pmc}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->compressed and $self->{"pmc"}) 4 3 7
Condition66%2/3
$self->compressed and $self->{"pmc"}
!ll&&!rl&&r
304
MC/DC50%1/2
$self->compressed and $self->{"pmc"}
$self->compressed $self->{"pmc"}
Truth table66%2/3
A and B
A= $self->compressedB= $self->{"pmc"}
ABresult
0X0
100
111
95 my $inflate = $self->{inflate}
96 4 ▶ ||= Compress::Raw::Zlib::Inflate->new(Bufsize => $max, LimitOutput => 1, WindowBits => -15);
Condition66%2/3
$self->{"inflate"} ||= "Compress::Raw::Zlib::Inflate"->new("Bufsize", $max, "LimitOutput", 1, "WindowBits", -15)
l!l&&r!l&&!r
220
MC/DC0%0/2
$self->{"inflate"} ||= "Compress::Raw::Zlib::Inflate"->new("Bufsize", $max, "LimitOutput", 1, "WindowBits", -15)
$self->{"inflate"} "Compress::Raw::Zlib::Inflate"->new("Bufsize", $max, "LimitOutput", 1, "WindowBits", -15)
Truth table66%2/3
A ||= B
A= $self->{"inflate"}B= "Compress::Raw::Zlib::Inflate"->new("Bufsize", $max, "LimitOutput", 1, "WindowBits", -15)
ABresult
1X1
011
000
97 4 $inflate->inflate(($msg .= "\x00\x00\xff\xff"), my $out);
98 4 ▶ return $self->finish(1009) if length $msg;
Branch: 1 branch
BranchTrueFalseTotal
if length $msg 0 4 4
99 4 $msg = $out;
100 }
101  
102 7 ▶ $self->emit(json => j($msg)) if $self->has_subscribers('json');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_subscribers("json") 0 7 7
103 7 $op = delete $self->{op};
104 7 ▶ $self->emit($op == WS_TEXT ? 'text' : 'binary' => $msg);
Branch: 1 branch
BranchTrueFalseTotal
$op == 1 ? : 7 0 7
105 7 ▶ $self->emit(message => $op == WS_TEXT ? decode 'UTF-8', $msg : $msg) if $self->has_subscribers('message');
Branch: 2 branches
BranchTrueFalseTotal
if $self->has_subscribers("message") 5 2 7
$op == 1 ? : 5 0 5
106 }
107  
108 0 ▶sub protocol { shift->res->headers->sec_websocket_protocol }
Subroutine protocol: not called CC=1 SCAR=6.9
Pod documented
109  
110 0 ▶sub remote_address { shift->handshake->remote_address }
Subroutine remote_address: not called CC=1 SCAR=6.9
Pod documented
111 0 ▶sub remote_port { shift->handshake->remote_port }
Subroutine remote_port: not called CC=1 SCAR=6.9
Pod documented
112 6 ▶sub req { shift->handshake->req }
Subroutine req: called CC=1 SCAR=0.0
Pod documented
113 8 ▶sub res { shift->handshake->res }
Subroutine res: called CC=1 SCAR=0.0
Pod documented
114  
115 1 ▶sub resume { $_[0]->handshake->resume and return $_[0] }
Subroutine resume: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->handshake->resume and return $_[0] 1 0 1
116  
117 5 ▶sub send {
Subroutine send: called CC=3 SCAR=11.0
Pod documented
118 5 my ($self, $msg, $cb) = @_;
119 5 ▶ $self->once(drain => $cb) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 1 4 5
120 5 ▶ $msg = $self->build_message($msg) unless ref $msg eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $msg eq "ARRAY" 2 3 5
121 5 $self->{write} .= Mojo::WebSocket::build_frame($self->masked, @$msg);
122 5 return $self->emit('resume');
123 }
124  
125 6 ▶sub server_read {
Subroutine server_read: called CC=4 SCAR=15.0
Pod documented
126 6 my ($self, $chunk) = @_;
127  
128 6 $self->{read} .= $chunk;
129 6 my $max = $self->max_websocket_size;
130 6 ▶ while (my $frame = Mojo::WebSocket::parse_frame(\$self->{read}, $max)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $frame = Mojo::WebSocket::parse_frame(\$self->{"read"}, $max)) 4 6 10
131 4 ▶ $self->finish(1009) and last unless ref $frame;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $frame 0 4 4
Condition20%1/5
ref $frame || $self->finish(1009) && (last)
l!l&&r!l&&!r
400
$self->finish(1009) and last
!ll
00
MC/DC0%0/2
ref $frame || $self->finish(1009) && (last)
ref $frame $self->finish(1009)
Truth table33%1/3
A || B
A= ref $frameB= $self->finish(1009)
ABresult
1X1
011
000
132 4 $self->parse_message($frame);
133 }
134  
135 6 $self->emit('resume');
136 }
137  
138 16 ▶sub server_write {
Subroutine server_write: called CC=5 SCAR=16.1
Pod documented
139 16 my $self = shift;
140 16 ▶ $self->emit('drain') unless length($self->{write} //= '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($self->{"write"} //= "") 11 5 16
Condition100%2/2
$self->{"write"} //= ""
l!l
511
MC/DC100%1/1
$self->{"write"} //= ""
$self->{"write"}
Truth table100%2/2
A
A= $self->{"write"}
Aresult
11
00
141 16 ▶ $self->completed if !length $self->{write} && $self->{closing};
Branch: 1 branch
BranchTrueFalseTotal
if not length $self->{"write"} and $self->{"closing"} 4 12 16
Condition100%3/3
not length $self->{"write"} and $self->{"closing"}
!ll&&!rl&&r
574
MC/DC100%2/2
not length $self->{"write"} and $self->{"closing"}
not length $self->{"write"} $self->{"closing"}
Truth table100%3/3
not(A) and B
A= not length $self->{"write"}B= $self->{"closing"}
ABresult
0X0
100
111
142 16 return delete $self->{write};
143 }
144  
145 0 ▶sub with_compression {
Subroutine with_compression: not called CC=4 SCAR=30.0
Pod documented
146 0 my $self = shift;
147  
148 # "permessage-deflate" extension
149 0 ▶ $self->compressed(1) and $self->res->headers->sec_websocket_extensions('permessage-deflate')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->req->headers->sec_websocket_extensions // "") =~ /permessage-deflate/u 0 0 0
Condition0%0/8
($self->req->headers->sec_websocket_extensions // '') =~ /permessage-deflate/u && ($self->compressed(1) && $self->res->headers->sec_websocket_extensions('permessage-deflate'))
!ll&&!rl&&r
000
$self->req->headers->sec_websocket_extensions // ""
l!l
00
$self->compressed(1) and $self->res->headers->sec_websocket_extensions("permessage-deflate")
!ll&&!rl&&r
000
MC/DC0%0/3
($self->req->headers->sec_websocket_extensions // '') =~ /permessage-deflate/u && ($self->compressed(1) && $self->res->headers->sec_websocket_extensions('permessage-deflate'))
$self->req->headers->sec_websocket_extensions $self->compressed(1) $self->res->headers->sec_websocket_extensions("permessage-deflate")
Truth table0%0/4
A && B and C
A= $self->req->headers->sec_websocket_extensionsB= $self->compressed(1)C= $self->res->headers->sec_websocket_extensions("permessage-deflate")
ABCresult
0XX0
10X0
1100
1111
150 if ($self->req->headers->sec_websocket_extensions // '') =~ /permessage-deflate/;
151 }
152  
153 0 ▶sub with_protocols {
Subroutine with_protocols: not called CC=3 SCAR=24.8
Pod documented
154 0 my $self = shift;
155  
156 0 ▶ my %protos = map { trim($_) => 1 } split /,/, $self->req->headers->sec_websocket_protocol // '';
Condition0%0/2
$self->req->headers->sec_websocket_protocol // ""
l!l
00
MC/DC0%0/1
$self->req->headers->sec_websocket_protocol // ""
$self->req->headers->sec_websocket_protocol
Truth table0%0/2
A
A= $self->req->headers->sec_websocket_protocol
Aresult
11
00
157 0 ▶ return undef unless defined(my $proto = first { $protos{$_} } @_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $proto = &first(sub { $protos{$_}; } , @_)) 0 0 0
158  
159 0 $self->res->headers->sec_websocket_protocol($proto);
160 0 return $proto;
161 }
162  
163 25 1;
164  
165 =encoding utf8
166  
167 =head1 NAME
168  
169 Mojo::Transaction::WebSocket - WebSocket transaction
170  
171 =head1 SYNOPSIS
172  
173 use Mojo::Transaction::WebSocket;
174  
175 # Send and receive WebSocket messages
176 my $ws = Mojo::Transaction::WebSocket->new;
177 $ws->send('Hello World!');
178 $ws->on(message => sub ($ws, $msg) { say "Message: $msg" });
179 $ws->on(finish => sub ($ws, $code, $reason) { say "WebSocket closed with status $code." });
180  
181 =head1 DESCRIPTION
182  
183 L<Mojo::Transaction::WebSocket> is a container for WebSocket transactions, based on L<RFC
184 6455|https://tools.ietf.org/html/rfc6455> and L<RFC 7692|https://tools.ietf.org/html/rfc7692>.
185  
186 =head1 EVENTS
187  
188 L<Mojo::Transaction::WebSocket> inherits all events from L<Mojo::Transaction> and can emit the following new ones.
189  
190 =head2 binary
191  
192 $ws->on(binary => sub ($ws, $bytes) {...});
193  
194 Emitted when a complete WebSocket binary message has been received.
195  
196 $ws->on(binary => sub ($ws, $bytes) { say "Binary: $bytes" });
197  
198 =head2 drain
199  
200 $ws->on(drain => sub ($ws) {...});
201  
202 Emitted once all data has been sent.
203  
204 $ws->on(drain => sub ($ws) { $ws->send(time) });
205  
206 =head2 finish
207  
208 $ws->on(finish => sub ($ws, $code, $reason) {...});
209  
210 Emitted when the WebSocket connection has been closed.
211  
212 =head2 frame
213  
214 $ws->on(frame => sub ($ws, $frame) {...});
215  
216 Emitted when a WebSocket frame has been received.
217  
218 $ws->on(frame => sub ($ws, $frame) {
219 say "FIN: $frame->[0]";
220 say "RSV1: $frame->[1]";
221 say "RSV2: $frame->[2]";
222 say "RSV3: $frame->[3]";
223 say "Opcode: $frame->[4]";
224 say "Payload: $frame->[5]";
225 });
226  
227 =head2 json
228  
229 $ws->on(json => sub ($ws, $json) {...});
230  
231 Emitted when a complete WebSocket message has been received, all text and binary messages will be automatically JSON
232 decoded. Note that this event only gets emitted when it has at least one subscriber.
233  
234 $ws->on(json => sub ($ws, $hash) { say "Message: $hash->{msg}" });
235  
236 =head2 message
237  
238 $ws->on(message => sub ($ws, $msg) {...});
239  
240 Emitted when a complete WebSocket message has been received, text messages will be automatically decoded. Note that
241 this event only gets emitted when it has at least one subscriber.
242  
243 $ws->on(message => sub ($ws, $msg) { say "Message: $msg" });
244  
245 =head2 resume
246  
247 $tx->on(resume => sub ($tx) {...});
248  
249 Emitted when transaction is resumed.
250  
251 =head2 text
252  
253 $ws->on(text => sub ($ws, $bytes) {...});
254  
255 Emitted when a complete WebSocket text message has been received.
256  
257 $ws->on(text => sub ($ws, $bytes) { say "Text: $bytes" });
258  
259 =head1 ATTRIBUTES
260  
261 L<Mojo::Transaction::WebSocket> inherits all attributes from L<Mojo::Transaction> and implements the following new
262 ones.
263  
264 =head2 compressed
265  
266 my $bool = $ws->compressed;
267 $ws = $ws->compressed($bool);
268  
269 Compress messages with C<permessage-deflate> extension.
270  
271 =head2 established
272  
273 my $bool = $ws->established;
274 $ws = $ws->established($bool);
275  
276 WebSocket connection established.
277  
278 =head2 handshake
279  
280 my $handshake = $ws->handshake;
281 $ws = $ws->handshake(Mojo::Transaction::HTTP->new);
282  
283 The original handshake transaction, usually a L<Mojo::Transaction::HTTP> object.
284  
285 =head2 masked
286  
287 my $bool = $ws->masked;
288 $ws = $ws->masked($bool);
289  
290 Mask outgoing frames with XOR cipher and a random 32-bit key.
291  
292 =head2 max_websocket_size
293  
294 my $size = $ws->max_websocket_size;
295 $ws = $ws->max_websocket_size(1024);
296  
297 Maximum WebSocket message size in bytes, defaults to the value of the C<MOJO_MAX_WEBSOCKET_SIZE> environment variable
298 or C<262144> (256KiB).
299  
300 =head1 METHODS
301  
302 L<Mojo::Transaction::WebSocket> inherits all methods from L<Mojo::Transaction> and implements the following new ones.
303  
304 =head2 build_message
305  
306 my $frame = $ws->build_message({binary => $bytes});
307 my $frame = $ws->build_message({text => $bytes});
308 my $frame = $ws->build_message({json => {test => [1, 2, 3]}});
309 my $frame = $ws->build_message($chars);
310  
311 Build WebSocket message.
312  
313 =head2 client_read
314  
315 $ws->client_read($data);
316  
317 Read data client-side, used to implement user agents such as L<Mojo::UserAgent>.
318  
319 =head2 client_write
320  
321 my $bytes = $ws->client_write;
322  
323 Write data client-side, used to implement user agents such as L<Mojo::UserAgent>.
324  
325 =head2 closed
326  
327 $tx = $tx->closed;
328  
329 Same as L<Mojo::Transaction/"completed">, but also indicates that all transaction data has been sent.
330  
331 =head2 connection
332  
333 my $id = $ws->connection;
334  
335 Connection identifier.
336  
337 =head2 finish
338  
339 $ws = $ws->finish;
340 $ws = $ws->finish(1000);
341 $ws = $ws->finish(1003 => 'Cannot accept data!');
342  
343 Close WebSocket connection gracefully.
344  
345 =head2 is_websocket
346  
347 my $bool = $ws->is_websocket;
348  
349 True, this is a L<Mojo::Transaction::WebSocket> object.
350  
351 =head2 kept_alive
352  
353 my $bool = $ws->kept_alive;
354  
355 Connection has been kept alive.
356  
357 =head2 local_address
358  
359 my $address = $ws->local_address;
360  
361 Local interface address.
362  
363 =head2 local_port
364  
365 my $port = $ws->local_port;
366  
367 Local interface port.
368  
369 =head2 parse_message
370  
371 $ws->parse_message([$fin, $rsv1, $rsv2, $rsv3, $op, $payload]);
372  
373 Parse WebSocket message.
374  
375 =head2 protocol
376  
377 my $proto = $ws->protocol;
378  
379 Return negotiated subprotocol or C<undef>.
380  
381 =head2 remote_address
382  
383 my $address = $ws->remote_address;
384  
385 Remote interface address.
386  
387 =head2 remote_port
388  
389 my $port = $ws->remote_port;
390  
391 Remote interface port.
392  
393 =head2 req
394  
395 my $req = $ws->req;
396  
397 Handshake request, usually a L<Mojo::Message::Request> object.
398  
399 =head2 res
400  
401 my $res = $ws->res;
402  
403 Handshake response, usually a L<Mojo::Message::Response> object.
404  
405 =head2 resume
406  
407 $ws = $ws->resume;
408  
409 Resume L</"handshake"> transaction.
410  
411 =head2 send
412  
413 $ws = $ws->send({binary => $bytes});
414 $ws = $ws->send({text => $bytes});
415 $ws = $ws->send({json => {test => [1, 2, 3]}});
416 $ws = $ws->send([$fin, $rsv1, $rsv2, $rsv3, $op, $payload]);
417 $ws = $ws->send($chars);
418 $ws = $ws->send($chars => sub {...});
419  
420 Send message or frame non-blocking via WebSocket, the optional drain callback will be executed once all data has been
421 written.
422  
423 # Send "Ping" frame
424 use Mojo::WebSocket qw(WS_PING);
425 $ws->send([1, 0, 0, 0, WS_PING, 'Hello World!']);
426  
427 =head2 server_read
428  
429 $ws->server_read($data);
430  
431 Read data server-side, used to implement web servers such as L<Mojo::Server::Daemon>.
432  
433 =head2 server_write
434  
435 my $bytes = $ws->server_write;
436  
437 Write data server-side, used to implement web servers such as L<Mojo::Server::Daemon>.
438  
439 =head2 with_compression
440  
441 $ws->with_compression;
442  
443 Negotiate C<permessage-deflate> extension for this WebSocket connection.
444  
445 =head2 with_protocols
446  
447 my $proto = $ws->with_protocols('v2.proto', 'v1.proto');
448  
449 Negotiate subprotocol for this WebSocket connection.
450  
451 =head1 SEE ALSO
452  
453 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
454  
455 =cut
« Mojolicious/Plugin/NotYAMLConfig.pm ↑ Summary Mojo/SSE.pm »