From f057a8b33d720a30652ffd7396a63cc78687f1ed Mon Sep 17 00:00:00 2001 From: Matthaus Woolard Date: Mon, 16 Oct 2017 08:43:15 +0300 Subject: [PATCH] Expose Pong Control frames * Let users send Pong frames * let users turn off automatic Ping responds with Pong. (only needed are edge cases) --- README.md | 19 +++++++++++++++++++ Sources/WebSocket.swift | 22 ++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 99c9b416..6c75a18b 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,25 @@ The writePing method is the same as write, but sends a ping control frame. socket.write(ping: Data()) //example on how to write a ping control frame over the socket! ``` +### write a pong frame + + +the writePong method is the same as writePing, but sends a pong control frame. + +```swift +socket.write(pong: Data()) //example on how to write a pong control frame over the socket! +``` + +Starscream will automatically respond to incoming `ping` control frames so you do not need to manually send `pong`s. + +However if for some reason you need to control this prosses you can turn off the automatic `ping` response by disabling `respondToPingWithPong`. + +```swift +socket.respondToPingWithPong = false //Do not automaticaly respond to incoming pings with pongs. +``` + +In most cases you will not need to do this. + ### disconnect The disconnect method does what you would expect and closes the socket. diff --git a/Sources/WebSocket.swift b/Sources/WebSocket.swift index 92bf57de..71414f3c 100644 --- a/Sources/WebSocket.swift +++ b/Sources/WebSocket.swift @@ -68,6 +68,7 @@ public protocol WebSocketClient: class { func write(string: String, completion: (() -> ())?) func write(data: Data, completion: (() -> ())?) func write(ping: Data, completion: (() -> ())?) + func write(pong: Data, completion: (() -> ())?) } //implements some of the base behaviors @@ -83,6 +84,10 @@ extension WebSocketClient { public func write(ping: Data) { write(ping: ping, completion: nil) } + + public func write(pong: Data) { + write(pong: pong, completion: nil) + } public func disconnect() { disconnect(forceTimeout: nil, closeCode: CloseCode.normal.rawValue) @@ -367,6 +372,8 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega public var currentURL: URL { return request.url! } + public var respondToPingWithPong: Bool = true + // MARK: - Private private struct CompressionState { @@ -505,6 +512,15 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega dequeueWrite(ping, code: .ping, writeCompletion: 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) { + guard isConnected else { return } + dequeueWrite(pong, code: .pong, writeCompletion: completion) + } + /** Private method that starts the connection. */ @@ -1088,8 +1104,10 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega private func processResponse(_ response: WSResponse) -> Bool { if response.isFin && response.bytesLeft <= 0 { if response.code == .ping { - let data = response.buffer! // local copy so it is perverse for writing - dequeueWrite(data as Data, code: .pong) + if respondToPingWithPong { + let data = response.buffer! // local copy so it is perverse for writing + dequeueWrite(data as Data, code: .pong) + } } else if response.code == .textFrame { guard let str = String(data: response.buffer! as Data, encoding: .utf8) else { writeError(CloseCode.encoding.rawValue)