使用node.js和socket.io每秒广播一次(Using node.js and socket.io to broadcast every second)

我目前正在使用node.js和socket.io处理拍卖脚本。 在拍卖期间,该网站将有500-1000名登录用户查看单个页面。 任何时候只有一件商品在售,类似于在拍卖行举行的真实拍卖。

我将向从服务器到客户端的所有已登录用户广播倒数计时器。 在服务器端,我将使用1秒的setInterval()倒计时到拍卖结束时间。 除此之外,发送的唯一其他消息将是当前的出价从单个客户端传递到服务器然后广播到所有。 这是一个可靠的方法吗? 它能够处理服务器上的使用吗?

如果没有,有没有更好的方法?

谢谢谢恩


I am currently working on an auction script using node.js and socket.io. The site will have 500-1000 logged in users viewing a single page during the auction. Only one item will be on sale at any one time, similar to a real auction held in an auction house.

I will be broadcasting a countdown timer to all of the signed in users from the server to the client. On the server side I will be using setInterval() of 1 second to countdown to the auction end time. Apart from this the only other message being sent across will be the current bid being passed from a single client to the server then broadcast to all. Will this be a reliable way to do this? And will it be able to handle the usage on the server?

If not is there a way which would be better?

Thanks Shane

2023-03-27 19:03

满意答案

对于计时器值,请在服务器端本身每秒更新本地计时器。 每当有任何用户进来时,给他这个值以及计时器的总值。 然后客户端将根据dandavis评论在本地启动自己的计时器,但在服务器端保留一些间隔,如15或10秒,服务器将广播当前计时器值,以便客户端相应地进行同步。

简而言之,服务器将每隔10(n:你决定)秒后广播,但它将在本地每秒更新定时器变量。 每当客户端进入时,它将获得总计时器值和当前计时器值。

广播当前出价的休息功能可以以正常方式完成。


For timer value, keep updating your local timer per second on server side itself. Whenever any user comes in, give him this value and also total value of timer. Then client will start their own timers locally as per comment by dandavis, but keep some interval like 15 or 10 seconds on server side on which server will broadcast the current timer value so that client will sync accordingly.

In short, server will broadcast every after 10(n:you decide) seconds but it will be updating timer variable per second locally. Whenever client comes in, it will get total timer value and current timer value.

Rest functionality of broadcasting the current bid can be done in normal way.

相关问答

更多

Javascript - Node.js - Socket.io多次触发(Javascript - Node.js - Socket.io multiple firing)

