WebSocket::Response - WebSocket Response
use WebSocket::Response; my $res = WebSocket::Response->new( host => 'example.com', uri => '/demo', origin => 'http://example.com', number1 => 777_007_543, number2 => 114_997_259, challenge => "\x47\x30\x22\x2D\x5A\x3F\x47\x58" ) || die( WebSocket::Response->error, "\n" ); $res->as_string; # HTTP/1.1 101 WebSocket Protocol Handshake # Upgrade: WebSocket # Connection: Upgrade # Sec-WebSocket-Origin: http://example.com # Sec-WebSocket-Location: ws://example.com/demo # # 0st3Rl&q-2ZU^weu # Parser $res = WebSocket::Response->new; $res->parse( <<EOT ); HTTP/1.1 101 WebSocket Protocol Handshake Upgrade: WebSocket Connection: Upgrade Sec-WebSocket-Origin: file:// Sec-WebSocket-Location: ws://example.com/demo 0st3Rl&q-2ZU^weu EOT
v0.1.0
Class to build or parse a WebSocket response. It inherits all the methods from WebSocket::Common. For convenience, they are all listed here.
my $req = WebSocket::Response->new( $code, $status, $headers, $buffer, host => 'example.com', uri => 'wss://example.com/chat', origin => 'http://example.com', subprotocol => 'com.example.chat' ); my $req = WebSocket::Response->new( $code, $status, $headers, $buffer, { host => 'example.com', uri => 'wss://example.com/chat', origin => 'http://example.com', subprotocol => 'com.example.chat' }); my $req = WebSocket::Response->new( $code, $status, $headers, host => 'example.com', uri => 'wss://example.com/chat', origin => 'http://example.com', subprotocol => 'com.example.chat' ); my $req = WebSocket::Response->new( $code, $status, $headers, { host => 'example.com', uri => 'wss://example.com/chat', origin => 'http://example.com', subprotocol => 'com.example.chat' });
Provided with an http code, http status, an optional set of headers, as either an array reference or a HTTP::Headers object, some optional content and an hash or hash reference of parameters, and this instantiates a new WebSocket::Response object. The supported parameters are as follow. Each parameter can be set or changed later using the method with the same name:
Content buffer
An integer representing the status code, such as 101 (switching protocol).
101
A Set-Cookie response header string. The string provided must be already properly formatted and encoded and will be added as is. For example:
Set-Cookie
WebSocket::Request->new( cookies => q{id=a3fWa; Max-Age=2592000}, host => 'example.com' );
Either an array reference of header-value pairs, or an HTTP::Headers object.
If an array reference is provided, an HTTP::Headers object will be instantiated with it.
Integer. Defaults to 20Kb. This is the maximum payload size.
The Origin header value.
Origin
See rfc6454
HTTP/1.1. This is the only version supported by rfc6455
Boolean. This is set to true when the connection is using ssl (i.e. wss), false otherwise.
wss
The status line, such as Switching Protocol. This is set upon parsing. There should be no need to set this by yourself.
Switching Protocol
The optional subprotocol which consists of multiple arbitrary identifiers that need to be recognised and supported by the server.
WebSocket::Request->new( subprotocol => 'com.example.chat', ); # or WebSocket::Request->new( subprotocol => [qw( com.example.chat com.example.internal )], );
See rfc6455
The request uri, such as /chat or it could also be a fully qualified uri such as wss://example.com/chat
/chat
wss://example.com/chat
The WebSocket protocol version. Defaults to draft-ietf-hybi-17
draft-ietf-hybi-17
Same as version, but pass an array of WebSocket::Version objects or version number, or draft identifier such as <draft-ietf-hybi-17>
Set or get the HTTP::Headers object. If none is set, and this method is accessed, a new one will be instantiated.
Calls as_string on HTTP::Headers and returns its value.
as_string
Set or get the Host header value.
Host
Set or get the boolean value. This is set to signal the parsing is complete.
my $rv = $res->parse( $some_response_data ) || die( $res->error );
Provided with some request content buffer and this will parse it using HTTP::Headers for the headers and the body with this module.
It returns undef and set an error object upon failure, and returns the current object on success.
undef
This method is kind of a misnomer because it actually performs header-parsing post processing mostly. It does some body processing for earlier version of the protocol when the handshake challenge was in the body rather than in the header.
It also tries to find out the protocol version used by the other party.
Returns the current object used.
Set or get the protocol used. Typically HTTP/1.1. This is set upon parsing. You should not have to set this yourself.
HTTP/1.1
Boolean value. True if the connection is using ssl, i.e. wss
Set or get the response status line, such as Switching Protocol
Set or get an array object (Module::Generic::Array) of subprotocols.
See rfc6455 for more information
Set or get the request uri. This returns a URI object.
Set the protocol version.
See rfc6455 section 4.1 for more information
Same as "versions", but pass an array of WebSocket::Version objects or version number, or draft identifier such as <draft-ietf-hybi-17>
Multiple versions are part of the protocol handshake negotiation from protocol version 4 and above.
Jacques Deguest <jack@deguest.jp>
perl
Copyright(c) 2021 DEGUEST Pte. Ltd. DEGUEST Pte. Ltd.
To install WebSocket, copy and paste the appropriate command in to your terminal.
cpanm
cpanm WebSocket
CPAN shell
perl -MCPAN -e shell install WebSocket
For more information on module installation, please visit the detailed CPAN module installation guide.