Skip to content

Commit

Permalink
Merge pull request #405 from hishnash/sendPong
Browse files Browse the repository at this point in the history
Expose Pong Control frames
  • Loading branch information
daltoniam authored Nov 6, 2017
2 parents ca20b02 + f057a8b commit 97e378d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
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 @@ -70,6 +70,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 @@ -85,6 +86,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 @@ -396,6 +401,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 @@ -534,6 +541,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 @@ -1125,8 +1141,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

0 comments on commit 97e378d

Please sign in to comment.