尝试替换此代码: $('.button').click(function() { var gamenumber = $(this).next().val(); socket.emit('challenge', gamenumber, $('.sign' +numerodujeu).val(),$('#screenname').val()); }); 与这一个: $('.button').unbind('click').click(function() { var gamenumber...

如何使用Socket.io防止Node.js中的分布式拒绝服务攻击?(How to protect against distributed denial-of-service attacks in Node.js with Socket.io?)

查看JS事件节流和去抖动! 这些技术将帮助您预防和检测攻击到某一点(这在我看来足够用于一个小型多人套接字游戏)...我只是断开该套接字,如果他们是攻击,哈哈! 如果您对示例代码感兴趣,请告诉我们 编辑: 在这个jsfiddle: http : //jsfiddle.net/y4tq9/9/ var sIO = {}; sIO.on = (function(){ var messages = {}; var speedLimit = 5; //5ms return func...

Node.js和Socket.io出错(Error with Node.js & Socket.io)

该功能不适用于1.0.x版本。 请根据此链接更改您的代码: 获取socket.io中聊天室中有多少人 That function is not available as per 1.0.x version. Please change your code based on this link: getting how many people are in a chat room in socket.io

Node.js w / socket.io:TypeError:Obj在新Manager上没有'on'方法(Node.js w/ socket.io : TypeError: Obj has no method 'on' at new Manager)

您将http模块传递给socket.io而不是http服务器。 var http = require('http'); var httpServer = http.createServer(function (request, response) { request.addListener('end', function () { clientFiles.serve(request, response); }); }).listen(8253); va...

带有node.js的heroku上的Socket.io聊天应用程序(Socket.io chat app on heroku with node.js)

在您的客户端代码中,您尝试连接到端口8080,这与您的服务器正在侦听的端口不一致。 In your client code you're trying to connect to port 8080, which is inconsistent with the port your server is listening on.

使用node.js和socket.io每秒广播一次(Using node.js and socket.io to broadcast every second)

对于计时器值,请在服务器端本身每秒更新本地计时器。 每当有任何用户进来时,给他这个值以及计时器的总值。 然后客户端将根据dandavis评论在本地启动自己的计时器,但在服务器端保留一些间隔,如15或10秒,服务器将广播当前计时器值,以便客户端相应地进行同步。 简而言之,服务器将每隔10(n:你决定)秒后广播,但它将在本地每秒更新定时器变量。 每当客户端进入时,它将获得总计时器值和当前计时器值。 广播当前出价的休息功能可以以正常方式完成。 For timer value, keep updating...

Node.js:TypeError:对象socket.io没有方法'listen'(Node.js: TypeError: Object socket.io has no method 'listen')

尝试这个: var express = require('express'), http = require('http'); var app = express(); var server = http.createServer(app); var io = require('socket.io').listen(server); server.listen(3000); Socket.io在运行时基本上劫持了http对象; express 3.xx不再运行http.server,...

Socket.io和Node.js无法在Heroku上运行(Socket.io and Node.js not working on Heroku)

本教程解释了您的确切需求: https : //devcenter.heroku.com/articles/node-websockets 问题是,在客户端,您正在侦听“localhost:3000”,而您必须侦听部署应用程序的服务器。 下一个代码是连接websocket的常用方法: var HOST = location.origin.replace(/^http/, 'ws') var ws = new WebSocket(HOST); var el = document.getElement...

Node.js和Socket.io添加用户名(Node.js & Socket.io Adding Username)

看看这里 - http://www.tamas.io/2013/05/19/simple-chat-application-using-node-js-and-socket-io/ 最简单的方法是添加一个人的对象 - 请参阅源代码(文章中的链接)。 如果你想要实现房间,请阅读: http : //www.tamas.io/2013/05/19/simple-chat-application-using-node-js-and-socket-io/ 玩的开心。 Have a look here - ...

JS:Node.js和Socket.io - 全局和体系结构(JS: Node.js and Socket.io - globals and architecture)

首先,请不要将用户置于全局变量中,最好将其放在模块中,并在需要时在其他地方使用它。 你可以这样做: users.js var users = { _list : {} }; users.create = function(data){ this._list[data.id] = data; } users.get = function(user_id){ return this._list[user_id]; }; users.getAll = function(){ ...

相关文章

更多

Node.js视频教程

捷训Node.js入门教学视频,对初学者来说应该不错的,教学视频中包括javascript的基本知识的 ...

开源项目列表

ElasticSearch Hessian Akka Socket.IO Node.js Kafka ...

Java:IO/NIO篇,读写属性文件(properties)

1. 描述 尝试用多种方法读取属性文件。 测试打印系统属性; 测试读取、写入用户属性文件; 测试 ...

java socket server

用java编写的一个socket服务端,通过一个tcp测试工具测试这个服务端,发现发送数据给服务端,服 ...

Hadoop core之IO

包:org.apache.Hadoop.io AbstractMapWritable:抽象类,是Map ...

关于java io流关闭的问题

RT,我想问的是,如果不关闭流,会出现什么样不同的情况? 希望大家能多多的给出不同的答案,很想知道! ...

Hadoop Backup Node

要了解 Hadoop Backup Node,要从Namenode的元数据说起。 我们都知道 ...

Hadoop node 部署步骤

1.OS安装 a)RHEL 6.2 X64 i.刻录光盘安装(略) b)安装选项 i.Basic se ...

Java socket通信

是从网上找到的聊天程序 有服务器和客户端, 在注册完之后出现 java.io.StreamCorrup ...

最新问答

更多

绝地求生、荒野行动、香肠派对 哪个更好玩???(都是吃鸡类游戏)

PC上的绝地求生,是最早也是最火的大逃杀游戏。 荒野行动是网易抄袭蓝洞绝地求生制作的手游。相似度90%,还有他一起出的终结折2,这2款正在被蓝洞告,打官司呢。 手游上的绝地求生有2部都是蓝洞授权(收钱)给腾讯开发的正版ID手游。所以跟PC上做的一模一样,蓝洞也没话说。 加上吃鸡国服也是腾讯独家代理,所以根本没有什么可说的。只要这个类型的 过于相似的,腾讯都可以借蓝洞之手起诉。打压同行是国内BAT最爱干的事嘛! 香肠派对画风虽然不一样,但核心玩法还是跟人家正版的一样的,同样也是没有被授权的。 98

如何在jQuery集合中选择第n个jQuery对象?(How to select the nth jQuery object in a jQuery collection?)

你可以使用eq : var rootElement = $('.grid').find('.box').eq(0); rootElement.find('.a'); /* Use chaining to do more work */ You can use eq: var rootElement = $('.grid').find('.box').eq(0); rootElement.find('.a'); /* Use chaining to do more work */

ASP NET使用jQuery和AJAX上传图像(ASP NET upload image with jQuery and AJAX)

您可以自己手动设置FormData键和值。 Upload 创建FormData并设置新的键/值 $("#btnUpload").on("click", function(e) { e.preventDefault(); var file = $("#imguploader").get(0).file

SQL Server XML查询中包含名称空间的位置(SQL Server XML query with namespaces in the where exist)

您可能希望使用#temp.identXml.query而不是#temp.identXml.query 。 您可以在这里阅读更多相关信息SQL Server XML exists() 我相信你也可以像这样使用它 Select #temp.identXml.value('(/*:PersonIdentity/*:MasterIndexes/*:PersonIndex/*:SourceIndex)[1]','varchar(100)') as Ident ,#temp.identXml.value(

宁夏银川永宁县望远镇哪里有修mp5的?

胜利街有家电维修,电脑城,银川商场多得很…

我想用更新的日期标记所有更新的行(I would like to mark all updated rows with the date that they have been updated)

您可以使用更新后触发的触发器来执行此操作。 给出如下表: create table your_table (id int primary key, val int, last_update datetime) 每当您更新表中的内容时,此触发器将设置last_update值。 CREATE TRIGGER trigger_name ON your_table AFTER UPDATE AS BEGIN UPDATE your_table SET your_ta

郑州会计培训班

招生的,至于时间吗,就看你自己的时间段了,你可以致电0371-63300220.他们会帮你选择一下的。离你最近,最专业的培训班。

如何定位数组中的负数,并得到所有正数的总和?(How to target e negative number from an array, and get the sum of all positive numbers?)

只需创建一个条件来检查它是正数还是负数,然后定义一个空的数组negatives ,如果数字是负数,则将其推到负数组中,如果是正数,则将其添加到sum变量中,请查看下面的工作示例。 function SummPositive( numbers ) { var negatives = []; var sum = 0; for(var i = 0; i < numbers.length; i++) { if(numbers[i] < 0) { negati

在响应图像上叠加网格(Overlay grid on responsive image)

使用两个linear-gradient s,我们可以创建两个简单的线条,然后每隔n%重复一次background-size 。 它看起来像这样: background: linear-gradient(to bottom, #000 2px, transparent 2px), linear-gradient(to right, #000 2px, transparent 2px); background-size: 10%; 两个渐变创建两条相交的线,长度为百分比,如下所示: 使用默认的b

无法让POST在Azure网站上运行(Could not get POST to work on Azure Website)

最后我找到了答案......我不得不删除尾随的斜线! 我使用了“ https://example.com/api/messages/ ”,这将自动产生GET,无论我使用PostAsync还是PostAsJsonAsync。 使用“ https://example.com/api/messages”,GET和POST似乎都运行良好! Finally I've found the answer.... I had to remove the trailing slash! I've used "ht