1Mojo::WebSocket(3)    User Contributed Perl Documentation   Mojo::WebSocket(3)
2
3
4

NAME

6       Mojo::WebSocket - The WebSocket protocol
7

SYNOPSIS

9         use Mojo::WebSocket qw(WS_TEXT build_frame parse_frame);
10
11         my $bytes = build_frame 0, 1, 0, 0, 0, WS_TEXT, 'Hello World!';
12         my $frame = parse_frame \$bytes, 262144;
13

DESCRIPTION

15       Mojo::WebSocket implements the WebSocket protocol as described in RFC
16       6455 <https://tools.ietf.org/html/rfc6455>.  Note that 64-bit frames
17       require a Perl with support for quads or they are limited to 32-bit.
18

FUNCTIONS

20       Mojo::WebSocket implements the following functions, which can be
21       imported individually.
22
23   build_frame
24         my $bytes = build_frame $masked, $fin, $rsv1, $rsv2, $rsv3, $op, $payload;
25
26       Build WebSocket frame.
27
28         # Masked binary frame with FIN bit and payload
29         say build_frame 1, 1, 0, 0, 0, WS_BINARY, 'Hello World!';
30
31         # Text frame with payload but without FIN bit
32         say build_frame 0, 0, 0, 0, 0, WS_TEXT, 'Hello ';
33
34         # Continuation frame with FIN bit and payload
35         say build_frame 0, 1, 0, 0, 0, WS_CONTINUATION, 'World!';
36
37         # Close frame with FIN bit and without payload
38         say build_frame 0, 1, 0, 0, 0, WS_CLOSE, '';
39
40         # Ping frame with FIN bit and payload
41         say build_frame 0, 1, 0, 0, 0, WS_PING, 'Test 123';
42
43         # Pong frame with FIN bit and payload
44         say build_frame 0, 1, 0, 0, 0, WS_PONG, 'Test 123';
45
46   challenge
47         my $bool = challenge Mojo::Transaction::WebSocket->new;
48
49       Check WebSocket handshake challenge.
50
51   client_handshake
52         my $tx = client_handshake Mojo::Transaction::HTTP->new;
53
54       Perform WebSocket handshake client-side.
55
56   parse_frame
57         my $frame = parse_frame \$bytes, $limit;
58
59       Parse WebSocket frame.
60
61         # Parse single frame and remove it from buffer
62         my $frame = parse_frame \$buffer, 262144;
63         say "FIN: $frame->[0]";
64         say "RSV1: $frame->[1]";
65         say "RSV2: $frame->[2]";
66         say "RSV3: $frame->[3]";
67         say "Opcode: $frame->[4]";
68         say "Payload: $frame->[5]";
69
70   server_handshake
71         my $tx = server_handshake Mojo::Transaction::HTTP->new;
72
73       Perform WebSocket handshake server-side.
74

CONSTANTS

76       Mojo::WebSocket implements the following constants, which can be
77       imported individually.
78
79   WS_BINARY
80       Opcode for "Binary" frames.
81
82   WS_CLOSE
83       Opcode for "Close" frames.
84
85   WS_CONTINUATION
86       Opcode for "Continuation" frames.
87
88   WS_PING
89       Opcode for "Ping" frames.
90
91   WS_PONG
92       Opcode for "Pong" frames.
93
94   WS_TEXT
95       Opcode for "Text" frames.
96

DEBUGGING

98       You can set the "MOJO_WEBSOCKET_DEBUG" environment variable to get some
99       advanced diagnostics information printed to "STDERR".
100
101         MOJO_WEBSOCKET_DEBUG=1
102

SEE ALSO

104       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
105
106
107
108perl v5.38.0                      2023-09-11                Mojo::WebSocket(3)
Impressum