- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发一个应用程序来从科学仪器中读取一些信息并使用远程网络客户端显示这些信息。我用 Flask 和 Flask-SocketIO 实现了这一点。我有一个主 application.py
文件,其中包含一个名为 app
的 Flask 实例。我将它与 Flask-SocketIO
一起使用。 Flask 部署的 HTML 页面包含一个与服务器应用程序通信的 Javascript Socketio 实例(在 python 中)。
此外,我还有一个代表我的科学仪器的外部模块。我想从网站(使用 Javascript)接收事件到这个模块类。我尝试将 app
和 socketio
python 对象传递给构造函数,以便从 Flask app
请求上下文。这非常有效,仅适用于从模块发出事件。
简而言之,application.py
具有以下基本结构:
from gevent import monkey
monkey.patch_all()
from flask import Flask, Response, render_template, session, request, g
from flask_socketio import SocketIO, emit, disconnect, Namespace
from Instrument import *
app = Flask(__name__)
app.debug = True
socketio = SocketIO(app)
# some stuff here with @app.routes
@app.route('/instrument')
def inst(arg):
t = Instrument(arg, app, socketio)
if __name__ == '__main__':
socketio.run(app, port=8000, log_output=True)
对于外部模块 Instrument.py
我尝试使用装饰器语法失败(有点像 @self.socketio.on
,我不太熟悉它们) .所以我尝试了以下代码:
class Instrument():
def __init__(self, arg, app, socketio):
self.app = app
self.socketio = socketio
self.socketio.on_event('someCoolEvent', self.someCoolMethod, namespace='/coolapp') # THIS GIVES ME ERRORS
def someCoolMethod():
with self.app.test_request_context('/coolapp'):
self.socketio.emit('emittedEvent', namespace='/telescope') # THIS WORKS FINE
我收到的错误如下(已编辑:添加了两行有用的内容):
[2017-03-03 06:31:31,456][INFO] - _handle_event: received event "my event" from a40724e9e60e4a61ace9e19e59ceabda [/telescope]
[2017-03-03 06:31:31,604][INFO] - handle_get_request: a40724e9e60e4a61ace9e19e59ceabda: Received request to upgrade to websocket
[2017-03-03 06:31:33 +0000] [6333] [ERROR] Error handling request /socket.io/?EIO=3&transport=websocket&sid=24e7d134ea4c4840888c28e0e3ff1f6d
Traceback (most recent call last):
File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/gunicorn/workers/async.py", line 52, in handle
self.handle_request(listener_name, req, client, addr)
File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/gunicorn/workers/ggevent.py", line 152, in handle_request
super(GeventWorker, self).handle_request(*args)
File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/gunicorn/workers/async.py", line 103, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/flask/app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/flask_socketio/__init__.py", line 42, in __call__
start_response)
File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/middleware.py", line 47, in __call__
return self.engineio_app.handle_request(environ, start_response)
File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/socketio/server.py", line 353, in handle_request
return self.eio.handle_request(environ, start_response)
File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/server.py", line 260, in handle_request
environ, start_response)
File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/socket.py", line 86, in handle_get_request
start_response)
File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/socket.py", line 127, in _upgrade_websocket
return ws(environ, start_response)
File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/async_gevent.py", line 34, in __call__
raise RuntimeError('You need to use the gevent-websocket server. '
RuntimeError: You need to use the gevent-websocket server. See the Deployment section of the documentation for more information.
应用程序初始化时,日志显示:
2017-03-04 02:52:04 [23530] [INFO] Starting gunicorn 18.0
2017-03-04 02:52:04 [23530] [INFO] Listening at: http://127.0.0.1:8000 (23530)
2017-03-04 02:52:04 [23530] [INFO] Using worker: gevent
2017-03-04 02:52:04 [23534] [INFO] Booting worker with pid: 23534
所以我尝试使用 gunicorn -k gevent -w 1 module:app
和 gunicorn - 配置文件
但我遇到了同样的错误。/etc/init/myapp.conf
k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 module:app
在浏览器中我看到另一个错误,它与“主要”错误同时出现:
WebSocket connection to 'ws://somewebpage.com:2080/socket.io/?
EIO=3&transport=websocket&sid=572923d1b8fd402795bba50823941520'
failed: Error during WebSocket handshake: Unexpected response code: 500
如何正确接收来自外部模块的事件?也许有更好的方法可以达到我想要的结果。如果有人帮我解决这个问题,我将不胜感激。
最佳答案
我自己回答。我找到了 following post ,这解释了错误:
WebSocket connection to 'ws://somewebpage.com:2080/socket.io/?
EIO=3&transport=websocket&sid=572923d1b8fd402795bba50823941520'
failed: Error during WebSocket handshake: Unexpected response code: 500
是由于nginx配置文件配置错误导致的。应该是这样的:
...
location / {
proxy_pass http://127.0.0.1:8000;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $http_host;
}
...
现在就像一个魅力:从导入的模块和主 python 脚本发出和接收事件。
关于python - flask 和 flask SocketIO : receiving an event from inside of an external module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42572829/
当检测鼠标x和y坐标时,最好像这样使用event.clientX和event.clientY: function show_coords(event){ var x=event.clientX;
我有以下代码: document.oncontextmenu = function(evt) { evt = evt || window.event; console.log(evt.
对于另一个问题,我遇到了一个似乎偶尔出现在 SO 的误解。一些提问者似乎认为触发器之于数据库就像事件之于 OOP 一样。 有没有人有一个很好的类比来解释为什么这是一个有缺陷的比较,以及误用它的后果?
$('body').keypress(function(event){ if(event.keyCode == 46){console.log('Delete Key Pressed')};
我正在制作一个“流体”文本区域,它根据内容调整其高度。我实际上正在尝试实现 this脚本。我有以下代码:https://ellie-app.com/Vjtvm6yrKWa1/4 问题是,当增加高度时,
我使用 Raphael .mouseover() 和 .mouseout() 事件来突出显示 SVG 中的某些元素。这工作正常,但在我单击一个元素后,我希望它停止突出显示。 在 Raphael doc
我目前正在开发一个应用程序,允许人们为在线广播电台安排“节目”。 我希望用户能够设置重复事件,例如:- “躁狂星期一”节目 - 每周一 9 点至 11 点“月中疯狂” - 每个月的第二个星期四“本月新
我有以下三个表格(简化版本): 已加载关卡: id(整数、主键、自动增量) globalId(整数,键) 日期(日期时间、键) serverId(Int,键) gamemodeId(Int,Key)
在我阅读 Gevent Tutorial 之后,我有一个关于 gevent.event.Event 的问题。 Event.set() 是否会唤醒所有被 Event.wait() 阻塞的函数? 就像下面
我对 cakephp ver3.1.3 没有经验 我按照说明实现了登录认证功能; http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-
现在,我发送 10 个事件,每个事件有 1 个属性。但是当我想过滤特定事件并按属性选择事件时,在“事件属性”过滤器中仅显示前 7 个事件,而我为其余事件添加的事件仅显示“第一次”过滤器,为什么? 最佳
我不知道我的 Firefox 发生了什么! 我的aspx和javascript代码是这样的: function a() { alert(
中有3个事件fns重装 ,我可以对两者做同样的事情 reg-event-db和 reg-event-fx . reg-event-db之间的主要区别是什么, reg-event-fx和 reg-eve
我遇到了 Firefox keydown 行为,因为在没有聚焦于特定字段的情况下按下 Enter 键(实际上是任何键)不会触发 keydown 事件只会触发`按键事件。 这可能会非常令人困惑,因为 k
这是我的代码片段 public class Notation : INotifyPropertyChanged { public event PropertyChangedEventHandl
我可以在一个 Jsf2 xhtml 文件中有多个标签吗? 在那种情况下,关联的监听器将以什么顺序被调用? Mojarra 2.1.1/Apache Tomcat 7.0.22/PrimeFaces 3
我可以在一个 Jsf2 xhtml 文件中有多个标签吗? 在那种情况下,关联的监听器将以什么顺序被调用? Mojarra 2.1.1/Apache Tomcat 7.0.22/PrimeFaces 3
我有以下 JavaScript: $('#ge-display').click(function (event) { window.open('/googleearth/ge-display.ph
我需要确定触发事件的元素。 使用 event.target 获取相应的元素。 我可以从那里使用哪些属性? 引用 编号 节点名 我找不到关于它的大量信息,即使在 jQuery 上也是如此页,所以希望有人
我在pyGame中创建了一个Asteroidz克隆,并在pygame.vent.get()循环中有两个for Event,一个用于检查退出请求,以及游戏是否应该通过按空格键开始,然后在游戏中进一步尝试
我是一名优秀的程序员,十分优秀!