龙卷风Websocket与单独的线程/应用程序的交互
我正在尝试通过单独的服务器线程在龙卷风上进行交互或发送消息。任何帮助将不胜感激。
这是一些代码
waiters = set()
class MainHandler(tornado.web.RequestHandler):
def get(self):
print ("[HTTP](MainHandler) User Connected.")
self.render("index.html")
class WSHandler(tornado.websocket.WebSocketHandler):
cache = []
cache_size = 200
def open(self):
global waiters
print(list(waiters))
print ('[WS] Connection was opened.')
waiters.add(self)
print(list(waiters))
def on_message(self, message):
print ('[WS] Incoming message:'), message
if message == "toggle":
global waiters
for client in self.waiters:
print("fired")
def on_close(self):
global waiters
print(list(waiters))
print ('[WS] Connection was closed.')
waiters.remove(self)
print(list(waiters))
@classmethod
def client_message_from_program(cls, JSONmsg):
print("JSONmsg") #This does not print using add_callback
print(JSONmsg)
for client in cls.waiters:
print('sent to client')
client.write_message(json.dumps(JSONmsg))
单独的线程代码,希望与Wshandler进行交互/接口 loopdeclaration引用了正确的龙卷风.ioloop.ioloop.instance()
msg_json={"type":"StatusUpdate","message":"Connected","message1":"123412","Message2":"Calculating RSSI","Message3":""}
LoopDeclaration.add_callback(WSHandler.client_message_from_program, msg_json)
这不会引起例外,但不会引起wshandler的任何进一步行动。没有消息传递。
创建客户的全局列表并以这种方式发送消息将不起作用,或者在测试中不适合我。 (全球列表不会在Wshandler之外进行更新,在龙卷风5.0及以后的wshandler之外发送消息)
将不胜感激。一些旧线程提到在龙卷风WS上以“特殊客户端”为“特殊客户端”的遗产线程将是最好的方法,但我不知道如何实现这一目标,并且无法找到前进的路径。谢谢。
I am trying to interact or send messages to clients on tornado from a separate server thread. Any help would be appreciated.
Here is some code
waiters = set()
class MainHandler(tornado.web.RequestHandler):
def get(self):
print ("[HTTP](MainHandler) User Connected.")
self.render("index.html")
class WSHandler(tornado.websocket.WebSocketHandler):
cache = []
cache_size = 200
def open(self):
global waiters
print(list(waiters))
print ('[WS] Connection was opened.')
waiters.add(self)
print(list(waiters))
def on_message(self, message):
print ('[WS] Incoming message:'), message
if message == "toggle":
global waiters
for client in self.waiters:
print("fired")
def on_close(self):
global waiters
print(list(waiters))
print ('[WS] Connection was closed.')
waiters.remove(self)
print(list(waiters))
@classmethod
def client_message_from_program(cls, JSONmsg):
print("JSONmsg") #This does not print using add_callback
print(JSONmsg)
for client in cls.waiters:
print('sent to client')
client.write_message(json.dumps(JSONmsg))
Separate thread's code, hoping to interact/interface with WSHandler
LoopDeclaration is referencing the correct tornado.ioloop.IOLoop.instance()
msg_json={"type":"StatusUpdate","message":"Connected","message1":"123412","Message2":"Calculating RSSI","Message3":""}
LoopDeclaration.add_callback(WSHandler.client_message_from_program, msg_json)
This does not cause an exception, but it does not cause any further action from the WSHandler. No messages are passed.
Creating global list of clients and sending message that way will not work, or has not for me in testing. (global list would not update outside of WSHandler, sending messages outside of WSHandler unsupported in tornado 5.0 and beyond)
Any support would be appreciated. Some legacy threads mention starting my web-application as a 'special client' on the tornado WS would be the best method, but I have no idea how to accomplish that and have been unable to find a path forward. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
其他Stackoverflow建议创建一个“特殊客户”,但有关此技术成就的信息最少。我在这里添加我的解决方案,以帮助他人遇到同样的问题。
相关的服务器代码:
应用程序/特殊客户端,
我敢肯定有更好的方法可以做到这一点,如果有人提供未来的建议,请添加它们。我对酒吧/子模型感兴趣,但没有时间足够学习...
Other stackoverflow suggested creating a 'special client' but had minimal info on technical accomplishments of this. I'm adding my solution here to aid others if they encounter the same problem.
Relevant Server Code:
Application/Special Client
I'm sure there are better ways to do this, if anyone has advice for future please add them. I'm interested in the PUB/SUB model but didn't have time to adequately learn...