abstract class WS_Service
- WS_Service
- WS_Protocol
- Reference
- Object
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.crConstructors
Class Method Summary
-
.graceful_shutdown(message : String = "Graceful shutdown")
This is meant to be called for each connection when the server is shutting down.
Instance Method Summary
-
#authenticate(path : String, params : URI::Params, requested_subprotocols : Array(String), request : HTTP::Request, remote_address : Socket::Address | Nil) : Bool
Authenticate the incoming connection.
-
#internal_close(code : HTTP::WebSocket::CloseCode, message : String)
Handle closure.
-
#internal_connect(s : HTTP::WebSocket)
This sets up the connection.
-
#internal_on_pong(message : String)
This is called when a pong is received.
- #last_pong_received : Time
-
#on_connect
This is called when your WebSocket is connected.
- #subprotocol : String | Nil
- #subprotocol=(subprotocol : String | Nil)
Constructor Detail
Class Method Detail
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.
Instance Method Detail
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.
Handle closure. The superclass method calls #on_close
.
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.
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.