Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for Websockets #1004

Merged
merged 12 commits into from
Feb 13, 2020
22 changes: 17 additions & 5 deletions Sources/ApolloWebSocket/WebSocketTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class WebSocketTransport {
public static var provider: ApolloWebSocketClient.Type = ApolloWebSocket.self
public weak var delegate: WebSocketTransportDelegate?

let reconnect: Atomic<Bool> = Atomic(false)
let reconnect: Atomic<Bool>
var websocket: ApolloWebSocketClient
let error: Atomic<Error?> = Atomic(nil)
let serializationFormat = JSONSerializationFormat.self
Expand All @@ -44,6 +44,7 @@ public class WebSocketTransport {

private let sendOperationIdentifiers: Bool
private let reconnectionInterval: TimeInterval
private let allowSendingDuplicates: Bool
fileprivate let sequenceNumberCounter = Atomic<Int>(0)
fileprivate var reconnected = false

Expand All @@ -67,19 +68,25 @@ public class WebSocketTransport {
/// - Parameter clientName: The client name to use for this client. Defaults to `Self.defaultClientName`
/// - Parameter clientVersion: The client version to use for this client. Defaults to `Self.defaultClientVersion`.
/// - Parameter sendOperationIdentifiers: Whether or not to send operation identifiers with operations. Defaults to false.
/// - Paremeter reconnect: Wether to auto reconnect when websocket looses connection. Defaults to true.
/// - Parameter reconnectionInterval: How long to wait before attempting to reconnect. Defaults to half a second.
/// - Parameter allowSendingDuplicates: Allow sending duplicate messages. Important when reconnected. Defaults to true.
/// - Parameter connectingPayload: [optional] The payload to send on connection. Defaults to an empty `GraphQLMap`.
/// - Parameter requestCreator: The request creator to use when serializing requests. Defaults to an `ApolloRequestCreator`.
public init(request: URLRequest,
clientName: String = WebSocketTransport.defaultClientName,
clientVersion: String = WebSocketTransport.defaultClientVersion,
sendOperationIdentifiers: Bool = false,
reconnect: Bool = true,
reconnectionInterval: TimeInterval = 0.5,
allowSendingDuplicates: Bool = true,
connectingPayload: GraphQLMap? = [:],
requestCreator: RequestCreator = ApolloRequestCreator()) {
self.connectingPayload = connectingPayload
self.sendOperationIdentifiers = sendOperationIdentifiers
self.reconnect = Atomic(reconnect)
self.reconnectionInterval = reconnectionInterval
self.allowSendingDuplicates = allowSendingDuplicates
self.requestCreator = requestCreator
self.websocket = WebSocketTransport.provider.init(request: request, protocols: protocols)
self.clientName = clientName
Expand Down Expand Up @@ -176,16 +183,15 @@ public class WebSocketTransport {
let queue = self.queue.sorted(by: { $0.0 < $1.0 })
self.queue.removeAll()
for (id, msg) in queue {
self.write(msg,id: id)
self.write(msg, id: id)
}
}

private func processMessage(socket: WebSocketClient, data: Data) {
print("WebSocketTransport::unprocessed event \(data)")
}

public func initServer(reconnect: Bool = true) {
self.reconnect.value = reconnect
public func initServer() {
self.acked = false

if let str = OperationMessage(payload: self.connectingPayload, type: .connectionInit).rawMessage {
Expand Down Expand Up @@ -297,7 +303,13 @@ extension WebSocketTransport: WebSocketDelegate {
// re-send the subscriptions whenever we are re-connected
// for the first connect, any subscriptions are already in queue
for (_,msg) in self.subscriptions {
write(msg)
if allowSendingDuplicates {
write(msg)
} else {
// search duplicate message from the queue
let id = queue.first { $0.value == msg }?.key
write(msg, id: id)
designatednerd marked this conversation as resolved.
Show resolved Hide resolved
}
}
} else {
self.delegate?.webSocketTransportDidConnect(self)
Expand Down