app.d 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import std;
  2. // Docs: https://trikko.github.io/serverino/
  3. // Tips and tricks: https://github.com/trikko/serverino/wiki/
  4. // Examples: https://github.com/trikko/serverino/tree/master/examples
  5. import serverino;
  6. mixin ServerinoMain;
  7. @onServerInit ServerinoConfig setup(){
  8. ServerinoConfig sc = ServerinoConfig.create(); // Config with default params
  9. //writeln("ServerinoConfig sc = ", sc);
  10. // ServerinoConfig sc = ServerinoConfig(
  11. // DaemonConfig(all, 10485760, 10 secs, 3 secs, 0, 5, 2048, false, true, false, []),
  12. // WorkerConfig(5 secs, 10 secs, 6 hours, 1 hour, 1 minute, true, "", "", false), [], 0, false)
  13. sc.addListener("127.0.0.1", 8081);
  14. //writeln("ServerinoConfig sc = ", sc);
  15. //ServerinoConfig sc = ServerinoConfig(
  16. // DaemonConfig(all, 10485760, 10 secs, 3 secs, 0, 5, 2048, false, true, false, [serverino.config.Listener]),
  17. // WorkerConfig(5 secs, 10 secs, 6 hours, 1 hour, 1 minute, true, "", "", false), [], 0, false)
  18. return sc;
  19. }
  20. // Accept a new connection only if the request path is "/echo"
  21. @onWebSocketUpgrade bool onUpgrade(Request req){
  22. return req.path == "/echo";
  23. }
  24. // Handle the WebSocket connection
  25. @endpoint void echo(Request r, WebSocket ws){
  26. // Read messages from the client
  27. while(true){
  28. // Only if the message is valid
  29. if( WebSocketMessage msg = ws.receiveMessage() ){
  30. // Send the message back to the client
  31. ws.send("I received your message: `" ~ msg.asString ~ "`");
  32. }
  33. }
  34. }
  35. @route!"/"
  36. @endpoint void index(Request r, Output o){ o.serveFile("static/index.html"); }
  37. @route!"/style.css"
  38. @endpoint void css(Request r, Output o){ o.serveFile("static/style.css"); }