Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes and improvements to robustness #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions socket_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def on_disconnect(self, client):

def __init__(self):
self.clients = {}
self.closed = False

def _handle(self, info):
command = info['command']
Expand Down Expand Up @@ -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
Expand All @@ -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")