diff --git a/socket_io.py b/socket_io.py index 11c573d..e8e1acf 100644 --- a/socket_io.py +++ b/socket_io.py @@ -139,6 +139,7 @@ def on_disconnect(self, client): def __init__(self): self.clients = {} + self.closed = False def _handle(self, info): command = info['command'] @@ -217,10 +218,13 @@ def listen(self, ws_port, py_port=None): os.close(handle) # run that script in node.js - process = subprocess.Popen(['node', path]) + env = os.environ + env["NODE_PATH"] = NodePath() + process = subprocess.Popen(['node', path], env=env) def cleanup(): process.kill() - os.remove(path) + if os.path.exists(path): + os.remove(path) atexit.register(cleanup) # make sure we can communicate with node.js @@ -236,10 +240,21 @@ def send(data, info): # run the server buffer = '' - while 1: - buffer += sock.recv(4096) + while not self.closed: + response = sock.recv(4096) + if response == "": + print "Node.js <-> Python socket broke in Socket.IO" + break + buffer += response index = buffer.find('\0') while index >= 0: data, buffer = buffer[0:index], buffer[index+1:] self._handle(json.loads(data)) index = buffer.find('\0') + cleanup() + + def close(self): + self.closed = True + +def NodePath(): + return os.path.join(os.path.dirname(__file__), "node_modules")