Skip to content

websockets

Websockets is a solution for where we previously used long timeouts (long-hanging) for. If you come across the name "Comet" they mean opening a connection from the client to the server and letting it wait until the server has data ready for the client. Websockets turn this around so you can have your script call a websockets server , which in browser-client view is at the position of the client ! Almost all new browser version support websockets now.

mod_pywebsocket

Due to outdated examples, these scripts do not work. And since autobahn (see above) does work.. i see no use in investigating this further. Pending...

installation

A lot of implementations are around, but this will concentrate on apache2 and python.

install
apt-get install python-mod-pywebsocket

After this you can get documentation about the two versions : apache and standalone server :

pydoc
pydoc mod_pywebsocket
pydoc mod_pywebsocket.standalone

demo

First an explanation of the python http server module :

SimpleHTTPServer

SimpleHTTPServer is a python module you can use to just server a directory directly onto a port.

SimpleHTTPServer
python -m SimpleHTTPServer 8889

-m means : search the python path for the module named and run it as a script. So the above line starts a 'simple' http server on port 8889 and server the files in the current directory. Short guide ? :

SimpleHTTPServer
1
2
3
4
mkdir test
cd test
echo "hello" >> index.html
python -m SimpleHTTPServer 8889

Browse to http://localhost:8889 , yes ... hello to you too. This is used in the following example as well

creating a standalone demo

Just do it in the test directory s before, but change index.html to :

demo
<!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://localhost:9876/");
            s.onopen = function(e) { alert("opened"); }
            s.onclose = function(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>

Second create a server.py script containing :

server.py
#!/usr/bin/env python

import socket, threading, time

def handle(s):
  print repr(s.recv(4096))
  s.send('''
HTTP/1.1 101 Web Socket Protocol Handshaker
Upgrade: WebSocketr
Connection: Upgrader
WebSocket-Origin: http://localhost:8889r
WebSocket-Location: ws://localhost:9876/r
WebSocket-Protocol: sample
  '''.strip() + 'rnrn')
  time.sleep(1)
  s.send('x00helloxff')
  time.sleep(1)
  s.send('x00worldxff')
  s.close()

s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 9876));
s.listen(1);
while 1:
  t,_ = s.accept();
  threading.Thread(target = handle, args = (t,)).start()

Now startup the server like this :

start
./server.py & python -m SimpleHTTPServer 8889