-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathDefaultWebSocket.swift
More file actions
70 lines (55 loc) · 2.38 KB
/
DefaultWebSocket.swift
File metadata and controls
70 lines (55 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import Starscream
import Foundation
/// Included default implementation of a `WebSocketClient`, based on `Starscream`'s `WebSocket`.
public class DefaultWebSocket: WebSocketClient, Starscream.WebSocketDelegate {
/// The websocket protocols supported by this websocket client implementation.
static private let wsProtocols = ["graphql-ws"]
/// The underlying `Starscream` websocket used by this websocket client.
private let underlyingWebsocket: Starscream.WebSocket
public var request: URLRequest {
get { underlyingWebsocket.request }
set { underlyingWebsocket.request = newValue }
}
public weak var delegate: WebSocketClientDelegate?
public var callbackQueue: DispatchQueue {
get { underlyingWebsocket.callbackQueue }
set { underlyingWebsocket.callbackQueue = newValue }
}
/// Required initializer
///
/// - Parameters:
/// - request: The URLRequest to use on connection.
/// - certPinner: [optional] The object providing information about certificate pinning. Should default to Starscream's `FoundationSecurity`.
/// - compressionHandler: [optional] The object helping with any compression handling. Should default to nil.
required public init(request: URLRequest) {
self.underlyingWebsocket = Starscream.WebSocket(
request: request,
protocols: Self.wsProtocols,
stream: FoundationStream())
self.underlyingWebsocket.delegate = self
}
public func connect() {
self.underlyingWebsocket.connect()
}
public func disconnect() {
self.underlyingWebsocket.disconnect()
}
public func write(ping: Data, completion: (() -> Void)?) {
self.underlyingWebsocket.write(ping: ping, completion: completion)
}
public func write(string: String) {
self.underlyingWebsocket.write(string: string)
}
public func websocketDidConnect(socket: Starscream.WebSocketClient) {
self.delegate?.websocketDidConnect(socket: self)
}
public func websocketDidDisconnect(socket: Starscream.WebSocketClient, error: Error?) {
self.delegate?.websocketDidDisconnect(socket: self, error: error)
}
public func websocketDidReceiveMessage(socket: Starscream.WebSocketClient, text: String) {
self.delegate?.websocketDidReceiveMessage(socket: self, text: text)
}
public func websocketDidReceiveData(socket: Starscream.WebSocketClient, data: Data) {
self.delegate?.websocketDidReceiveData(socket: self, data: data)
}
}