WebSocketService

public protocol WebSocketService : AnyObject

The WebSocketService protocol is implemented by classes that wish to be WebSocket server side end points. An instance of the WebSocketService protocol is the server side of a WebSocket connection. There can be many WebSocket connections connected to a single WebSocketService protocol instance. The protocol is a set of callbacks that are invoked when various events occur.

  • Called when a WebSocket client connects to the server and is connected to a specific WebSocketService.

    Declaration

    Swift

    func connected(connection: WebSocketConnection)

    Parameters

    connection

    The WebSocketConnection object that represents the client’s connection to this WebSocketService

  • Called when a WebSocket client disconnects from the server.

    Declaration

    Swift

    func disconnected(connection: WebSocketConnection, reason: WebSocketCloseReasonCode)

    Parameters

    connection

    The WebSocketConnection object that represents the connection that was disconnected from this WebSocketService.

    reason

    The WebSocketCloseReasonCode that describes why the client disconnected.

  • Called when a WebSocket client sent a binary message to the server to this WebSocketService.

    Declaration

    Swift

    func received(message: Data, from: WebSocketConnection)

    Parameters

    message

    A Data struct containing the bytes of the binary message sent by the client.

    client

    The WebSocketConnection object that represents the connection over which the client sent the message to this WebSocketService

  • Called when a WebSocket client sent a text message to the server to this WebSocketService.

    Declaration

    Swift

    func received(message: String, from: WebSocketConnection)

    Parameters

    message

    A String containing the text message sent by the client.

    client

    The WebSocketConnection object that represents the connection over which the client sent the message to this WebSocketService

  • connectionTimeout Default implementation

    The time in seconds that a connection must be unresponsive to be automatically closed by the server. If the WebSocket server has not received any messages in the first half of the timeout time it will ping the connection. If a pong is not received in the remaining half of the timeout, the connection will be closed with a 1006 (connection closed abnormally) status code. The connectionTimeout defaults to nil, meaning no connection cleanup will take place.

    Default Implementation

    Default computed value for connectionTimeout that returns nil.

    Declaration

    Swift

    var connectionTimeout: Int? { get }