cherrypy as a gevent-socketio server

2019-08-07 10:06发布

I have just started using gevent-socketio and it's great!

But I have been using the default socketioserver and socketio_manage from the chat tutorial and was wondering how to integrate socketio with cherrypy.

essentially, how do I turn this:

class MyNamespace(BaseNamespace):...

def application(environ, start_response):
    if environ['PATH_INFO'].startswith('/socket.io'):
        return socketio_manage(environ, { '/app': MyNamespace})
    else:
        return serve_file(environ, start_response)

def serve_file(...):...

sio_server = SocketIOServer(
    ('', 8080), application, 
    policy_server=False) sio_server.serve_forever()

into a normal cherrypy server?

2条回答
\"骚年 ilove
2楼-- · 2019-08-07 10:21

CherryPy doesn't implement the socket.io protocol, nor does it support WebSocket as a built-in. However, there is an extension to CherryPy, called ws4py, that implements only the bare WebSocket protocol on top of its stack. You could start there probably.

查看更多
再贱就再见
3楼-- · 2019-08-07 10:35

Gevent-socketio is based on Gevent, and Gevent's web server. There are two implementations: pywsgi, which is pure python, and wsgi, which uses libevent's http implementation.

See the paragraph starting with "The difference between pywsgi.WSGIServer and wsgi.WSGIServer" over here:

http://www.gevent.org/servers.html

Only those servers are "green", in the sense that they yield the control to the Gevent loop.. so you can only use those servers afaik. The reason for this is that the server is present at the very beginning of the request, and will know how to handle the "Upgrade" and websockets protocol negotiations, and it will pass values inside the "environ" that the next layer (SocketIO) will expect and know how to handle.

You will also need to use the gevent-websocket package.. because it is green (and gevent-socketio is based on that one). You can't just swap the websocket stack.

Hope this helps.

查看更多
登录 后发表回答