1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import std;
- // Docs: https://trikko.github.io/serverino/
- // Tips and tricks: https://github.com/trikko/serverino/wiki/
- // Examples: https://github.com/trikko/serverino/tree/master/examples
- import serverino;
- mixin ServerinoMain;
- @onServerInit ServerinoConfig setup(){
- ServerinoConfig sc = ServerinoConfig.create(); // Config with default params
- //writeln("ServerinoConfig sc = ", sc);
- // ServerinoConfig sc = ServerinoConfig(
- // DaemonConfig(all, 10485760, 10 secs, 3 secs, 0, 5, 2048, false, true, false, []),
- // WorkerConfig(5 secs, 10 secs, 6 hours, 1 hour, 1 minute, true, "", "", false), [], 0, false)
- sc.addListener("127.0.0.1", 8081);
- //writeln("ServerinoConfig sc = ", sc);
- //ServerinoConfig sc = ServerinoConfig(
- // DaemonConfig(all, 10485760, 10 secs, 3 secs, 0, 5, 2048, false, true, false, [serverino.config.Listener]),
- // WorkerConfig(5 secs, 10 secs, 6 hours, 1 hour, 1 minute, true, "", "", false), [], 0, false)
- return sc;
- }
- // Accept a new connection only if the request path is "/echo"
- @onWebSocketUpgrade bool onUpgrade(Request req){
- return req.path == "/echo";
- }
- // Handle the WebSocket connection
- @endpoint void echo(Request r, WebSocket ws){
- // Read messages from the client
- while(true){
- // Only if the message is valid
- if( WebSocketMessage msg = ws.receiveMessage() ){
- // Send the message back to the client
- ws.send("I received your message: `" ~ msg.asString ~ "`");
- }
- }
- }
- @route!"/"
- @endpoint void index(Request r, Output o){ o.serveFile("static/index.html"); }
- @route!"/style.css"
- @endpoint void css(Request r, Output o){ o.serveFile("static/style.css"); }
|