From bd2db0168d90bf2bcc2ad6e0f1c1f4c4bb75ec87 Mon Sep 17 00:00:00 2001 From: Braden Ehrat Date: Mon, 25 Feb 2013 00:00:24 -0600 Subject: [PATCH] Changed to work with socket.io 0.9 --- README.md | 6 +++++- package.json | 2 +- socket_io.py | 30 ++++++++++++------------------ 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 3efa002..dafe061 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,14 @@ +This is a fork from [evanw/socket.io-python](https://github.com/evanw/socket.io-python) and is currently upgraded to work with socket.io 0.9 (instead of the old 0.6 api). + +TODO: arbitrary events + # A socket.io bridge for Python This gives Python users access to socket.io, a node.js library. This library provides simple and efficient bidirectional communication between browsers and servers over a message-oriented socket. Transport is normalized over various technologies including WebSockets, Flash sockets, and AJAX polling. ## Installation -This bridge requires [node.js](http://nodejs.org) and [socket.io](http://socket.io). Install node.js and [npm](http://npmjs.org/), then run `npm install .` in this directory to install the correct version of socket.io (the newer versions have changed their API). +This bridge requires [node.js](http://nodejs.org) and [socket.io](http://socket.io). Install node.js and [npm](http://npmjs.org/), then run `npm install .` in this directory to install the correct version of socket.io. ## Usage diff --git a/package.json b/package.json index a065032..0f40197 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,6 @@ "name": "socket.io-python", "version": "0.0.1", "dependencies": { - "socket.io": "0.6" + "socket.io": "0.9" } } diff --git a/socket_io.py b/socket_io.py index 11c573d..c249430 100644 --- a/socket_io.py +++ b/socket_io.py @@ -6,7 +6,9 @@ and servers over a message-oriented socket. Transport is normalized over various technologies including WebSockets, Flash sockets, and AJAX polling. -For the latest source, visit https://github.com/evanw/socket.io-python +For Evan Wallace's latest source, visit https://github.com/evanw/socket.io-python +For the latest source, visit https://github.com/behrat/socket.io-python + ''' import os @@ -21,12 +23,8 @@ # UDP to allow for arbitrarily large packets. _js = ''' var net = require('net'); -var http = require('http'); -var io = require('socket.io'); - -// http server -var server = http.createServer(function() {}); -server.listen(%d); +var io = require('socket.io').listen(%d); +io.set('log level', 1); // tcp connection to server var tcp = net.createConnection(%d, 'localhost'); @@ -46,28 +44,24 @@ buffer += data.toString('utf8'); }); -// socket.io connection to clients -var socket = io.listen(server); function sendToServer(client, command, data) { data = JSON.stringify({ - session: client.sessionId, + session: client.id, command: command, data: data, - address: client.connection.remoteAddress, - port: client.connection.remotePort + address: client.handshake.address.address, + port: client.handshake.address.port }); tcp.write(data + '\0'); } function sendToClient(json) { if (json.broadcast) { - for (var session in socket.clients) { - socket.clients[session].send(json.data); - } - } else if (json.session in socket.clients) { - socket.clients[json.session].send(json.data); + io.sockets.send(json.data); + } else if (json.session in io.sockets.sockets) { + io.sockets.sockets[json.session].send(json.data); } } -socket.on('connection', function(client) { +io.sockets.on('connection', function(client) { sendToServer(client, 'connect', null); client.on('message', function(data) { sendToServer(client, 'message', data);