A couple of weeks ago, Tim and I worked on a little game/demo using WebSockets and C# (I haven’t been able to put it online since I do not have a Windows server). It was a lot of fun and we were able to see the potential of WebSockets and how much internet can will change in the next few years because of them.
Today I spent a couple of hours researching and implementing a WebSocket solution that I could run in my linux servers, and I found this very nice tutorial for creating a Python WebSockets server: http://yz.mit.edu/wp/web-sockets-tutorial-with-simple-python-server/
So I took that code and modified to create a very, very basic chat, that broadcasts received messages to all connected clients. You can try it here (as long as the python server is still up): http://enekoalonso.com/research/html5/websockets/python/
Here is the client code:
-
var form = document.getElementById('form');
-
var msg = document.getElementById('msg');
-
var output = document.getElementById('serveroutput');
-
-
var s = new WebSocket("ws://"+window.location.hostname+":9876/");
-
s.onopen = function(e) {
-
console.log("opened");
-
out('Connected.');
-
}
-
s.onclose = function(e) {
-
console.log("closed");
-
out('Connection closed.');
-
}
-
s.onmessage = function(e) {
-
console.log("got: " + e.data);
-
out(e.data);
-
}
-
-
form.onsubmit = function(e) {
-
e.preventDefault();
-
msg.value = '';
-
window.scrollTop = window.scrollHeight;
-
}
-
-
function sendMsg() {
-
s.send(msg.value);
-
}
-
-
function out(text) {
-
var el = document.createElement('p');
-
el.innerHTML = text;
-
output.appendChild(el);
-
}
-
-
msg.focus();
And the server code:
-
#!/usr/bin/env python
-
-
import socket, threading
-
-
# httphost = "localhost:8888"
-
# sockethost = "localhost:9876"
-
httphost = "enekoalonso.com"
-
sockethost = "enekoalonso.com:9876"
-
-
def handle(s, addr):
-
handsake = "HTTP/1.1 101 Web Socket Protocol Handshake\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\nWebSocket-Origin: http://%s\r\nWebSocket-Location: ws://%s/\r\nWebSocket-Protocol: sample\r\n\r\n" % (httphost, sockethost)
-
s.send(handsake)
-
data = s.recv(1024)
-
lock = threading.Lock()
-
-
while 1:
-
data = s.recv(1024)
-
if not data: break
-
print 'Data from', addr, ':', data
-
lock.acquire()
-
[conn.send(data) for conn in clients]
-
lock.release()
-
-
print 'Client closed:', addr
-
lock.acquire()
-
clients.remove(s)
-
lock.release()
-
s.close()
-
-
def start_server():
-
s = socket.socket()
-
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
-
s.bind(('', 9876))
-
s.listen(1)
-
while 1:
-
conn, addr = s.accept()
-
print 'Connected by', addr
-
clients.append(conn)
-
threading.Thread(target = handle, args = (conn,addr)).start()
-
-
clients = []
-
start_server()
Check the live demo (be aware WebSockets are only supported by Google Chrome right now): http://enekoalonso.com/research/html5/websockets/python/
Open a few tabs and send some text. You should see it appearing in all of them. It will also work if you open the page in multiple browser windows or even if you open the page in different computers.
WebSockets with Python! Yay!
http://dev.enekoalonso.com/2010/05/22/more-websockets-now-with-python/
This comment was originally posted on Twitter