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

Expose Pong Control frames #405

Merged
merged 1 commit into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 20 additions & 2 deletions Sources/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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)
Expand Down