2016-07-06 47 views
0

我正在使用geventwebsockets,但甚至無法獲得示例應用程序的工作。下面的代碼給我的錯誤:如何獲得gevent websockets的工作?

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() 

我已經手忙腳亂。誰能幫我嗎?非常感謝。

回答

0

不知是否對您的工作

如果添加echo_app如圖

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

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) 

和 傳像

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