NWWebSocket
open class NWWebSocket : WebSocketConnection
A WebSocket client that manages a socket connection.
-
The WebSocket connection delegate.
Declaration
Swift
public weak var delegate: WebSocketConnectionDelegate? -
The default
NWProtocolWebSocket.Optionsfor a WebSocket connection.These options specify that the connection automatically replies to Ping messages instead of delivering them to the
receiveMessage(data:context:)method.Declaration
Swift
public static var defaultOptions: NWProtocolWebSocket.Options { get }
-
Creates a
NWWebSocketinstance which connects to a socketurlwith some configurationoptions.Declaration
Swift
public convenience init(request: URLRequest, connectAutomatically: Bool = false, options: NWProtocolWebSocket.Options = NWWebSocket.defaultOptions, connectionQueue: DispatchQueue = .main)Parameters
requestThe
URLRequestcontaining the connection endpointURL.connectAutomaticallyDetermines if a connection should occur automatically on initialization. The default value is
false.optionsThe configuration options for the connection. The default value is
NWWebSocket.defaultOptions.connectionQueueA
DispatchQueueon which to deliver all connection events. The default value is.main. -
Creates a
NWWebSocketinstance which connects a socketurlwith some configurationoptions.Declaration
Swift
public init(url: URL, connectAutomatically: Bool = false, options: NWProtocolWebSocket.Options = NWWebSocket.defaultOptions, connectionQueue: DispatchQueue = .main)Parameters
urlThe connection endpoint
URL.connectAutomaticallyDetermines if a connection should occur automatically on initialization. The default value is
false.optionsThe configuration options for the connection. The default value is
NWWebSocket.defaultOptions.connectionQueueA
DispatchQueueon which to deliver all connection events. The default value is.main.
-
Connect to the WebSocket.
Declaration
Swift
open func connect() -
Send a UTF-8 formatted
Stringover the WebSocket.Declaration
Swift
open func send(string: String)Parameters
stringThe
Stringthat will be sent. -
Send some
Dataover the WebSocket.Declaration
Swift
open func send(data: Data)Parameters
dataThe
Datathat will be sent. -
Start listening for messages over the WebSocket.
Declaration
Swift
public func listen() -
Ping the WebSocket periodically.
Declaration
Swift
open func ping(interval: TimeInterval)Parameters
intervalThe
TimeInterval(in seconds) with which to ping the server. -
Ping the WebSocket once.
Declaration
Swift
open func ping() -
Disconnect from the WebSocket.
Declaration
Swift
open func disconnect(closeCode: NWProtocolWebSocket.CloseCode = .protocolCode(.normalClosure))Parameters
closeCodeThe code to use when closing the WebSocket connection.
View on GitHub
NWWebSocket Class Reference