1Mojo::Transaction::WebSUoscekretC(o3n)tributed Perl DocuMmoejnot:a:tTiroannsaction::WebSocket(3)
2
3
4
6 Mojo::Transaction::WebSocket - WebSocket transaction
7
9 use Mojo::Transaction::WebSocket;
10
11 # Send and receive WebSocket messages
12 my $ws = Mojo::Transaction::WebSocket->new;
13 $ws->send('Hello World!');
14 $ws->on(message => sub {
15 my ($ws, $msg) = @_;
16 say "Message: $msg";
17 });
18 $ws->on(finish => sub {
19 my ($ws, $code, $reason) = @_;
20 say "WebSocket closed with status $code.";
21 });
22
24 Mojo::Transaction::WebSocket is a container for WebSocket transactions,
25 based on RFC 6455 <http://tools.ietf.org/html/rfc6455> and RFC 7692
26 <http://tools.ietf.org/html/rfc7692>.
27
29 Mojo::Transaction::WebSocket inherits all events from Mojo::Transaction
30 and can emit the following new ones.
31
32 binary
33 $ws->on(binary => sub {
34 my ($ws, $bytes) = @_;
35 ...
36 });
37
38 Emitted when a complete WebSocket binary message has been received.
39
40 $ws->on(binary => sub {
41 my ($ws, $bytes) = @_;
42 say "Binary: $bytes";
43 });
44
45 drain
46 $ws->on(drain => sub {
47 my $ws = shift;
48 ...
49 });
50
51 Emitted once all data has been sent.
52
53 $ws->on(drain => sub {
54 my $ws = shift;
55 $ws->send(time);
56 });
57
58 finish
59 $ws->on(finish => sub {
60 my ($ws, $code, $reason) = @_;
61 ...
62 });
63
64 Emitted when the WebSocket connection has been closed.
65
66 frame
67 $ws->on(frame => sub {
68 my ($ws, $frame) = @_;
69 ...
70 });
71
72 Emitted when a WebSocket frame has been received.
73
74 $ws->on(frame => sub {
75 my ($ws, $frame) = @_;
76 say "FIN: $frame->[0]";
77 say "RSV1: $frame->[1]";
78 say "RSV2: $frame->[2]";
79 say "RSV3: $frame->[3]";
80 say "Opcode: $frame->[4]";
81 say "Payload: $frame->[5]";
82 });
83
84 json
85 $ws->on(json => sub {
86 my ($ws, $json) = @_;
87 ...
88 });
89
90 Emitted when a complete WebSocket message has been received, all text
91 and binary messages will be automatically JSON decoded. Note that this
92 event only gets emitted when it has at least one subscriber.
93
94 $ws->on(json => sub {
95 my ($ws, $hash) = @_;
96 say "Message: $hash->{msg}";
97 });
98
99 message
100 $ws->on(message => sub {
101 my ($ws, $msg) = @_;
102 ...
103 });
104
105 Emitted when a complete WebSocket message has been received, text
106 messages will be automatically decoded. Note that this event only gets
107 emitted when it has at least one subscriber.
108
109 $ws->on(message => sub {
110 my ($ws, $msg) = @_;
111 say "Message: $msg";
112 });
113
114 resume
115 $tx->on(resume => sub {
116 my $tx = shift;
117 ...
118 });
119
120 Emitted when transaction is resumed.
121
122 text
123 $ws->on(text => sub {
124 my ($ws, $bytes) = @_;
125 ...
126 });
127
128 Emitted when a complete WebSocket text message has been received.
129
130 $ws->on(text => sub {
131 my ($ws, $bytes) = @_;
132 say "Text: $bytes";
133 });
134
136 Mojo::Transaction::WebSocket inherits all attributes from
137 Mojo::Transaction and implements the following new ones.
138
139 compressed
140 my $bool = $ws->compressed;
141 $ws = $ws->compressed($bool);
142
143 Compress messages with "permessage-deflate" extension.
144
145 established
146 my $bool = $ws->established;
147 $ws = $ws->established($bool);
148
149 WebSocket connection established.
150
151 handshake
152 my $handshake = $ws->handshake;
153 $ws = $ws->handshake(Mojo::Transaction::HTTP->new);
154
155 The original handshake transaction, usually a Mojo::Transaction::HTTP
156 object.
157
158 masked
159 my $bool = $ws->masked;
160 $ws = $ws->masked($bool);
161
162 Mask outgoing frames with XOR cipher and a random 32-bit key.
163
164 max_websocket_size
165 my $size = $ws->max_websocket_size;
166 $ws = $ws->max_websocket_size(1024);
167
168 Maximum WebSocket message size in bytes, defaults to the value of the
169 "MOJO_MAX_WEBSOCKET_SIZE" environment variable or 262144 (256KiB).
170
172 Mojo::Transaction::WebSocket inherits all methods from
173 Mojo::Transaction and implements the following new ones.
174
175 build_message
176 my $frame = $ws->build_message({binary => $bytes});
177 my $frame = $ws->build_message({text => $bytes});
178 my $frame = $ws->build_message({json => {test => [1, 2, 3]}});
179 my $frame = $ws->build_message($chars);
180
181 Build WebSocket message.
182
183 client_read
184 $ws->client_read($data);
185
186 Read data client-side, used to implement user agents such as
187 Mojo::UserAgent.
188
189 client_write
190 my $bytes = $ws->client_write;
191
192 Write data client-side, used to implement user agents such as
193 Mojo::UserAgent.
194
195 closed
196 $tx = $tx->closed;
197
198 Same as "completed" in Mojo::Transaction, but also indicates that all
199 transaction data has been sent.
200
201 connection
202 my $id = $ws->connection;
203
204 Connection identifier.
205
206 finish
207 $ws = $ws->finish;
208 $ws = $ws->finish(1000);
209 $ws = $ws->finish(1003 => 'Cannot accept data!');
210
211 Close WebSocket connection gracefully.
212
213 is_websocket
214 my $bool = $ws->is_websocket;
215
216 True, this is a Mojo::Transaction::WebSocket object.
217
218 kept_alive
219 my $bool = $ws->kept_alive;
220
221 Connection has been kept alive.
222
223 local_address
224 my $address = $ws->local_address;
225
226 Local interface address.
227
228 local_port
229 my $port = $ws->local_port;
230
231 Local interface port.
232
233 parse_message
234 $ws->parse_message([$fin, $rsv1, $rsv2, $rsv3, $op, $payload]);
235
236 Parse WebSocket message.
237
238 protocol
239 my $proto = $ws->protocol;
240
241 Return negotiated subprotocol or "undef".
242
243 remote_address
244 my $address = $ws->remote_address;
245
246 Remote interface address.
247
248 remote_port
249 my $port = $ws->remote_port;
250
251 Remote interface port.
252
253 req
254 my $req = $ws->req;
255
256 Handshake request, usually a Mojo::Message::Request object.
257
258 res
259 my $res = $ws->res;
260
261 Handshake response, usually a Mojo::Message::Response object.
262
263 resume
264 $ws = $ws->resume;
265
266 Resume "handshake" transaction.
267
268 send
269 $ws = $ws->send({binary => $bytes});
270 $ws = $ws->send({text => $bytes});
271 $ws = $ws->send({json => {test => [1, 2, 3]}});
272 $ws = $ws->send([$fin, $rsv1, $rsv2, $rsv3, $op, $payload]);
273 $ws = $ws->send($chars);
274 $ws = $ws->send($chars => sub {...});
275
276 Send message or frame non-blocking via WebSocket, the optional drain
277 callback will be executed once all data has been written.
278
279 # Send "Ping" frame
280 use Mojo::WebSocket 'WS_PING';
281 $ws->send([1, 0, 0, 0, WS_PING, 'Hello World!']);
282
283 server_read
284 $ws->server_read($data);
285
286 Read data server-side, used to implement web servers such as
287 Mojo::Server::Daemon.
288
289 server_write
290 my $bytes = $ws->server_write;
291
292 Write data server-side, used to implement web servers such as
293 Mojo::Server::Daemon.
294
295 with_compression
296 $ws->with_compression;
297
298 Negotiate "permessage-deflate" extension for this WebSocket connection.
299
300 with_protocols
301 my $proto = $ws->with_protocols('v2.proto', 'v1.proto');
302
303 Negotiate subprotocol for this WebSocket connection.
304
306 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
307
308
309
310perl v5.28.0 2018-09-14 Mojo::Transaction::WebSocket(3)