Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 178860aee0dfa180831400d403a76a3dcbcdef12 @drewlesueur committed Jan 25, 2010
Showing with 144 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. 0 README
  3. BIN favicon.png
  4. +5 −0 handler.py
  5. +38 −0 index.html
  6. +95 −0 python-socket-server.py
  7. +3 −0 tick.py
  8. +2 −0 util.py
@@ -0,0 +1 @@
+*.pyc
0 README
No changes.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,5 @@
+import util
+
+def handle(data, socko, to_read, to_write, to_error,scope):
+ util.ws_send(socko, scope.setdefault('last_message', ""))
+ scope['last_message'] = data
@@ -0,0 +1,38 @@
+<!doctype html>
+<html>
+<head>
+<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
+<script>
+if ("WebSocket" in window) {
+ var ws = new WebSocket("ws://aimee-pc:5555/");
+ //setInterval(function(){ alert(ws.readyState) }, 1000)
+ ws.onopen = function() {
+ alert("success")
+ ws.send("message to send");
+ };
+ ws.onmessage = function (evt) {
+ var m = evt.data;
+ console.log(m)
+ };
+ ws.onclose = function() {
+ alert("closed")
+ };
+} else {
+ alert("No web socket support"); //flash alternative ?
+}
+$(document).ready(function(){
+ $('#form').submit(function(){
+ ws.send($('#message').val())
+ $('#message').val('')
+ return false;
+ })
+})
+</script>
+</head>
+<body>
+Web socket
+<form id="form" action="http://www.google.com">
+<input id="message">
+</form>
+</body>
+</html>
@@ -0,0 +1,95 @@
+import socket
+import select
+import time
+import handler
+import tick
+cycles_per_tick = 5000
+port = 5555
+hostname = socket.gethostname() #"127.0.0.1" #"192.168.1.104"
+hostname = hostname.lower() #important
+websocket_location = "/"
+scope = {}
+serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+serversocket.bind((hostname, port)) #this is the one you want
+hnp = hostname + ":" + str(port)
+print "go to http://" + hnp
+serversocket.listen(5)
+serversocket.setblocking(0)
+#serversocket.settimeout(10000);
+#serversocket.settimeout(.01);
+def handle_data(data, socko, to_read, to_write, to_error):
+ if data[0:len("<policy-file-request/>")] == "<policy-file-request/>":
+ socko.send( '<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain="*" to-ports= "*" /></cross-domain-policy>')
+ print "sent policy file"
+ closesocket(socko)
+ elif data[0:55] == "GET / HTTP/1.1\r\nUpgrade: WebSocket\r\nConnection: Upgrade":
+ websocket_response = u"HTTP/1.1 101 Web Socket Protocol Handshake\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\nWebSocket-Origin: http://"+ hnp +"\r\nWebSocket-Location: ws://"+ hnp + websocket_location + "\r\n\r\n"
+ socko.send(websocket_response)
+ print websocket_response
+ print "web socky"
+ elif data[0:14] == "GET / HTTP/1.1": #normal http
+ file = open("index.html", "r")
+ contents = file.read()
+ print "normal http"
+ socko.send("HTTP/1.1 200 OK\r\n"
+ "Connection: close\r\n" +
+ "Content-Type: text/html\r\n" +
+ "Content-Length: " + str(len(contents)) + "\r\n\r\n" +
+ contents)
+ elif data[0:25] == "GET /favicon.ico HTTP/1.1":
+ print "favicon"
+ contents = open("favicon.png", "rb").read()
+ socko.send("HTTP/1.1 200 OK\r\n"
+ "Connection: close\r\n" +
+ "Content-Type: image/png\r\n" +
+ "Content-Length: " + str(len(contents)) + "\r\n\r\n" +
+ contents)
+ elif (data[0] == "\x00" and data[-1] == "\xff"): #websocket format
+ handler.handle(data[1:-1], socko, to_read, to_write, to_error, scope)
+ else:
+ print "you said"
+ print data
+ for i in data:
+ print ord(i)
+connections = []
+conn_info = [];
+counter = 0;
+def closesocket(socko):
+ socko.close()
+ connections.remove(socko) #remove is pretty cool if it works!
+ socko = None
+while True:
+ counter = counter + 1
+ time.sleep(.001) #change this in the future for speed
+ ready_to_read, ready_to_write, in_error = select.select([serversocket],[],[],0) #should I change that 0 to .001
+ for sock in ready_to_read:
+ print "test"
+ (clientsocket, address) = sock.accept()
+ print "putting " + address[0] + " onto connections";
+ conn_info.append(address)
+ clientsocket.setblocking(0)
+ connections.append(clientsocket)
+ print len(connections)
+ if len(connections) > 0:
+ to_read, to_write, to_error = select.select(connections,connections,connections,0)
+ for socko in to_read:
+ try: #i needed this try block only for google chrome! wierd.
+ data = socko.recv(1024) #1024 potential problem causer
+ except socket.error, msg:
+ closesocket(socko)
+ break
+ print data
+ if not data:
+ print "no data"
+ closesocket(socko)
+ break
+ try:
+ handle_data(data, socko, to_read, to_write, to_error)
+ except socket.error, msg:
+ closesocket(socko)
+ for socko in to_error:
+ print socko + " had an error"
+ for socko in to_write:
+ if counter % cycles_per_tick == 0:
+ print "tick"
+ tick.tick(socko,scope)
@@ -0,0 +1,3 @@
+import util
+def tick(socko,scope):
+ util.ws_send(socko, scope.setdefault("last_message", ""))
@@ -0,0 +1,2 @@
+def ws_send(socko, message):
+ socko.send('\x00' + message.encode('utf-8') + '\xff')

0 comments on commit 178860a

Please sign in to comment.