WebSocket

public final class WebSocket : BasicWorker

Represents a client connected via WebSocket protocol. Use this to receive text/data frames and send responses.

 ws.onText { ws, string in
    ws.send(string.reversed())
 }
  • Adds a callback to this WebSocket to receive text-formatted messages.

    ws.onText { ws, string in
        ws.send(string.reversed())
    }
    

    Use onBinary(_:) to handle binary-formatted messages.

    Declaration

    Swift

    public func onText(_ callback: @escaping (WebSocket, String) -> ())

    Parameters

    callback

    Closure to accept incoming text-formatted data. This will be called every time the connected client sends text.

  • Adds a callback to this WebSocket to receive binary-formatted messages.

    ws.onBinary { ws, data in
        print(data)
    }
    

    Use onText(_:) to handle text-formatted messages.

    Declaration

    Swift

    public func onBinary(_ callback: @escaping (WebSocket, Data) -> ())

    Parameters

    callback

    Closure to accept incoming binary-formatted data. This will be called every time the connected client sends binary-data.

  • Adds a callback to this WebSocket to handle errors.

    ws.onError { ws, error in
        print(error)
    }
    

    Declaration

    Swift

    public func onError(_ callback: @escaping (WebSocket, Error) -> ())

    Parameters

    callback

    Closure to handle error’s caught during this connection.

  • Adds a callback to this WebSocket to handle incoming close codes.

    ws.onCloseCode { closeCode in
        print(closeCode)
    }
    

    Declaration

    Swift

    public func onCloseCode(_ callback: @escaping (WebSocketErrorCode) -> ())

    Parameters

    callback

    Closure to handle received close codes.

  • Sends text-formatted data to the connected client.

    ws.onText { ws, string in
        ws.send(string.reversed())
    }
    

    Declaration

    Swift

    public func send<S>(_ text: S, promise: Promise<Void>? = nil) where S: Collection, S.Element == Character

    Parameters

    text

    String to send as text-formatted data to the client.

    promise

    Optional Promise to complete when the send is finished.

  • Sends binary-formatted data to the connected client.

    ws.onText { ws, string in
        ws.send([0x68, 0x69])
    }
    

    Declaration

    Swift

    public func send(_ binary: Data, promise: Promise<Void>? = nil)

    Parameters

    text

    Data to send as binary-formatted data to the client.

    promise

    Optional Promise to complete when the send is finished.

  • Sends text-formatted data to the connected client.

    ws.onText { ws, string in
        ws.send(text: string.reversed())
    }
    

    Declaration

    Swift

    public func send(text: LosslessDataConvertible, promise: Promise<Void>? = nil)

    Parameters

    text

    LosslessDataConvertible to send as text-formatted data to the client.

    promise

    Optional Promise to complete when the send is finished.

  • Sends binary-formatted data to the connected client.

    ws.onText { ws, string in
        ws.send(binary: [0x68, 0x69])
    }
    

    Declaration

    Swift

    public func send(binary: LosslessDataConvertible, promise: Promise<Void>? = nil)

    Parameters

    data

    LosslessDataConvertible to send as binary-formatted data to the client.

    promise

    Optional Promise to complete when the send is finished.

  • Sends raw-data to the connected client using the supplied WebSocket opcode.

    // client will receive "Hello, world!" as one message
    ws.send(raw: "Hello, ", opcode: .text, fin: false)
    ws.send(raw: "world", opcode: .continuation, fin: false)
    ws.send(raw: "!", opcode: .continuation)
    

    Declaration

    Swift

    public func send(raw data: LosslessDataConvertible, opcode: WebSocketOpcode, fin: Bool = true, promise: Promise<Void>? = nil)

    Parameters

    data

    LosslessDataConvertible to send to the client.

    opcode

    WebSocketOpcode indicating data format. Usually .text or .binary.

    fine

    If false, additional .continuation frames are expected.

    promise

    Optional Promise to complete when the send is finished.

  • true if the WebSocket has been closed.

    Declaration

    Swift

    public internal(set) var isClosed: Bool
  • A Future that will be completed when the WebSocket closes.

    Declaration

    Swift

    public var onClose: Future<Void> { get }
  • Closes the WebSocket‘s connection, disconnecting the client.

    Declaration

    Swift

    public func close(code: WebSocketErrorCode? = nil)

    Parameters

    code

    Optional WebSocketCloseCode to send before closing the connection. If a code is provided, the WebSocket will wait until an acknowledgment is received from the server before actually closing the connection.