Skip to content
leeway edited this page Nov 19, 2021 · 1 revision

WebSocket

open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelegate 

Inheritance

NSObject, StreamDelegate, WebSocketClient, WSStreamDelegate

Initializers

init(request:protocols:stream:)

Used for setting protocols.

public init(request: URLRequest, protocols: [String]? = nil, stream: WSStream = FoundationStream()) 

init(url:protocols:)

public convenience init(url: URL, protocols: [String]? = nil) 

init(url:writeQueueQOS:protocols:)

public convenience init(url: URL, writeQueueQOS: QualityOfService, protocols: [String]? = nil) 

Properties

ErrorDomain

public static let ErrorDomain = "WebSocket"

callbackQueue

public var callbackQueue = DispatchQueue.main

delegate

Responds to callback about new messages coming in over the WebSocket and also connection/disconnect messages.

public weak var delegate: WebSocketDelegate?

advancedDelegate

The optional advanced delegate can be used instead of of the delegate

public weak var advancedDelegate: WebSocketAdvancedDelegate?

pongDelegate

Receives a callback for each pong message recived.

public weak var pongDelegate: WebSocketPongDelegate?

onConnect

public var onConnect: (() -> Void)?

onDisconnect

public var onDisconnect: ((Error?) -> Void)?

onText

public var onText: ((String) -> Void)?

onData

public var onData: ((Data) -> Void)?

onPong

public var onPong: ((Data?) -> Void)?

onHttpResponseHeaders

public var onHttpResponseHeaders: (([String: String]) -> Void)?

disableSSLCertValidation

public var disableSSLCertValidation = false

overrideTrustHostname

public var overrideTrustHostname = false

desiredTrustHostname

public var desiredTrustHostname: String? = nil

sslClientCertificate

public var sslClientCertificate: SSLClientCertificate? = nil

enableCompression

public var enableCompression = true

security

!((os(Linux)))
public var security: SSLTrustValidator?

enabledSSLCipherSuites

!((os(Linux)))
public var enabledSSLCipherSuites: [SSLCipherSuite]?

isConnected

public var isConnected: Bool 

request

public var request: URLRequest 

currentURL

public var currentURL: URL 

respondToPingWithPong

public var respondToPingWithPong: Bool = true

Methods

connect()

Connect to the WebSocket server on a background thread.

open func connect() 

disconnect(forceTimeout:closeCode:)

Disconnect from the server. I send a Close control frame to the server, then expect the server to respond with a Close control frame and close the socket from its end. I notify my delegate once the socket has been closed.

open func disconnect(forceTimeout: TimeInterval? = nil, closeCode: UInt16 = CloseCode.normal.rawValue) 

If you supply a non-nil forceTimeout, I wait at most that long (in seconds) for the server to close the socket. After the timeout expires, I close the socket and notify my delegate.

If you supply a zero (or negative) forceTimeout, I immediately close the socket (without sending a Close control frame) and notify my delegate.

Parameters

  • forceTimeout: Maximum time to wait for the server to close the socket.
  • closeCode: The code to send on disconnect. The default is the normal close code for cleanly disconnecting a webSocket.

write(string:completion:)

Write a string to the websocket. This sends it as a text frame.

open func write(string: String, completion: (() -> ())? = nil) 

If you supply a non-nil completion block, I will perform it when the write completes.

Parameters

  • string: The string to write.
  • completion: The (optional) completion handler.

write(data:completion:)

Write binary data to the websocket. This sends it as a binary frame.

open func write(data: Data, completion: (() -> ())? = nil) 

If you supply a non-nil completion block, I will perform it when the write completes.

Parameters

  • data: The data to write.
  • completion: The (optional) completion handler.

write(ping:completion:)

Write a ping to the websocket. This sends it as a control frame. Yodel a sound to the planet. This sends it as an astroid. http:​//youtu.be/Eu5ZJELRiJ8?t=42s

open func write(ping: Data, completion: (() -> ())? = nil) 

write(pong:completion:)

Write a pong to the websocket. This sends it as a control frame. Respond to a Yodel.

open func write(pong: Data, completion: (() -> ())? = nil) 

newBytesInStream()

Delegate for the stream methods. Processes incoming bytes

public func newBytesInStream() 

streamDidError(error:)

public func streamDidError(error: Error?) 
Types
Protocols
Global Variables
Clone this wiki locally