Skip to content

Commit 13b31b6

Browse files
Merge pull request #299 from allenhuang/add_pong_closure_to_websocket
Add pong closure to websocket
2 parents f7fda97 + 7a40b0a commit 13b31b6

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Sources/DemoServer.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ public func demoServer(_ publicDir: String) -> HttpServer {
180180
session.writeText(text)
181181
}, { (session, binary) in
182182
session.writeBinary(binary)
183-
})
183+
}, { (session, pong) in
184+
// Got a pong frame
185+
}
186+
)
184187

185188
server.notFoundHandler = { r in
186189
return .movedPermanently("https://github.com/404")

Sources/WebSockets.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import Foundation
1010

1111
public func websocket(
1212
_ text: ((WebSocketSession, String) -> Void)?,
13-
_ binary: ((WebSocketSession, [UInt8]) -> Void)?) -> ((HttpRequest) -> HttpResponse) {
13+
_ binary: ((WebSocketSession, [UInt8]) -> Void)?,
14+
_ pong: ((WebSocketSession, [UInt8]) -> Void)?) -> ((HttpRequest) -> HttpResponse) {
1415
return { r in
1516
guard r.hasTokenForHeader("upgrade", token: "websocket") else {
1617
return .badRequest(.text("Invalid value of 'Upgrade' header: \(r.headers["upgrade"] ?? "unknown")"))
@@ -90,6 +91,9 @@ public func websocket(
9091
session.writeFrame(ArraySlice(frame.payload), .pong)
9192
}
9293
case .pong:
94+
if let handlePong = pong {
95+
handlePong(session, frame.payload)
96+
}
9397
break
9498
}
9599
}

0 commit comments

Comments
 (0)