Subject: [hybi] XMLRPC Server - websocket

From: vinod kumar <[email-masked]>
Date: Tue, 26 Jun 2012 11:35:25 +0200

Hi All,

    Can you please help me out how to connect a websocket to xmlrpc server
?  I have my xmlrpc server running. I made a client code for websocket too.
But I'm not able to connect the websocket to my server .

Thanks,
Vinodh

---

From: John Tamplin <[email-masked]>
Date: Tue, 26 Jun 2012 15:00:28 -0400

> But I'm not able to connect the websocket to my server .

Your server has to implement the WebSocket protocol.

-- 
John A. Tamplin
Software Engineer (GWT), Google

---

From: vinod kumar <[email-masked]>
Date: Wed, 27 Jun 2012 09:48:55 +0200

Hi John,

Thanks for your reply. I have implemented the websocket. Still facing some
trouble. The event onopen is not happening. When I send something from my
server, it occurs. But nothing like onmessage event happens.
Hi,

 I'm not sure whether the connection is established or not. The things
which I kept in onopen event are not getting alerted. Please have a look at
the codes below:
------
Server
------
#!/usr/bin/env python

import socket, threading, time, string
import hashlib
import base64

def get_response_handshake_key(s):
  hs_lines = s.split()
  part1 = ""
  parse_position = hs_lines[7].index('\\r')
  half1 = hs_lines[7][:parse_position]
  half2 = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
  raw_key = half1+half2
  sha1_object = hashlib.sha1()
  sha1_object.update(raw_key)
  half_encoded = sha1_object.digest()
  encoded_key = base64.b64encode(half_encoded)
  return encoded_key

def handle(s):
  key_half1 = repr(s.recv(4443))
  encoded_key = get_response_handshake_key(key_half1)
  hand_shake_response = "'HTTP/1.1 101 Switching Protocols \r\nUpgrade:
websocket \r\nConnection: Upgrade \r\nSec-WebSocket-Accept:
%s\r\n'"%(encoded_key)
  s.send(hand_shake_response)
  print 'response sent vinod'
  time.sleep(1)
  print 'message1 sent '
  s.send('\x00hello\xff')
  time.sleep(1)
  s.send('\x00world\xff')
  print 'message2 sent'
  s.close()

s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('ip_address', 4443));
print 'binding done '
s.listen(1);
print 'listening '
while 1:
  t,_ = s.accept();
  print 'something got accepted '
  threading.Thread(target = handle, args = (t,)).start()
----------------------------------------------------------------------------------------------------------
Client
---------
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Web Socket Example</title>
    <meta charset="UTF-8">
    <script>
      window.onload = function() {
        var s = new WebSocket("ws://ip_address:4443/");
        s.onopen = function(e) { alert("opened"); }
        s.onclose = function(e) { alert(e);alert("closed"); }
        s.onmessage = function(e) { alert("got: " + e.data); }
      };
    </script>
  </head>
    <body>
      <div id="holder" style="width:600px; height:300px"></div>
    </body>
</html>
-------------------------------------------------------------------------------
OUTPUT on console at server :
binding done
listening
something got accepted
'GET / HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nHost:
ip_address:4443\r\nOrigin:http://192.168.0.101:8888\r\nSec-WebSocket-Key:
JTsXI9vB6e/psrPTasC4zw==\r\nSec-WebSocket-Version: 13\r\n\r\n'

response sent vinod
message1 sent
message2 sent
---------------------------------------------------------------------------------
It's showing message sent here. But I'm not receiving any messages on
browser. Now even the "alert open" is happening for onopen event. I have
"s.close()" at the end because of which the only thing happening on browser
is : an alert with "closed"

Please help me out. The protocol is implemented all fine. I checked with
the encoders too. They are fine. There could be a small thing I'm missing.

Thanks,
Vinodh