web2py / websockets - websocket_messaging.py 中的示例不起作用?

web2py / websockets - the example in websocket_messaging.py doesn't work?

我原样复制了gluon/contrib/websocket_messaging.py文件中的两个动作。

controllers/debug.py:

def index():
    form=LOAD('debug','ajax_form',ajax=True)
    script=SCRIPT('''
        jQuery(document).ready(function(){
          var callback=function(e){alert(e.data)};
          if(!web2py_websocket('ws://127.0.0.1:8888/realtime/mygroup',callback))
            alert("html5 websocket not supported by your browser, try Google Chrome");
        });
    ''')
    return dict(form=form, script=script)

def ajax_form():
    form=SQLFORM.factory(Field('message'))
    if form.accepts(request,session):
        from gluon.contrib.websocket_messaging import websocket_send
        websocket_send(
            'http://127.0.0.1:8888',form.vars.message,'mykey','mygroup')
    return form

views/debug/index.html:

{{extend 'layout.html'}}
{{=form}}
{{=script}}

但是当我连接到该网站时,我看到以下错误消息:

Firefox can't establish a connection to the server at ws://127.0.0.1:8888/realtime/mygroup

我这样启动服务器:

python gluon/contrib/websocket_messaging.py -k mykey -p 8888

那里的消息是:

WARNING:tornado.access:403 GET /realtime/mygroup (127.0.0.1) 0.55ms

但我可以看到我发送的任何消息,只是不会通过 alert 功能弹出。

作为免责声明,我是 java 和 websockets 的新手,因此非常感谢您的帮助。

所以问题是我的 websocket_messaging.py 缺少以下内容:

def check_origin( self , origin ):
    return True

class DistributeHandler(tornado.websocket.WebSocketHandler)

尽管 https://github.com/web2py/web2py/blob/master/gluon/contrib/websocket_messaging.py 已正确实施。 (查看第 149 行)

我是 运行 web2py 2.11.2,所以不知道这是否是预期的,我也通过 web2py 管理界面更新,而不是通过下载新的 web2py 实例。不确定这是否会导致问题。

无论如何都解决了!