简体   繁体   English

每隔一段时间向 websockets 发送消息

[英]sending message to websockets on interval

I'm trying to build a "view recent searches" functionality into a website I have.我正在尝试将“查看最近搜索”功能构建到我拥有的网站中。 Essentially, I would like to display a new recent search on the website every 5 seconds.本质上,我想每 5 秒在网站上显示一个新的最近搜索。

What is the best way to do this?做这个的最好方式是什么? I was thinking of having each page connect to my server with socket.io, then broadcast a message to all sockets every 5 seconds about new searches.我正在考虑让每个页面都使用 socket.io 连接到我的服务器,然后每 5 秒向所有 sockets 广播一条关于新搜索的消息。 On the client-side, upon receiving the message from the server on the socket, i'll use some client-side javascript to update the recent searches part of the webpage.在客户端,在套接字上收到来自服务器的消息后,我将使用一些客户端 javascript 来更新网页的最近搜索部分。

Is the best way to do this?这是最好的方法吗? If so, how should I implement it?如果是这样,我应该如何实施?

I am using node.js/express.我正在使用 node.js/express。

Something like this...像这样的东西...

var io = require('socket.io').listen(3001);

// Send to Everyone
setTimeout(function() {
  io.sockets.send('message');
}, 5000 );

// Send to Everyone on channel test123
setTimeout(function() {
  io.sockets.emit('test123', {test: "success!"} );
}, 5000 );

(Untested, but should work. Requires Socket.IO 0.7.x or newer. Let me know how this goes =] . (未经测试,但应该可以工作。需要 Socket.IO 0.7.x 或更高版本。让我知道这是怎么回事=]

Do note, however, that this is not the most optimal way of handling this.但是请注意,这不是处理此问题的最佳方式。 It would be best if you used events to signal your server to send new data to the client.最好使用事件来通知服务器向客户端发送新数据。 For example, when you add new data to your database, at that time (and that time only) send new data to the client.例如,当您将新数据添加到数据库时,那时(并且仅在那个时候)将新数据发送到客户端。 It's a bit silly & inefficient to check every 5 seconds when you can design around it.当您可以围绕它进行设计时,每 5 秒检查一次有点愚蠢且效率低下。 The whole point of Socket.IO is to push, not to poll. Socket.IO 的重点是推送,而不是轮询。 New technologies require a new way of thinking.新技术需要新的思维方式。

Also, very important about displaying recent searches: These links are typically NEVER displayed for users, as they're often too random to be useful.此外,显示最近的搜索非常重要:这些链接通常从不向用户显示,因为它们通常太随机而无用。 Top searches, maybe, but that information wouldn't change regularly enough to warrant such a frequent refresh.可能是热门搜索,但这些信息不会经常变化,不足以保证如此频繁的刷新。 In addition, most people use this technique to get their website listed for page for which they have no true content.此外,大多数人使用这种技术来让他们的网站列出他们没有真实内容的页面。 An important thing to note is that if you allow search engines to these pages (by not blacklisting your search results pages in robots.txt ), you run a VERY high risk of getting a low-quality flag from Google.需要注意的重要一点是,如果您允许搜索引擎访问这些页面(通过不在robots.txt中将您的搜索结果页面列入黑名单),您将面临从 Google 获得低质量标志的非常高的风险。

My most sincere advice: While this may be a cool feature to have, especially for an admin console, it is utterly useless for visitors and can prove to be a detriment to your website's SEO value by adding to Google's index low quality content and loads of links going nowhere useful.我最真诚的建议:虽然这可能是一个很酷的功能,尤其是对于管理控制台,但它对访问者完全没用,并且可以通过添加到谷歌的索引中来证明对您网站的 SEO 价值的损害 低质量的内容和负载链接无用。 I wouldn't even display it.我什至不会显示它。

If you are picking a set time like 5 seconds, then I don't believe you will need to set up socket io.如果您选择 5 秒这样的设定时间,那么我认为您不需要设置套接字 io。 Simply do a setInterval and ask the server every 5 seconds for new recent searches.只需执行 setInterval 并每 5 秒向服务器询问一次新的最近搜索。 This will also mean that the calls for recent searches won't be synchronized, making sure the the server doesn't have to send updates to everyone at once (as would be with socket.io and broadcasting new recent search messages)这也意味着最近搜索的调用不会同步,确保服务器不必立即向每个人发送更新(就像 socket.io 和广播新的最近搜索消息一样)

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

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