abstract class WS_Service

Overview

Server version of WS_Protocol. Make a derivative of this class, and it will automatically be registered with WS_Middleware, and will handle service requests for its #path.

Extended Modules

Defined in:

ws_service.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new #

[View source]

Class Method Detail

def self.graceful_shutdown(message : String = "Graceful shutdown") #
Description copied from class WS_Protocol

This is meant to be called for each connection when the server is shutting down. There is a potential race because I can't lock WebSocket#run from processing a read or write in @fiber, while this is called from another fiber.


[View source]

Instance Method Detail

def authenticate(path : String, params : URI::Params, requested_subprotocols : Array(String), request : HTTP::Request, remote_address : Socket::Address | Nil) : Bool #

Authenticate the incoming connection. Return true if it should be accepted, false otherwise. This is meant to be overridden by the derivative class. The WebSocket isn't opened until after this method returns, so it's not possible to send to the client in this method.

path is a String containing the requested path, without any query parameters.

params are a `URI::Params containing the query parmaeters, which can be used to send additional data.

requested_subprotocols is an Array of String containing the names of the requested subprotocols. If your code accepts one, set self.subprotocols to the name of the accepted subprotocol, and this will be communicated to the client in the Sec-Websocket-Protocol header. If you don't implement subprotocols, # it's not necessary to set self.subprotocols.

request is the HTTP::Request, including the headers sent with the Upgrade request which requests the change to WebSocket.

remote_address is the remote address of the client, or nil if that can't be deterimend.


[View source]
def internal_close(code : HTTP::WebSocket::CloseCode, message : String) #

Handle closure. The superclass method calls #on_close.


[View source]
def internal_connect(s : HTTP::WebSocket) #

This sets up the connection.


[View source]
def internal_on_pong(message : String) #

This is called when a pong is received. It remembers what time it was received, and this is saved in @last_pong_received. The ping fiber uses that value to time out an unresponding connection. This method then calls the superclass method, which calls #on_pong to notify the user.


[View source]
def last_pong_received : Time #

[View source]
def on_connect #

This is called when your WebSocket is connected. You may now send to the peer. WS_Client doesn't have this method, because the constructor doesn't return until the connection is valid. In WS_Service, #authenticate is called before the connection is made, to decide whether to make it or not. Then #connect is called when the connection is actually valid.


[View source]
def subprotocol : String | Nil #

[View source]
def subprotocol=(subprotocol : String | Nil) #

[View source]