123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <link href="synrc.css" type="text/css" rel="stylesheet">
- </head>
- <body>
- <div class="threecol">
- <div class="left">
- <div class="hints"></div>
- <div class="main">
- <h1><a name="chapter2">N2O Protocol</a></h1>
- <h1><a name="p1.1">Requests</a></h1>
- <h2><a name="p1.1">pickle</a></h2>
- Picled messaged are used if you send messages over unencrypted
- channel and want to hide the content of the message,
- that was generated on server. You can use BASE64 pickling mechanisms
- with optional AES/RIPEMD160 encrypting.
- <code>ws.send(enc(
- tuple(atom('pickle'),
- binary('ddtake'),
- binary('g2gCaAVkAAJldmQABWluZGV4ZAAEdGFrZWsABH'+
- Rha2VkAAVldmVudGgDYgAABXViAAQKXmIAC3cK'),
- [tuple(atom('ddtake'),'0')])));</code>
- Where Base64 represents the N2O EVENT:
- <code>#ev{module=index,payload=take,trigger="take",name=event}</code>
- <p>This is Nitrogen-based messaging model.<br>
- This request will return JSON with EVAL field only.</p>
- <h2><a name="p1.1">client</a></h2>
- <p>Client messages usually originated at client and represent the Client API Requests:</p>
- <code>ws.send(enc(
- tuple(
- atom('client'),
- tuple(atom('join_game'),1000001))));</code>
- <p>NOTE: This request may return JSON with EVAL and DATA fields.</p>
- <h2><a name="p1.1">bert</a></h2>
- <p>When you want to receive BERT messages on client you usually ask the
- server by sending:</p>
- <code>ws.send(enc(
- tuple(
- atom('bert'),
- binary('API Request'));</code>
- <p>This messages could be handled as this:</p>
- <code>event({bert,Message}) ->
- wf:info("This API will return BERT enveloped echo"),
- {answer,echo,Message};</code>
- <p>NOTE: you will always receive BERT messages.</p>
- <h2><a name="p1.1">binary</a></h2>
- <p>When you need raw binary Blob on client side,
- for images or other raw data you can ask server like this:</p>
- <code>ws.send(enc(
- tuple(
- atom('binary'),
- binary('API Request'));</code>
- <p>And handle also in binary clause:</p>
- <code>event({binary,Message}) ->
- wf:info("This API will return Raw Binary"),
- <<84,0,0,0,108>>;</code>
- <p>NOTE: if event returns not the binary client will recieve BERT encoded message.</p>
- <h2><a name="p1.1">server</a></h2>
- Server messages are usually being sent to client originated on the
- server by sending <b>info</b> notifications directly to Web Socket process:
- <code>> WebSocketPid ! {server, Message} </code>
- <p>You can obtain this Pid like here:</p>
- <code>event(init) -> wf:info("Web Socket Pid: ~p",[self()]);</code>
- <p>You can also send server messages from client relays:</p>
- <code>ws.send(enc(
- tuple(
- atom('server'),
- binary('Binary Message'));</code>
- <p>NOTE: This request may return JSON with EVAL and DATA fields.</p>
- <h1><a name="p1.1">Responses</a></h1>
- <h2><a name="p1.1">JSON envelop</a></h2>
- <p>Each message from Web Socket channel to Client encoded as JSON object.
- <a href="https://github.com/synrc/n2o_scripts/blob/master/n2o/n2o.js">N2O.js</a>
- is used to decode WebSocket binary messages from JSON container.</p>
- <code>{ "eval": "ws.send("Send Back This String");",
- "data": [131,104,2,100,0,7,109,101,115,115,
- 97,103,101,107,0,5,72,101,108,108,111] }
- </code>
- <p>EVAL values evaluated immediately and DATA values passed
- to handle_web_socket(data) function if exists.</p>
- <code>function handle_web_socket(body) { console.log(body); } </code>
- <h2><a name="p1.1">JSON enveloped BERT</a></h2>
- <p>Usually in DATA come <a href="http://bert-rpc.org">BERT</a> messages (Binary Erlang Term Format).
- <a href="https://github.com/synrc/n2o_scripts/blob/master/n2o/js">js</a>
- is used to decode Game Protocol message.</p>
- <code>function handle_web_socket(body) {
- console.log(String(dec(body))); } </code>
- <code style="background-color:white;margin-bottom:-5px;">E> Received: {message,"Hello"} </code>
- <h2><a name="">BERT binary</a></h2>
- <code>function handle_web_socket(body) {
- console.log(String(dec(body))); } </code>
- <h2><a name="p1.1">RAW binary</a></h2>
- <code>function handle_web_socket_blob(body) { } </code>
- <code style="background-color:white;margin-bottom:-5px;">E> Unknown Raw Binary Received: [72,101,108,108,111]</code>
- </div>
- <div class="contents">
- <iframe src="contents.htm" frameborder=0 width=340 height=2190></iframe>
- </div>
- </div>
- </body>
- </html>
|