Add ability to track ping/pongs
This commit is contained in:
parent
7b04309990
commit
c03fc96f04
@ -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.
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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!
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user