|
|
@@ -7,11 +7,19 @@
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
+@available(*, deprecated, message: "Use websocket(text:binary:pong:connected:disconnected:) instead.")
|
|
|
+public func websocket(_ text: @escaping (WebSocketSession, String) -> Void,
|
|
|
+ _ binary: @escaping (WebSocketSession, [UInt8]) -> Void,
|
|
|
+ _ pong: @escaping (WebSocketSession, [UInt8]) -> Void) -> ((HttpRequest) -> HttpResponse) {
|
|
|
+ return websocket(text: text, binary: binary, pong: pong)
|
|
|
+}
|
|
|
|
|
|
public func websocket(
|
|
|
- _ text: ((WebSocketSession, String) -> Void)?,
|
|
|
- _ binary: ((WebSocketSession, [UInt8]) -> Void)?,
|
|
|
- _ pong: ((WebSocketSession, [UInt8]) -> Void)?) -> ((HttpRequest) -> HttpResponse) {
|
|
|
+ text: ((WebSocketSession, String) -> Void)? = nil,
|
|
|
+ binary: ((WebSocketSession, [UInt8]) -> Void)? = nil,
|
|
|
+ pong: ((WebSocketSession, [UInt8]) -> Void)? = nil,
|
|
|
+ connected: ((WebSocketSession) -> Void)? = nil,
|
|
|
+ disconnected: ((WebSocketSession) -> Void)? = nil) -> ((HttpRequest) -> HttpResponse) {
|
|
|
return { r in
|
|
|
guard r.hasTokenForHeader("upgrade", token: "websocket") else {
|
|
|
return .badRequest(.text("Invalid value of 'Upgrade' header: \(r.headers["upgrade"] ?? "unknown")"))
|
|
|
@@ -105,6 +113,8 @@ public func websocket(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ connected?(session)
|
|
|
+
|
|
|
do {
|
|
|
try read()
|
|
|
} catch let error {
|
|
|
@@ -126,6 +136,8 @@ public func websocket(
|
|
|
// If an error occurs, send the close handshake.
|
|
|
session.writeCloseFrame()
|
|
|
}
|
|
|
+
|
|
|
+ disconnected?(session)
|
|
|
}
|
|
|
guard let secWebSocketAccept = String.toBase64((secWebSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").sha1()) else {
|
|
|
return HttpResponse.internalServerError
|