UniEvent::WebSocket::ServerConnection - websocket peer connection on server
$ws_server->connection_callback(sub { my ($server, $connection, $connect_request) = @_; $connection->message_callback(sub {...}); ... });
UniEvent::WebSocket::ServerConnection is a subclass of UniEvent::WebSocket::Connection used in websocket server to represent peer connection.
UniEvent::WebSocket::ServerConnection
Returns connection id (64bit integer). Every connection in websocket server gets an unique id.
Sends http response for client's http upgrade request and accept websocket handshake. Should only be called from handshake_callback server's callback and only if there were no request errors.
handshake_callback
This is optional, if not called, server will send default handshake response.
This method is useful for sending additional data along with handshake response.
$response must be a Protocol::WebSocket::Fast::ConnectResponse object or a hashref that its contrustor supports.
$response
$server->handshake_callback(sub { my ($server, $conn, $req) = @_; if (!$req->error) { $conn->send_accept_response({ headers => {'My-Custom-Data' => $data}, }); } });
No need to fill in all the http fields for correct upgrade response, it will be done automatically.
Sends http response for client's http upgrade request and deny websocket handshake, closing the server connection. Should only be called from handshake_callback server's callback.
This is optional, if not called, server will send default handshake error response (if there were handshake errors).
You can send error response even if there were no handshake errors, for example, if you don't like the requested uri or some custom headers are missing.
$server->handshake_callback(sub { my ($server, $conn, $req) = @_; return if $req->error; # default error response will be sent automatically if ($req->uri->path ne '/my/ws') { $conn->send_accept_error({ code => 404, body => 'wrong uri', }); } elsif (!check_auth($req->headers->{'My-Auth'})) { $conn->send_accept_error({ code => 400, body => 'authorization required', }); } ... });
No need to fill in all the http fields for correct error response, it will be done automatically.
To install UniEvent::WebSocket, copy and paste the appropriate command in to your terminal.
cpanm
cpanm UniEvent::WebSocket
CPAN shell
perl -MCPAN -e shell install UniEvent::WebSocket
For more information on module installation, please visit the detailed CPAN module installation guide.