app.d 910 B

123456789101112131415161718192021222324252627282930313233
  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. // Accept a new connection only if the request path is "/echo"
  8. @onWebSocketUpgrade bool onUpgrade(Request req){
  9. return req.path == "/echo";
  10. }
  11. // Handle the WebSocket connection
  12. @endpoint void echo(Request r, WebSocket ws){
  13. // Read messages from the client
  14. while(true){
  15. // Only if the message is valid
  16. if( WebSocketMessage msg = ws.receiveMessage() ){
  17. // Send the message back to the client
  18. ws.send("I received your message: `" ~ msg.asString ~ "`");
  19. }
  20. }
  21. }
  22. @route!"/"
  23. @endpoint void index(Request r, Output o){ o.serveFile("static/index.html"); }
  24. @route!"/style.css"
  25. @endpoint void css(Request r, Output o){ o.serveFile("static/style.css"); }