n2o_proto.htm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  4. <link href="synrc.css" type="text/css" rel="stylesheet">
  5. </head>
  6. <body>
  7. <div class="threecol">
  8. <div class="left">
  9. <div class="hints"></div>
  10. <div class="main">
  11. <h1><a name="chapter2">N2O Protocol</a></h1>
  12. <h1><a name="p1.1">Requests</a></h1>
  13. <h2><a name="p1.1">pickle</a></h2>
  14. Picled messaged are used if you send messages over unencrypted
  15. channel and want to hide the content of the message,
  16. that was generated on server. You can use BASE64 pickling mechanisms
  17. with optional AES/RIPEMD160 encrypting.
  18. <code>ws.send(enc(
  19. tuple(atom('pickle'),
  20. binary('ddtake'),
  21. binary('g2gCaAVkAAJldmQABWluZGV4ZAAEdGFrZWsABH'+
  22. Rha2VkAAVldmVudGgDYgAABXViAAQKXmIAC3cK'),
  23. [tuple(atom('ddtake'),'0')])));</code>
  24. Where Base64 represents the N2O EVENT:
  25. <code>#ev{module=index,payload=take,trigger="take",name=event}</code>
  26. <p>This is Nitrogen-based messaging model.<br>
  27. This request will return JSON with EVAL field only.</p>
  28. <h2><a name="p1.1">client</a></h2>
  29. <p>Client messages usually originated at client and represent the Client API Requests:</p>
  30. <code>ws.send(enc(
  31. tuple(
  32. atom('client'),
  33. tuple(atom('join_game'),1000001))));</code>
  34. <p>NOTE: This request may return JSON with EVAL and DATA fields.</p>
  35. <h2><a name="p1.1">bert</a></h2>
  36. <p>When you want to receive BERT messages on client you usually ask the
  37. server by sending:</p>
  38. <code>ws.send(enc(
  39. tuple(
  40. atom('bert'),
  41. binary('API Request'));</code>
  42. <p>This messages could be handled as this:</p>
  43. <code>event({bert,Message}) ->
  44. wf:info("This API will return BERT enveloped echo"),
  45. {answer,echo,Message};</code>
  46. <p>NOTE: you will always receive BERT messages.</p>
  47. <h2><a name="p1.1">binary</a></h2>
  48. <p>When you need raw binary Blob on client side,
  49. for images or other raw data you can ask server like this:</p>
  50. <code>ws.send(enc(
  51. tuple(
  52. atom('binary'),
  53. binary('API Request'));</code>
  54. <p>And handle also in binary clause:</p>
  55. <code>event({binary,Message}) ->
  56. wf:info("This API will return Raw Binary"),
  57. <<84,0,0,0,108>>;</code>
  58. <p>NOTE: if event returns not the binary client will recieve BERT encoded message.</p>
  59. <h2><a name="p1.1">server</a></h2>
  60. Server messages are usually being sent to client originated on the
  61. server by sending <b>info</b> notifications directly to Web Socket process:
  62. <code>> WebSocketPid ! {server, Message} </code>
  63. <p>You can obtain this Pid like here:</p>
  64. <code>event(init) -> wf:info("Web Socket Pid: ~p",[self()]);</code>
  65. <p>You can also send server messages from client relays:</p>
  66. <code>ws.send(enc(
  67. tuple(
  68. atom('server'),
  69. binary('Binary Message'));</code>
  70. <p>NOTE: This request may return JSON with EVAL and DATA fields.</p>
  71. <h1><a name="p1.1">Responses</a></h1>
  72. <h2><a name="p1.1">JSON envelop</a></h2>
  73. <p>Each message from Web Socket channel to Client encoded as JSON object.
  74. <a href="https://github.com/synrc/n2o_scripts/blob/master/n2o/n2o.js">N2O.js</a>
  75. is used to decode WebSocket binary messages from JSON container.</p>
  76. <code>{ "eval": "ws.send("Send Back This String");",
  77. "data": [131,104,2,100,0,7,109,101,115,115,
  78. 97,103,101,107,0,5,72,101,108,108,111] }
  79. </code>
  80. <p>EVAL values evaluated immediately and DATA values passed
  81. to handle_web_socket(data) function if exists.</p>
  82. <code>function handle_web_socket(body) { console.log(body); } </code>
  83. <h2><a name="p1.1">JSON enveloped BERT</a></h2>
  84. <p>Usually in DATA come <a href="http://bert-rpc.org">BERT</a> messages (Binary Erlang Term Format).
  85. <a href="https://github.com/synrc/n2o_scripts/blob/master/n2o/js">js</a>
  86. is used to decode Game Protocol message.</p>
  87. <code>function handle_web_socket(body) {
  88. console.log(String(dec(body))); } </code>
  89. <code style="background-color:white;margin-bottom:-5px;">E> Received: {message,"Hello"} </code>
  90. <h2><a name="">BERT binary</a></h2>
  91. <code>function handle_web_socket(body) {
  92. console.log(String(dec(body))); } </code>
  93. <h2><a name="p1.1">RAW binary</a></h2>
  94. <code>function handle_web_socket_blob(body) { } </code>
  95. <code style="background-color:white;margin-bottom:-5px;">E> Unknown Raw Binary Received: [72,101,108,108,111]</code>
  96. </div>
  97. <div class="contents">
  98. <iframe src="contents.htm" frameborder=0 width=340 height=2190></iframe>
  99. </div>
  100. </div>
  101. </body>
  102. </html>