ws_bert_login.d 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. alias int8 = byte; // -128 — 127
  2. alias int16 = short; // -32768 — 32767
  3. alias int32 = int; // -2147483648 — 2147483647
  4. alias int64 = long; // -9223372036854775808 — 9223372036854775807
  5. alias uint8 = ubyte; // 0 — 255
  6. alias uint16 = ushort; // 0 — 65535
  7. alias uint32 = uint; // 0 — 4294967295
  8. alias uint64 = ulong; // 0 — 18446744073709551615
  9. alias f32 = float;
  10. alias f64 = double;
  11. // char '\xFF' unsigned 8 bit (UTF-8 code unit)
  12. // wchar '\uFFFF' unsigned 16 bit (UTF-16 code unit)
  13. // dchar '\U0000FFFF' unsigned 32 bit (UTF-32 code unit)
  14. import vibe.core.core;
  15. import vibe.http.router;
  16. import vibe.http.server;
  17. import vibe.http.fileserver;
  18. import vibe.http.websockets;
  19. import vibe.core.log;
  20. import std.stdio;
  21. import std.string;
  22. import std.array;
  23. import std.conv : to;
  24. import bert; // https://github.com/221V/dlang_erl_bert https://git.4dev.win/game1/dlang_erl_bert
  25. import mustache;
  26. alias MustacheEngine!(string) Mustache;
  27. void ws_bert_handle(scope WebSocket sock){
  28. // simple echo server + :)
  29. while(sock.connected){
  30. //auto msg = sock.receiveText();
  31. //sock.send(msg ~ " :)");
  32. auto msg = sock.receiveBinary();
  33. if(msg == "1"){
  34. sock.send("0"); // ws PING - PONG
  35. }else{
  36. writeln("Received: ", msg);
  37. auto decoder = new BertDecoder( cast(ubyte[])msg.dup );
  38. auto decoded = decoder.decode();
  39. msg_match(decoded, sock);
  40. }
  41. }
  42. }
  43. void msg_match(BertValue decoded, WebSocket sock){
  44. writeln("Decoded: ", decoded.toString());
  45. if(decoded.type_ == BertType.Tuple){
  46. auto decoded1 = decoded.tupleValue;
  47. if(decoded1.length == 3){
  48. if(auto num1 = cast(uint8)decoded1[0].intValue){
  49. writeln("num1 = ", num1, " ", typeof(num1).stringof); // ws.send(enc(tuple( number(1), number(42), number(777) ))); // Decoded: {1, 42, 777} // num1 = 1 ubyte
  50. sock.send("{console.log(" ~ to!string(num1 + 42) ~ ")}"); // got 43 in browser console
  51. } // else do nothing
  52. if(auto str1 = cast(string)decoded1[1].binaryValue){
  53. 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
  54. sock.send("{console.log('" ~ str1 ~ " la-la-la" ~ "')}"); // got 'blabla la-la-la' in browser console
  55. } // else do nothing
  56. // var big_value = bigInt("61196067033413");
  57. // ws.send(enc(tuple( number(1), bin('9'), bignum( big_value ) ))); // got as long for auto
  58. if(decoded1[2].type_ == BertType.Int){
  59. if(auto num3 = decoded1[2].intValue){
  60. writeln("num3 = ", num3, " ", typeof(num3).stringof); // ws.send(enc(tuple( number(1), bin('9'), number(1) ))); // Decoded: {1, <<57>>, 1} // num3 = 1 long
  61. } // else do nothing
  62. } // else do nothing
  63. // var big_value = bigInt("6119606703341361196067033413");
  64. // ws.send(enc(tuple( number(1), bin('9'), bignum( big_value ) ))); // got as BigInt
  65. if(decoded1[2].type_ == BertType.BigInt){
  66. if(auto num3b = decoded1[2].bigintValue){
  67. writeln("num3b = ", num3b, " ", typeof(num3b).stringof); // {1, <<57>>, 6119606703341361196067033413} // num3b = 6119606703341361196067033413 BigInt
  68. } // else do nothing
  69. } // else do nothing
  70. if(decoded1[2].type_ == BertType.List){
  71. 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]}
  72. if(list1.length == 3){
  73. //if(auto num31 = cast(uint32)list1[0].intValue){
  74. if(auto num31 = list1[0].intValue){
  75. 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 uint
  76. sock.send("{console.log(" ~ to!string(num31 + 77) ~ ")}"); // got 78 in browser console
  77. } // else do nothing
  78. } // else do nothing
  79. } // else do nothing
  80. } // else do nothing
  81. } // else do nothing
  82. }
  83. void login_test(HTTPServerRequest req, HTTPServerResponse res){
  84. Mustache mustache;
  85. auto context = new Mustache.Context;
  86. mustache.path = "priv";
  87. mustache.ext = "dtl";
  88. context["lang"] = "en";
  89. context["number1"] = 42;
  90. context.useSection("maybe1");
  91. context["part1"] = "<span>777</span>";
  92. context["result1"] = "Hello, World!\n";
  93. res.headers["Content-Type"] = "text/html; charset=utf-8";
  94. //res.writeBody("Hello, World!\n" ~ result);
  95. res.writeBody( mustache.render("login_test", context) );
  96. }