如何使用socket.io实现长时间轮询? [英] How to implement long polling with socket.io?

查看:487
本文介绍了如何使用socket.io实现长时间轮询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我已经在node.js中创建了一个应用程序,以将socket.io与mysql发送通知给所有客户端.因此,我的mysql数据库中的任何更改都将立即通过socket.io通知给多个客户端.该应用程序的工作魅力.

Currently, i have created an application in node.js to send notification to all client using socket.io with mysql. So any change in my mysql database will be notified to the multiply clients through socket.io instantly. The application work charm.

我面临的问题与性能问题有关.

The problem i'm facing is related to performance issue.

我的浏览器从服务器获取持续的推送通知,这会降低我的浏览器的性能. I/客户端仅在数据库更改时需要获取通知,其余时间服务器应处于暂停状态,而不会向cilent发送任何消息.

My browser get the constant push notification from the server, which slows down the performance of my browser. I/Client need to get the notification only on the database change, rest of the time the Server should be in pause state, with out sending any message to cilent.

换句话说,我需要实施长轮询技术.

In other words i need to implement long polling technique.

如何使用socket.io实现长时间轮询?

How do i implement long polling with socket.io?

我已经在Google上搜索了一个示例,但是我发现没有一个示例可以帮助我在node.js中使用socket.io和mysql进行长时间轮询

I have googled to look for an example, but i found none which could help me to implement long polling with socket.io and mysql in node.js

是否可以使用socket.io进行长时间轮询?

Is it possible to implement long polling with socket.io?

如果是这样,有人可以将我重定向到带有示例的有用链接吗?

If so, could somebody redirect me to some useful link with example?

谢谢.

推荐答案

您可以强制socket.io仅使用长轮询:

You can force socket.io to use only long-polling:

io.set('transports', ['jsonp-polling']);

但是我不认为长轮询是您正在寻找的解决方案.您应该独立于所使用的基础技术.我可能会做这样的事情:

But I don't think long-polling is the solution you are looking - for. You should be independet from the underlying technique used. I might do something like this:

function writeToMysqlDb(data) {
    mySqlDriver.write(data); //whatever you use to write data to MySql
    socket.emit('dbChanged', data); //send the same data to all connected clients
};

我将传输方式保留为默认选项:将默认传输方式传输到websocket,htmlfile,xhr-polling,jsonp-polling"

I would leave the transports to the default options: "transports defaults to websocket, htmlfile, xhr-polling, jsonp-polling"

这篇关于如何使用socket.io实现长时间轮询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