gpt4 book ai didi

Python 和 Socket.IO - 应用程序在连接后挂起

转载 作者:太空宇宙 更新时间:2023-11-03 16:37:57 25 4
gpt4 key购买 nike

我正在开发一个Python应用程序。基本实现需要将 Raspberry Pi 连接到主服务器并共享消息。服务器将运行 python 应用程序来分析数据并将信息返回到 Raspberry Pi。

我使用 python-socketio 作为服务器部分,使用 socketio.client 作为 Raspberry Pi 代码来实现该系统。

我的问题是我能够连接到服务器并收到响应消息,但程序在客户端挂起。

我的服务器代码是:

import socketio
import eventlet
from flask import Flask

# Define Socket.IO server and application wrapper
sio = socketio.Server()
app = Flask(__name__)

@sio.on('connect', namespace = '/test')
def connect(sid, environ):
print 'New Connection ' + sid
sio.emit("server_response", sid, room = sid, namespace = '/test' )

@sio.on('message', namespace = '/test')
def message(sid, data):
print 'message ' + sid + " " + data
sio.emit("server_reply", data = "Hello", room = sid, namespace = '/test')

@sio.on('disconnect', namespace = '/test')
def disconnect(sid):
print 'disconnect ' + sid

if __name__ == '__main__':
# Wrap Flask application with Socket.IO's middleware
app = socketio.Middleware(sio, app)

# Deploy as an eventlet WSGI server
eventlet.wsgi.server(eventlet.listen(('',8000)), app)

至于客户端部分,我正在使用以下代码:

from socketIO_client import SocketIO, BaseNamespace
global sio
global testNamespace
global myId

class testNamespace(BaseNamespace):

def on_server_response(self, userID):
myId = userID
print "Socket connection accepted"
print "I was assigned the id: " + myId

def on_server_reply(self, data):
print "message response: " + data

if __name__ == "__main__":
# Establish the connection
sio = SocketIO('localhost', 8000)
testNamespace = sio.define(testNamespace, '/test')

print "Going to emit message"
testNamespace.emit("message", "Hello")

sio.wait()

由于某种原因,我收到带有指定 ID 的服务器响应,但程序永远不会到达该行

print "Going to emit message"

我最初认为这与命名空间有关,但由于打印指令从未发生,我不明白我做错了什么。

编辑

我注意到,如果 on_server_response 我尝试发出消息 Hello with

self.emit("message", "Hello")

我能够将消息发送到服务器并获得回复。我现在不明白的是,如何在类定义之外的主程序上发出事件。

最佳答案

我发现了同样的问题,并在 SocketIO_client 上提出了一个问题。

https://github.com/invisibleroads/socketIO-client/issues/117

这是一个覆盖子类中有问题的部分的解决方案。然后就用这个SocketIOClient代替SocketIO

from socketIO_client import SocketIO

class SocketIOClient(SocketIO):
"""
Fix for library bug
"""

def _should_stop_waiting(self, for_connect=False, for_callbacks=False):
if for_connect:
for namespace in self._namespace_by_path.values():
is_namespace_connected = getattr(
namespace, '_connected', False)
#Added the check and namespace.path
#because for the root namespaces, which is an empty string
#the attribute _connected is never set
#so this was hanging when trying to connect to namespaces
# this skips the check for root namespace, which is implicitly connected
if not is_namespace_connected and namespace.path:
return False
return True
if for_callbacks and not self._has_ack_callback:
return True
return super(SocketIO, self)._should_stop_waiting()

关于Python 和 Socket.IO - 应用程序在连接后挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37058119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com