WebSocketTransport
CLASS
WebSocketTransport
1public class WebSocketTransportA network transport that uses web sockets requests to send GraphQL subscription operations to a server.
Properties
delegate
1public weak var delegate: WebSocketTransportDelegate?clientName
1public var clientName: StringNOTE: Setting this won't override immediately if the socket is still connected, only on reconnection.
clientVersion
1public var clientVersion: StringNOTE: Setting this won't override immediately if the socket is still connected, only on reconnection.
Methods
init(websocket:store:clientName:clientVersion:sendOperationIdentifiers:reconnect:reconnectionInterval:allowSendingDuplicates:connectOnInit:connectingPayload:requestBodyCreator:operationMessageIdCreator:)
1public init(websocket: WebSocketClient,
2 store: ApolloStore? = nil,
3 clientName: String = WebSocketTransport.defaultClientName,
4 clientVersion: String = WebSocketTransport.defaultClientVersion,
5 sendOperationIdentifiers: Bool = false,
6 reconnect: Bool = true,
7 reconnectionInterval: TimeInterval = 0.5,
8 allowSendingDuplicates: Bool = true,
9 connectOnInit: Bool = true,
10 connectingPayload: GraphQLMap? = [:],
11 requestBodyCreator: RequestBodyCreator = ApolloRequestBodyCreator(),
12 operationMessageIdCreator: OperationMessageIdCreator = ApolloSequencedOperationMessageIdCreator())Designated initializer
Parameters:
websocket: The websocket client to use for creating a websocket connection.
store: [optional] The
ApolloStoreused as a local cache. Defaults tonil.clientName: The client name to use for this client. Defaults to
Self.defaultClientNameclientVersion: The client version to use for this client. Defaults to
Self.defaultClientVersion.sendOperationIdentifiers: Whether or not to send operation identifiers with operations. Defaults to false.
reconnect: Whether to auto reconnect when websocket looses connection. Defaults to true.
reconnectionInterval: How long to wait before attempting to reconnect. Defaults to half a second.
allowSendingDuplicates: Allow sending duplicate messages. Important when reconnected. Defaults to true.
connectOnInit: Whether the websocket connects immediately on creation. If false, remember to call
resumeWebSocketConnection()to connect. Defaults to true.connectingPayload: [optional] The payload to send on connection. Defaults to an empty
GraphQLMap.requestBodyCreator: The
RequestBodyCreatorto use when serializing requests. Defaults to anApolloRequestBodyCreator.operationMessageIdCreator: The
OperationMessageIdCreatorused to generate a unique message identifier per request. Defaults toApolloSequencedOperationMessageIdCreator.
Parameters
| Name | Description |
|---|---|
| websocket | The websocket client to use for creating a websocket connection. |
| store | [optional] The ApolloStore used as a local cache. Defaults to nil. |
| clientName | The client name to use for this client. Defaults to Self.defaultClientName |
| clientVersion | The client version to use for this client. Defaults to Self.defaultClientVersion. |
| sendOperationIdentifiers | Whether or not to send operation identifiers with operations. Defaults to false. |
| reconnect | Whether to auto reconnect when websocket looses connection. Defaults to true. |
| reconnectionInterval | How long to wait before attempting to reconnect. Defaults to half a second. |
| allowSendingDuplicates | Allow sending duplicate messages. Important when reconnected. Defaults to true. |
| connectOnInit | Whether the websocket connects immediately on creation. If false, remember to call resumeWebSocketConnection() to connect. Defaults to true. |
| connectingPayload | [optional] The payload to send on connection. Defaults to an empty GraphQLMap. |
| requestBodyCreator | The RequestBodyCreator to use when serializing requests. Defaults to an ApolloRequestBodyCreator. |
| operationMessageIdCreator | The OperationMessageIdCreator used to generate a unique message identifier per request. Defaults to ApolloSequencedOperationMessageIdCreator. |
isConnected()
1public func isConnected() -> Boolping(data:completionHandler:)
1public func ping(data: Data, completionHandler: (() -> Void)? = nil)initServer()
1public func initServer()closeConnection()
1public func closeConnection()deinit
1deinitunsubscribe(_:)
1public func unsubscribe(_ subscriptionId: String)updateHeaderValues(_:reconnectIfConnected:)
1public func updateHeaderValues(_ values: [String: String?], reconnectIfConnected: Bool = true)updateConnectingPayload(_:reconnectIfConnected:)
1public func updateConnectingPayload(_ payload: GraphQLMap, reconnectIfConnected: Bool = true)pauseWebSocketConnection()
1public func pauseWebSocketConnection()Disconnects the websocket while setting the auto-reconnect value to false,
allowing purposeful disconnects that do not dump existing subscriptions.
NOTE: You will receive an error on the subscription (should be a WebSocket.WSError with code 1000) when the socket disconnects.
ALSO NOTE: To reconnect after calling this, you will need to call resumeWebSocketConnection.
resumeWebSocketConnection(autoReconnect:)
1public func resumeWebSocketConnection(autoReconnect: Bool = true)Reconnects a paused web socket.
Parameter autoReconnect:
trueif you want the websocket to automatically reconnect if the connection drops. Defaults to true.
Parameters
| Name | Description |
|---|---|
| autoReconnect | true if you want the websocket to automatically reconnect if the connection drops. Defaults to true. |