Hi, I have started working on CoffeScript/Javascript websocket library for nanomsg. I choose CoffeeScript because it's easier to read/code and compiles to JavaScript(no prior exp with JavaScript/CoffeeScript). Having finished three socket types(PAIR, REQ, REP)...I have few queries. 1. Where can I find(docs/rfc, nanomsg code, mangos code) various socket type message framing structure? Pub-Sub case will be interesting.. 2. Do you see a need to introduce queue for each socket on client side(CoffeeScript) similar to nanomsg? May be web-worker can be used here..need to explore. Please find below code snippets from library and corresponding client side interface. These interfaces will change as I further refine code. nanosock.coffee: ... class root.Pair extends NanoMsgSockBase byteArray: '' connect: (url) -> super(url, "pair.sp.nanomsg.org") recv: (evt) -> byteArray = new Uint8Array(evt.data) super(evt, 0, byteArray.length) class root.Req extends NanoMsgSockBase constructor: () -> @header = new Uint32Array(1) window.crypto.getRandomValues(@header) super() connect: (url) -> super(url, "rep.sp.nanomsg.org") recv: (evt) -> byteArray = new Uint8Array(evt.data) super(evt, 4, byteArray.length) send: (msg) -> outgoing_msg = new Uint32Array(@header.byteLength + msg.length) outgoing_msg.set(new Uint32Array(@header), 0) uint8View = new DataView(outgoing_msg.buffer) for i in [4...msg.length+4] uint8View.setUint8(i, msg.charCodeAt(i-4)) super(outgoing_msg) ... Client: ... <script src="../../../src/nanosock.js"></script> <script type="text/javascript"> function WebSocketTest() { if ("WebSocket" in window) { var pair = new Pair(); pair.connect("ws://127.0.0.1:80"); pair.onOpen( function() { console.log("connection is open..."); pair.send("Hello World from CoffeeScript!!!"); }); pair.onMessage( function(evt) { // Receive incoming data var incoming_data = pair.recv(evt); console.log("Received : " + incoming_data); pair.send("Hello World from CoffeeScript!!!"); }); pair.onError( function() { console.log("WebSocket Error"); }); pair.onClose( function() { console.log("connection is closed..."); }); ... Regards, Prem On Thu, Feb 26, 2015 at 11:55 PM, Prem Shankar Kumar <meprem@xxxxxxxxx> wrote: > > Thanks Garrett. > > I just changed socket type to REQ and returning same data to server. > It looks ok now. > > Yes, a JavaScript library would be nice. > > #include <assert.h> > #include <unistd.h> > #include <string.h> > #include <pthread.h> > #include <stdio.h> > #include <nanomsg/nn.h> > #include <nanomsg/reqrep.h> > > int main (const int argc, const char **argv) > { > int sock = nn_socket (AF_SP, NN_REQ); > assert (sock >= 0); > assert (nn_bind (sock, "ws://127.0.0.1:80") >= 0); > while (1) > { > char *buf = NULL; > int bytes = nn_send (sock, "Hello", 5, 0); > bytes = nn_recv (sock, &buf, NN_MSG, 0); > assert (bytes >= 0); > printf ("RECEIVED :%s\n", buf); > nn_freemsg (buf); > } > return nn_shutdown (sock, 0); > } > > $ sudo ./nanoweb > RECEIVED :Hello > RECEIVED :Hello > RECEIVED :Hello > RECEIVED :Hello > RECEIVED :Hello > RECEIVED :Hello > RECEIVED :Hello > RECEIVED :Hello > RECEIVED :Hello > RECEIVED :Hello > RECEIVED :Hello > ^C > > <!DOCTYPE HTML> > <html> > <head> > <script type="text/javascript"> > function WebSocketTest() > { > if ("WebSocket" in window) > { > alert("WebSocket is supported by your Browser!"); > // Let us open a web socket > var ws = new WebSocket("ws://127.0.0.1:80", ["req.sp.nanomsg.org"]); > > ws.onopen = function() > { > // Web Socket is connected, send data using send() > alert("Message is sent..."); > //ws.send(""); > //ws.send("Message"); > }; > ws.onmessage = function (evt) > { > var received_msg = evt.data; > alert(evt.data); > ws.send(evt.data); > }; > ws.onclose = function() > { > // websocket is closed. > alert("Connection is closed..."); > }; > } > else > { > // The browser doesn't support WebSocket > alert("WebSocket NOT supported by your Browser!"); > } > } > </script> > </head> > <body> > <div id="sse"> > <a href="javascript:WebSocketTest()">Run WebSocket</a> > </div> > </body> > </html> > > > Regards, > Prem > > > On Thu, Feb 26, 2015 at 10:57 PM, Garrett D'Amore <garrett@xxxxxxxxxx> > wrote: > >> Your problem is that you can't *just* send the bytes in your JavaScript. >> While the *transport* is web socket, the underlying REP protocol requires a >> specific header be included -- that means you have to inject at a minimum a >> 32-bit request ID with the high order bit set, in front of your message. >> >> The receiving socket in libnanomsg will reject the message as malformed >> without that header. >> >> We really need to create a JavaScript SP library that implements these >> details, as well as handles some of the semantic issues (like implementing >> request/reply matching, and raw vs. cooked semantics for REQ/REP, >> SURVEYOR/RESPONDENT, etc.) >> >> - Garrett >> >> On Feb 26, 2015, at 12:18 PM, Prem Shankar Kumar <meprem@xxxxxxxxx> >> wrote: >> >> Hi, >> >> I was trying a simple WebSocket test case in development branch. >> It doesn't seem working. >> >> Could you please check what's wrong with test case? >> >> // Server: >> #include <assert.h> >> #include <unistd.h> >> #include <string.h> >> #include <pthread.h> >> #include <stdio.h> >> #include <nanomsg/nn.h> >> #include <nanomsg/reqrep.h> >> >> int main (const int argc, const char **argv) >> { >> int sock = nn_socket (AF_SP, NN_REP); >> assert (sock >= 0); >> assert (nn_bind (sock, "ws://127.0.0.1:80 <http://127.0.0.1/>") >= 0); >> while (1) >> { >> char *buf = NULL; >> int bytes = nn_recv (sock, &buf, NN_MSG, 0); >> assert (bytes >= 0); >> printf ("RECEIVED :%s\n", buf); >> bytes = nn_send (sock, "Hello", 5, 0); >> nn_freemsg (buf); >> } >> return nn_shutdown (sock, 0); >> } >> >> Client: >> <!DOCTYPE HTML> >> <html> >> <head> >> <script type="text/javascript"> >> function WebSocketTest() >> { >> if ("WebSocket" in window) >> { >> alert("WebSocket is supported by your Browser!"); >> // Let us open a web socket >> var ws = new WebSocket("ws://127.0.0.1:80 <http://127.0.0.1/>", [" >> rep.sp.nanomsg.org"]); >> ws.onopen = function() >> { >> // Web Socket is connected, send data using send() >> alert("Message is sent..."); >> ws.send("Message"); >> }; >> ws.onmessage = function (evt) >> { >> var received_msg = evt.data; >> alert("Message is received..."); >> }; >> ws.onclose = function() >> { >> // websocket is closed. >> alert("Connection is closed..."); >> }; >> } >> else >> { >> // The browser doesn't support WebSocket >> alert("WebSocket NOT supported by your Browser!"); >> } >> } >> </script> >> </head> >> <body> >> <div id="sse"> >> <a href="javascript:WebSocketTest()">Run WebSocket</a> >> </div> >> </body> >> </html> >> >> >> Server Output: >> $ sudo ./nanoweb >> <No Output> >> >> Client Header: >> >> Request URL:ws://127.0.0.1/ >> Request Method:GET >> Status Code:101 Switching Protocols >> Request Headersview source >> Accept-Encoding:gzip,deflate,sdch >> Accept-Language:en-US,en;q=0.8 >> Cache-Control:no-cache >> Connection:Upgrade >> Host:127.0.0.1 >> Origin:null >> Pragma:no-cache >> Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits >> Sec-WebSocket-Key:q6rlJc/CM99JcAnRPtXuxQ== >> Sec-WebSocket-Protocol:rep.sp.nanomsg.org >> Sec-WebSocket-Version:13 >> Upgrade:websocket >> User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, >> like Gecko) Chrome/38.0.2125.111 Safari/537.36 >> Response Headersview source >> Connection:Upgrade >> Sec-WebSocket-Accept:jUk7KfyHAecXruo3DkLQMgeHNZc= >> Sec-WebSocket-Protocol:rep.sp.nanomsg.org >> Upgrade:websocket >> >> Regards, >> Prem >> >> >> >