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 > > >