Subject: Re: [hybi] XMLRPC Server - websocket

From: "Yin, Li" <[email-masked]>
Date: Wed, 27 Jun 2012 08:46:18 +0000

hand_shake_response should end of “\r\n\r\n”, you can have a try again.

By the way, which browser are you using? If the WebSocket connection can’t be created, the failed reason should be printed on console of browser,
as far as I know, chromium can do that.
If the error still occurs, please provide console message from browser.

Regards,
Li Yin

From: [email-masked] [mailto:[email-masked]] On Behalf Of vinod kumar
Sent: Wednesday, June 27, 2012 3:49 PM
To: John Tamplin; [email-masked]
Subject: Re: [hybi] XMLRPC Server - websocket

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

---

From: vinod kumar <[email-masked]>
Date: Wed, 27 Jun 2012 10:53:26 +0200

Hi Li Yin,

Thanks a lot. The "\r\n" is the mistake. Now, it shows me the alert
"opened". So, connection is established. But when I try sending something
as it's shown in code, the other event "onmessage" is not getting
triggered. And browser console is empty now.

Thanks,
Vinodh

On Wed, Jun 27, 2012 at 10:46 AM, Yin, Li <[email-masked]> wrote:

>  hand_shake_response should end of “\r\n\r\n”, you can have a try again.**
> **
>
> ** **
>
> By the way, which browser are you using? If the WebSocket connection can’t
> be created, the failed reason should be printed on console of browser,****
>
> as far as I know, chromium can do that.****
>
> If the error still occurs, please provide console message from browser.***
> *
>
> ** **
>
> Regards,****
>
> Li Yin****
>
> ** **
>
> *From:* [email-masked] [mailto:[email-masked]] *On Behalf
> Of *vinod kumar
> *Sent:* Wednesday, June 27, 2012 3:49 PM
> *To:* John Tamplin; [email-masked]
> *Subject:* Re: [hybi] XMLRPC Server - websocket****
>
> ** **
>
> 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****
>

---

From: "Yin, Li" <[email-masked]>
Date: Wed, 27 Jun 2012 14:08:47 +0000

The data format you sent is incorrect.
More detailed information, you can read the RFC6455
http://tools.ietf.org/html/rfc6455#section-5.1

From: vinod kumar [mailto:[email-masked]]
Sent: Wednesday, June 27, 2012 4:53 PM
To: Yin, Li; [email-masked]
Subject: Re: [hybi] XMLRPC Server - websocket

Hi Li Yin,

Thanks a lot. The "\r\n" is the mistake. Now, it shows me the alert "opened". So, connection is established. But when I try sending something as it's shown in code, the other event "onmessage" is not getting triggered. And browser console is empty now.

Thanks,
Vinodh
On Wed, Jun 27, 2012 at 10:46 AM, Yin, Li <[email-masked]<mailto:[email-masked]>> wrote:
hand_shake_response should end of “\r\n\r\n”, you can have a try again.

Regards,
Li Yin

From: [email-masked]<mailto:[email-masked]> [mailto:[email-masked]<mailto:[email-masked]>] On Behalf Of vinod kumar
Sent: Wednesday, June 27, 2012 3:49 PM
To: John Tamplin; [email-masked]<mailto:[email-masked]>
Subject: Re: [hybi] XMLRPC Server - websocket

Hi John,

def handle(s):

s = socket.socket()

response sent vinod
message1 sent
message2 sent

Thanks,
Vinodh