WebSocketConnection

public class WebSocketConnection

Represents a specific WebSocket connection. Provides a unique identifier for the connection and APIs to send messages and control commands to the client at the other end of the connection.

  • id

    Unique identifier for this WebSocketConnection

    Declaration

    Swift

    public let id: String
  • The ServerRequest from the original protocol upgrade

    Declaration

    Swift

    public let request: ServerRequest
  • Close a WebSocket connection by sending a close control command to the client optionally with the specified reason code and description text.

    Declaration

    Swift

    public func close(reason: WebSocketCloseReasonCode? = nil, description: String? = nil)

    Parameters

    reason

    An optional reason code to send in the close control command describing why the connection was closed. If nil, a reason code of WebSocketCloseReasonCode.normal will be sent.

    description

    An optional text description to be sent in the close control frame.

  • Forcefully close a WebSocket connection by sending a close control command to the client optionally with the specified reason code and description text followed by closing the socket.

    Declaration

    Swift

    public func drop(reason: WebSocketCloseReasonCode? = nil, description: String? = nil)

    Parameters

    reason

    An optional reason code to send in the close control command describing why the connection was closed. If nil, a reason code of WebSocketCloseReasonCode.normal will be sent.

    description

    An optional text description to be sent in the close control frame.

  • Send a ping control frame to the client

    Declaration

    Swift

    public func ping(withMessage: String? = nil)

    Parameters

    withMessage

    An optional string to be included in the ping control frame.

  • Send a message to the client contained in a Data struct.

    Declaration

    Swift

    public func send(message: Data, asBinary: Bool = true)

    Parameters

    message

    A Data struct containing the bytes to be sent to the client as a message.

    asBinary

    If true, which is the default, the message is sent as a binary mesage. If false, the message will be sent as a text message.

  • Send a text message to the client

    Declaration

    Swift

    public func send(message: String)

    Parameters

    message

    A String containing the text to be sent to the client as a text message.