- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在我的项目中,我使用了 React 前端和带有 RESTful API 的 Flask 服务器。基本功能是前端从服务器获取数据并显示。这工作正常,但我想我会通过让客户端在服务器从其他地方接收到新数据时自动重新获取来改进它。我可以使用轮询并每隔一段时间发出一个获取请求,但这似乎是 WebSockets 的完美用例。因此,我正在尝试在服务器和客户端之间实现一个 websocket 连接,其中服务器在某些事件发生时向客户端发送消息。
我的服务器是用 python 和 Flask 编写的,使用特定于 Flask 的 websocket 库似乎是个好主意。我最终使用了 flask-socketio ,但我一直在让它工作时遇到问题。我的服务器相关部分如下:
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
... # Server functionality for receiving and storing data from elsewhere, not related to the websocket
# Handle the webapp connecting to the websocket
@socketio.on('connect')
def test_connect():
print('someone connected to websocket')
emit('responseMessage', {'data': 'Connected! ayy'})
# Handle the webapp connecting to the websocket, including namespace for testing
@socketio.on('connect', namespace='/devices')
def test_connect2():
print('someone connected to websocket!')
emit('responseMessage', {'data': 'Connected! ayy'})
# Handle the webapp sending a message to the websocket
@socketio.on('message')
def handle_message():
print('someone sent to the websocket')
# Handle the webapp sending a message to the websocket, including namespace for testing
@socketio.on('message', namespace='/devices')
def handle_message2():
print('someone sent to the websocket!')
@socketio.on_error_default # handles all namespaces without an explicit error handler
def default_error_handler(e):
print('An error occured:')
print(e)
if __name__ == '__main__':
socketio.run(app, debug=True, host='0.0.0.0')
对于前端,我最初也尝试使用库。我选择了react-websocket .
<Websocket
url={'ws://localhost:5000'} // I also tried /devices at the end of this url
onMessage={() => console.log('Received message through websocket.')}
debug={true}
/>
但是,这个解决方案在连接后立即断开连接,这意味着出现了问题。不过,要找出到底出了什么问题似乎是不可能的;尽管 debug={true}
,但我在日志中没有任何指示。我没有使用这个解决方案,而是为前端换用了一个更自定义的 WebSocket 解决方案:
componentDidMount() {
moment.locale('nb');
jsonRequest(irrelevant url).then(jsonResponse => {
... // Handle the results of the initial fetch
const socket = new WebSocket('ws://localhost:5000'); // I also tried /devices at the end of this url
socket.onopen = () => console.log('opened custom socket');
socket.onclose = () => console.log('closed custom socket');
socket.onmessage = e => console.log('got message');
socket.onerror = e => console.log('websocket error');
socket.addEventListener('open', function(event) {
console.log('sending');
socket.send('Hello Server!');
});
socket.addEventListener('message', function(event) {
console.log('Message from server ', event.data);
});
this.socket = socket;
this.setState(...);
});
}
Rendering...
是我放在渲染函数最顶部的控制台语句。
所以在这两种方案中,客户端连接后立即断开websocket连接。我的服务器也没有注意到任何东西。没有一个事件处理程序触发 - 不是 @socketio.on('connect')
,不是 @socketio.on('message')
甚至 @socketio .on_error_default
。因此,我发现自己遇到了障碍。我如何从这里调试?可能是什么问题?我在 Ubuntu 服务器上尝试过完全相同的代码,所以我认为本地主机不是问题。
最佳答案
您将 Socket.IO 与 WebSocket 混淆了。 Socket.IO 协议(protocol)建立在 WebSocket 和 HTTP 之上。您的连接失败是由于您使用普通的 WebSocket 客户端连接到 Socket.IO 服务器造成的。你需要使用 Socket.IO 客户端,比如 this one .
关于javascript - reactjs 客户端和 flask-socketio 服务器之间的 WebSocket 连接未打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46245643/
我想在一些计算机之间建立点对点连接,这样用户就可以在没有外部服务器的情况下聊天和交换文件。我的第一个想法如下: 我在服务器上创建了一个中央 ServerSocket,所有应用程序都可以连接到该服务器。
我正在 Unity 中构建多人游戏。为此,我必须将一些值从客户端发送到两个或多个通过服务器连接的客户端。我想将其构建为服务器真实游戏。客户端将使用 Android,他们的数据将通过服务器同步(可能是一
练习 C 网络编程:我正在编写一个简单的 TCP 客户端-服务器应用程序,它应该将消息(在每个客户端的单独线程中)作为字符串从服务器发送到客户端并在客户端(稍后将成为控制台商店应用程序)。我首先发送消
我使用证书身份验证设置了 AWS Client VPN。我正在为客户端-客户端访问系统进行设置,基本上如 this AWS scenario/example 中所述.一切正常,如果我知道他们的 IP
我正在开发一个小型客户端1/客户端2、服务器(线程)TCP 游戏。在尝试处理延迟问题时,我意识到我的 transmitState() 中存在缺陷。它强制将不必要的信息传递到通讯流中,从而造成迟缓,将汽
来自文档:Configurable token lifetimes in Azure Active Directory (Public Preview) 它提到“ secret 客户端”,刷新 tok
Apollo 客户端开发工具无法连接到我的应用程序。我已在 ApolloClient 构造函数中将 connectToDevTools 传递为 true,但没有任何 react 。我也试过this p
我想在 Pod 内使用 Fabric8 kubernetes 客户端 (java)。如何获取部署集群的 kubernetes 客户端? 我可以使用该集群的 kubeconfig 文件获取任何集群的配置
我正在阅读 the security issue with Log4j我了解此产品受此漏洞影响。但是 Oracle 客户端 11.2 和 12 是否受此问题影响? 我找不到这些产品是否使用任何 Log
Eureka 服务器设置 pom.xml 1.8 Hoxton.SR1 org.springframework.cloud spring
我有一个点对点(客户端/服务器)设置(通过本地 LAN),它使用 Netty,一个 Java 网络框架。我使用原始 TCP/IP(例如,没有 HTTP)进行通信和传输。现在,根据要求,我们希望转向 T
上一篇已经实现了ModbusTcp服务器和8个主要的功能码,只是还没有实现错误处理功能。 但是在测试客户端时却发现了上一篇的一个错误,那就是写数据成功,服务器不需要响应。 接下来要做的就是实现Modb
有没有办法将二维十六进制代码数组转换为 png 图像? 数组看起来像这样(只是更大) [ [ '#FF0000', '#00FF00' ], [ '#0000FF'
我是套接字编程的新手。每次我运行客户端程序时,它都会说“无法连接到服务器”。谁能告诉我我在哪里犯了错误。任何帮助将不胜感激。 这是client.c #include #include #inclu
我们在UNIX环境下制作了简单的client.c和server.c程序。我们使用它来传输一个简单的文本文件,首先打开它,然后读取它并使用 open、read 和 send 系统调用发送;在客户端,我接
当我的程序来自 my previous question正在响应客户端,它应该发送加密消息。 当客户端连接时,它会发送一条类似“YourMessage”的消息。现在我想做的是,当客户端连接时,应该以某
我正在使用 C 和 putty 编写客户端/服务器程序。两个 c 文件位于同一系统上。 我目前在向客户端写回其正在使用的框架以及打印我的框架时遇到问题。它打印出 3 0 9 8,但随后开始打印 134
我正在使用 C 中的 select() 制作一个模拟快餐或其他任何东西的客户端服务器。 我有客户随机点 1-5 种“食物”。服务器每 30 秒决定一次。所有客户最喜欢的食物是什么?他为那些客户提供服务
对于单机游戏,基本的游戏循环是(来源:维基百科) while( user doesn't exit ) check for user input run AI move enemies
1、CentOS安装TortoiseSVN 复制代码 代码如下: yum install -y subversion 2、SVN客户端命令
我是一名优秀的程序员,十分优秀!