简体   繁体   English

从 js 中的 websocket 读取数据

[英]reading data from websocket in js

i have a service, which uses QWebSocketServer.我有一个服务,它使用 QWebSocketServer。 My server is able to handle client requests and send several events to client (without any request, it is important).我的服务器能够处理客户端请求并向客户端发送多个事件(没有任何请求,这很重要)。 I've tested my server with QWebSocket class like shown in Qt example.我已经使用 QWebSocket 类测试了我的服务器,如 Qt 示例中所示。 All works perfectly.一切正常。

Now i want to implement frontend in js.现在我想在 js 中实现前端。 And i've faced with one thing which i cannot understand.我遇到了一件我无法理解的事情。 If client sends request to server, server's answer can be received on client side, but if server sends data without client request, i'm not able to receive this data.如果客户端向服务器发送请求,客户端可以收到服务器的应答,但是如果服务器发送数据而没有客户端请求,我将无法接收此数据。

In my js script i have usual code:在我的 js 脚本中,我有通常的代码:

websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };

So, why i can't receive data from server without request and what should i do for getting the ability to receive that?那么,为什么我无法在没有请求的情况下从服务器接收数据,我该怎么做才能获得接收数据的能力?

服务器一直在从另一个线程发送消息,这是问题的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM