32 lines
789 B
Swift
Executable File
32 lines
789 B
Swift
Executable File
import Vapor
|
|
|
|
var connection: WebSocket?
|
|
|
|
func routes(_ app: Application) throws {
|
|
|
|
app.get { req in
|
|
return "It works!"
|
|
}
|
|
|
|
/**
|
|
Start a new websocket connection for the client to receive table updates from the server
|
|
- Returns: Nothing
|
|
- Note: The first (and only) message from the client over the connection must be a valid session token.
|
|
*/
|
|
app.webSocket("listen") { req, socket in
|
|
socket.onBinary { socket, data in
|
|
print("\(data)")
|
|
}
|
|
socket.onText { socket, text in
|
|
print(text)
|
|
}
|
|
|
|
_ = socket.onClose.always { result in
|
|
connection = nil
|
|
print("Socket closed")
|
|
}
|
|
connection = socket
|
|
print("Socket connected")
|
|
}
|
|
}
|