本文介绍了pythonanywhere - 如何根据 web2py 消息传递示例使用 websockets 传输消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我构建了一个应用程序来测试带有 web2py 和 pythonanywhere 的 websockets,我让它在本地工作,但是当上传到 pythonanywhere 时它不起作用.我认为原因是我将东西发送到本地主机(127.0.0.1),但不知道上传时我需要在哪里发送东西(和收听).

debug.py 脚本是:

def 监听():脚本=脚本('''jQuery(文档).ready(函数(){var callback=function(e){$("#test_div").html(e.data)};if(!web2py_websocket('ws://127.0.0.1:8880/realtime/mygroup',callback))alert("您的浏览器不支持 html5 websocket,请尝试使用谷歌浏览器");});''')d = ''返回 { 'd':d , 'script':script }定义发送():form=LOAD('debug','ajax_form',ajax=True)返回{'形式':形式}def ajax_form():form=SQLFORM.factory(Field('message'))如果 form.accepts(request,session):导入 websocket_messaging重新加载( websocket_messaging )websocket_messaging.websocket_send( 'http://127.0.0.1:8880' , form.vars.message , 'mykey' , 'mygroup' )退货单

listen.html

{{extend 'layout.html'}}<div id="test_div">{{=d}}

{{=脚本}}

send.html

{{extend 'layout.html'}}{{=表格}}

还有几点:

  • 我按照这个 来自 pythonanywhere 中的 bash 终端.

    python websocket_messaging.py -p 8880 -k mykey

  • 我在 pythonanywhere 上有一个免费帐户,我想在开始支付之前测试一下.

  • 我发现 this 这似乎暗示我不能做什么我想在任何地方使用 python?

解决方案

PythonAnywhere dev here -- 不幸的是,WebSockets 目前不适用于我们的服务 :-(

So I've built out an app to test websockets with web2py and pythonanywhere and I got it to work locally but when uploading to pythonanywhere it doesn't work. I think the reason is that I'm sending things to localhost (127.0.0.1) but have no idea where I need to send things (and listen to) when uploading.

the debug.py script is:

def listen():

    script=SCRIPT('''
         jQuery(document).ready(function(){
           var callback=function(e){$("#test_div").html(e.data)};
    if(!web2py_websocket('ws://127.0.0.1:8880/realtime/mygroup',callback))
             alert("html5 websocket not supported by your browser, try Google Chrome");
         });
     ''')
     d = ''
     return { 'd':d , 'script':script }

def send():
    form=LOAD('debug','ajax_form',ajax=True)
    return { 'form':form }

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

the listen.html

{{extend 'layout.html'}}

<div id="test_div">
    {{=d}}
</div>

{{=script}}

the send.html

{{extend 'layout.html'}}

{{=form}}

A few more points:

  • I start the tornado server as per this from a bash terminal in pythonanywhere.

    python websocket_messaging.py -p 8880 -k mykey
    

  • I've got a free account on pythonanywhere and would like to test this out before I start paying for one.

  • I found this which seems to imply I can't do what I want on pythonanywhere?

解决方案

PythonAnywhere dev here -- unfortunately WebSockets don't work on our service at the moment :-(

这篇关于pythonanywhere - 如何根据 web2py 消息传递示例使用 websockets 传输消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 00:04