[nanomsg] WebSocket test case not working?

  • From: Prem Shankar Kumar <meprem@xxxxxxxxx>
  • To: nanomsg@xxxxxxxxxxxxx
  • Date: Thu, 26 Feb 2015 22:48:28 +0530

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") >= 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", ["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

Other related posts: