Adding Socket.IO to existing Django/WSGI project

2019-04-16 14:02发布

问题:

This guy seems to be trying to do the same thing but the answer to his question isn't clear enough. I'm not all that familiar with setting up socket servers so I'm still a bit lost here. Has anyone accomplished this before? How do you spin up the socket server and keep it running?

I wanted to comment on his post and ask him if he had success but I don't have enough reputation yet.

回答1:

My final solution to this was using the built-in runserver_socketio command for the manage.py script, running on 0.0.0.0:9000. I'm using Ubuntu so I made an upstart config to keep it alive. Seems to be working just fine running alongside my Apache/WSGI server. If anyone has a better solution please speak up.



回答2:

A approach I came up with was using the wsgi the apache uses to instance a thread that just calls the runserver_socketio command. That's not, for sure, the best approach. But if someone finds a better solution. Let us know in this post.

def init_socketio():
    os.system('python manage.py runserver_socketio 0.0.0.0:9000')

socketio_thread = Thread(target=init_socketio, args=())
socketio_thread.start()