ws_bert_login.d 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. alias uint8 = ubyte;
  2. alias uint32 = int;
  3. import vibe.core.core;
  4. import vibe.http.router;
  5. import vibe.http.server;
  6. import vibe.http.fileserver;
  7. import vibe.http.websockets;
  8. import vibe.core.log;
  9. import std.stdio;
  10. import std.string;
  11. import std.array;
  12. import std.conv : to;
  13. import bert; // https://github.com/221V/dlang_erl_bert https://git.4dev.win/game1/dlang_erl_bert
  14. import mustache;
  15. alias MustacheEngine!(string) Mustache;
  16. void ws_bert_handle(scope WebSocket sock){
  17. // simple echo server + :)
  18. while(sock.connected){
  19. //auto msg = sock.receiveText();
  20. //sock.send(msg ~ " :)");
  21. auto msg = sock.receiveBinary();
  22. if(msg == "1"){
  23. sock.send("0"); // ws PING - PONG
  24. }else{
  25. try{
  26. auto decoder = new BertDecoder( cast(ubyte[])msg.dup );
  27. auto decoded = decoder.decode();
  28. msg_match(decoded, sock);
  29. }catch(Exception e){
  30. writeln("Exception 37: ", e.msg);
  31. }
  32. }
  33. }
  34. }
  35. void msg_match(BertValue decoded, WebSocket sock){
  36. writeln("Decoded: ", decoded.toString());
  37. if(decoded.type_ == BertType.Tuple){
  38. auto decoded1 = decoded.tupleValue;
  39. if(decoded1.length == 3){
  40. if(auto num1 = cast(uint8)decoded1[0].intValue){
  41. writeln("num1 = ", num1, " ", typeof(num1).stringof); // ws.send(enc(tuple( number(1), number(42), number(777) ))); // Decoded: {1, 42, 777} // num1 = 1 ubyte
  42. sock.send("{console.log(" ~ to!string(num1 + 42) ~ ")}"); // got 43 in browser console
  43. } // else do nothing
  44. if(auto str1 = cast(string)decoded1[1].binaryValue){
  45. writeln("str1 = ", str1, " ", typeof(str1).stringof); // ws.send(enc(tuple( number(1), bin('blabla'), number(777) ))); // Decoded: {1, <<98,108,97,98,108,97>>, 777} // str1 = blabla string
  46. sock.send("{console.log('" ~ str1 ~ " la-la-la" ~ "')}"); // got 'blabla la-la-la' in browser console
  47. } // else do nothing
  48. if(decoded1[2].type_ == BertType.List){
  49. auto list1 = decoded1[2].listValue; // ws.send(enc(tuple( number(1), bin('blabla'), list( number(1), number(2), number(3) ) ))); // Decoded: {1, <<98,108,97,98,108,97>>, [1, 2, 3]}
  50. if(list1.length == 3){
  51. if(auto num31 = cast(uint32)list1[0].intValue){
  52. writeln("num31 = ", num31, " ", typeof(num31).stringof); // ws.send(enc(tuple( number(1), bin('9'), list( number(1), number(2), number(3) ) ))); // Decoded: {1, <<57>>, [1, 2, 3]} // num31 = 1 int
  53. sock.send("{console.log(" ~ to!string(num31 + 77) ~ ")}"); // got 78 in browser console
  54. } // else do nothing
  55. } // else do nothing
  56. } // else do nothing
  57. } // else do nothing
  58. } // else do nothing
  59. }
  60. void login_test(HTTPServerRequest req, HTTPServerResponse res){
  61. Mustache mustache;
  62. auto context = new Mustache.Context;
  63. mustache.path = "priv";
  64. mustache.ext = "dtl";
  65. context["lang"] = "en";
  66. context["number1"] = 42;
  67. context.useSection("maybe1");
  68. context["part1"] = "<span>777</span>";
  69. context["result1"] = "Hello, World!\n";
  70. res.headers["Content-Type"] = "text/html; charset=utf-8";
  71. //res.writeBody("Hello, World!\n" ~ result);
  72. res.writeBody( mustache.render("login_test", context) );
  73. }