Configure first WebSocket test
This commit is contained in:
7
Sources/App/configure.swift
Executable file
7
Sources/App/configure.swift
Executable file
@ -0,0 +1,7 @@
|
||||
import Vapor
|
||||
|
||||
// configures your application
|
||||
public func configure(_ app: Application) throws {
|
||||
app.http.server.configuration.port = 10000
|
||||
try routes(app)
|
||||
}
|
31
Sources/App/routes.swift
Executable file
31
Sources/App/routes.swift
Executable file
@ -0,0 +1,31 @@
|
||||
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")
|
||||
}
|
||||
}
|
9
Sources/Run/main.swift
Normal file
9
Sources/Run/main.swift
Normal file
@ -0,0 +1,9 @@
|
||||
import App
|
||||
import Vapor
|
||||
|
||||
var env = try Environment.detect()
|
||||
try LoggingSystem.bootstrap(from: &env)
|
||||
let app = Application(env)
|
||||
defer { app.shutdown() }
|
||||
try configure(app)
|
||||
try app.run()
|
Reference in New Issue
Block a user