WindowsMaker
WindowsMaker

Reputation: 3400

cherrypy as a gevent-socketio server

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?

Upvotes: 1

Views: 2041

Answers (2)

abourget
abourget

Reputation: 2419

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.

Upvotes: 3

Sylvain Hellegouarch
Sylvain Hellegouarch

Reputation: 851

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.

Upvotes: 1

Related Questions