Add ability to track ping/pongs

This commit is contained in:
Erik Little 2017-10-14 10:15:36 -04:00
parent 7b04309990
commit c03fc96f04
No known key found for this signature in database
GPG Key ID: 4930B7C5FBC1A69D
5 changed files with 123 additions and 5 deletions

View File

@ -455,6 +455,28 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
joinNamespace(nsp)
}
/// Called when the engine receives a pong message.
open func engineDidReceivePong() {
handleQueue.async {
self._engineDidReceivePong()
}
}
private func _engineDidReceivePong() {
handleClientEvent(.gotPong, data: [])
}
/// Called when the sends a ping to the server.
open func engineDidSendPing() {
handleQueue.async {
self._engineDidSendPing()
}
}
private func _engineDidSendPing() {
handleClientEvent(.sentPing, data: [])
}
/// Called when socket.io has acked one of our emits. Causes the corresponding ack callback to be called.
///
/// - parameter ack: The number for this ack.

View File

@ -245,18 +245,80 @@ public enum SocketClientEvent : String {
/// ```
case connect
/// Called when the socket has disconnected and will not attempt to try to reconnect.
/// Emitted when the socket has disconnected and will not attempt to try to reconnect.
///
/// Usage:
///
/// ```swift
/// socket.on(clientEvent: .disconnect) {data, ack in
/// // Some cleanup logic
/// }
/// ```
case disconnect
/// Called when an error occurs.
/// Emitted when an error occurs.
///
/// Usage:
///
/// ```swift
/// socket.on(clientEvent: .error) {data, ack in
/// // Some logging
/// }
/// ```
case error
/// Called when the client begins the reconnection process.
/// Emitted whenever the engine gets a pong.
///
/// Usage:
///
/// ```swift
/// socket.on(clientEvent: .gotPong) {_, _ in
/// // Maybe keep track of latency?
/// }
/// ```
case gotPong
/// Emitted when the client begins the reconnection process.
///
/// Usage:
///
/// ```swift
/// socket.on(clientEvent: .reconnect) {data, ack in
/// // Some reconnect event logic
/// }
/// ```
case reconnect
/// Called each time the client tries to reconnect to the server.
/// Emitted each time the client tries to reconnect to the server.
///
/// Usage:
///
/// ```swift
/// socket.on(clientEvent: .reconnectAttempt) {data, ack in
/// // Some reconnect attempt logging
/// }
/// ```
case reconnectAttempt
/// Called every time there is a change in the client's status.
/// Emitted whenever the engine sends a ping.
///
/// Usage:
///
/// ```swift
/// socket.on(clientEvent: .sentPing) {_, _ in
/// // Maybe keep track of latency?
/// }
/// ```
case sentPing
/// Emitted every time there is a change in the client's status.
///
/// Usage:
///
/// ```swift
/// socket.on(clientEvent: .statusChange) {data, ack in
/// // Some status changing logging
/// }
/// ```
case statusChange
}

View File

@ -485,6 +485,8 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
if message == "3probe" {
upgradeTransport()
}
client?.engineDidReceivePong()
}
/// Parses raw binary received from engine.io.
@ -566,6 +568,8 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
engineQueue.asyncAfter(deadline: DispatchTime.now() + .milliseconds(pingInterval)) {[weak self] in
self?.sendPing()
}
client?.engineDidSendPing()
}
// Moves from long-polling to websockets

View File

@ -44,6 +44,12 @@ import Foundation
/// - parameter reason: The reason the engine opened.
func engineDidOpen(reason: String)
/// Called when the engine receives a pong message.
func engineDidReceivePong()
/// Called when the sends a ping to the server.
func engineDidSendPing()
/// Called when the engine has a message that must be parsed.
///
/// - parameter msg: The message that needs parsing.

View File

@ -412,6 +412,30 @@ class SocketSideEffectTest: XCTestCase {
XCTAssertEqual(socket.nsp, "/", "It should set the namespace after creation")
}
func testClientCallsSentPingHandler() {
let expect = expectation(description: "The client should emit a sentPing event")
socket.on(clientEvent: .sentPing) {data, ack in
expect.fulfill()
}
socket.engineDidSendPing()
waitForExpectations(timeout: 0.2)
}
func testClientCallsGotPongHandler() {
let expect = expectation(description: "The client should emit a gotPong event")
socket.on(clientEvent: .gotPong) {data, ack in
expect.fulfill()
}
socket.engineDidReceivePong()
waitForExpectations(timeout: 0.2)
}
let data = "test".data(using: String.Encoding.utf8)!
let data2 = "test2".data(using: String.Encoding.utf8)!
private var socket: SocketIOClient!