我可以不断的从服务器推数据到同一个连接?服务器、数据

2023-09-10 20:58:02 作者:本宫受宠若惊

我设置了​​一个股票,股票,如Web页面,用JavaScript编写的。我也写了服务器的页面会谈,在C ++中。

I'm setting up a stock-ticker like web-page, written in JavaScript. I am also writing the server that the page talks to, in C++.

我想使网页有效,例如,它使发送一个订阅消息给应用服务器,然后保存保持活动连接打开,不断地接收来自应用服务器推入站数据。

I would like to make the web page efficient, such that it makes sends single subscription message to the app server, and then holds a keep-alive connection open, constantly receiving inbound data pushed from the app server.

目前,我每天我收到的数据从服务器的时间来重新发出Web客户端订阅电话。的问题是,每个XHR对象击中readyState的时间(4),该呼叫被有效地完成。任何到达后此网页数据被忽略。 web的客户机可以重新发送数据,并将复位的对象,但在发送是不必要的,并且只被用于重置XHR对象

At the moment, I have to re-issue the web clients' subscription call every time I receive data from the server. The problem is that each time the XHR object hits readyState(4), the call is effectively completed. Any data arriving at the web page after this is ignored. The web-client can resend data and that resets the object, but the send is unnecessary, and is only being used to reset the XHR object.

我想知道是否有可能以某种方式重新设置现有XHR对象,并把它放入其中它期望更多的入站数据,这样,当更多的数据被推向所述网页的状态下,该网页做出响应,并处理它。

I would like to know if it is possible to somehow reset the existing XHR object, and put it into a state where it expects more inbound data, so that when more data is pushed to the web page, the web page responds and processes it.

在此先感谢您的帮助,你可以给。注意:不使用JQuery在这个项目上

Thanks in advance for any help you can give. Note: Not using JQuery on this project.

推荐答案

HTTP是一个一次性的协议:一个请求,一个响应,就大功告成了。如果你想保持连接打开,您可以使用的WebSockets( MDN参考页 ,客户端code例如)。但是认为它不会对旧的浏览器支持(IE为例,刚开始支持10版的WebSockets),所以你可能需要实现使用XHR和的长轮询。

HTTP is a one-off protocol: one request, one response, and you're done. If you want to keep a connection open, you can use Websockets (MDN reference page, client-side code example). However consider it won't be supported on older browsers (IE, for example, just started supporting Websockets on version 10), so you'll probably need to implement a fallback using XHR and long-polling.

 
精彩推荐
图片推荐