1
votes

I am using geventwebsockets but can't even get the example app to work. The code below gives me the error:

Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 884, in handle_one_response self.run_application() File "/usr/local/lib/python2.7/dist-packages/geventwebsocket/handler.py", line 88, in run_application return super(WebSocketHandler, self).run_application() File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 870, in run_application self.result = self.application(self.environ, self.start_response) File "/usr/local/lib/python2.7/dist-packages/geventwebsocket/resource.py", line 90, in call raise Exception("No apps defined") Exception: No apps defined

from geventwebsocket import WebSocketServer, WebSocketApplication, Resource

class EchoApplication(WebSocketApplication):
    def on_open(self):
        print "Connection opened"

    def on_message(self, message):
        self.ws.send(message)

    def on_close(self, reason):
        print reason

WebSocketServer(
    ('', 8000),
    Resource({'/': EchoApplication})
).serve_forever()

I've been running around in circles. Can anyone help me out? Thanks so much.

1

1 Answers

0
votes

Does it work for you

if you add the echo_app

def echo_app(environ, start_response):
    websocket = environ.get("wsgi.websocket")

    if websocket is None:
        return http_handler(environ, start_response)
    try:
        while True:
            message = websocket.receive()
            websocket.send(message)
        websocket.close()
    except geventwebsocket.WebSocketError, ex:
        print "{0}: {1}".format(ex.__class__.__name__, ex)

and pass it like

print "Running %s from %s" % (agent, path)
WebSocketServer(("", 8000), echo_app, debug=False).serve_forever()

as shown in

https://bitbucket.org/noppo/gevent-websocket/src/0df192940acd288e8a8f6d2dd30329a3381c90f1/examples/echoserver.py