Socket.io – 多个用户的一个对象

我正在使用socket.io在node.js中构build一个应用程序,我希望人们能够对问题进行投票,并将计时器包括在内,这将为他们提供回答问题的时间限制。 我遇到的问题(我将在下面重点介绍)是,每次有人连接到页面时,它都会logging秒表对象的记号。 即使在人们离开之后,每当有人连接时也会继续logging一次。 这是我迄今为止的项目代码。

Stopwatch.js:

function Stopwatch() { if (false === (this instanceof Stopwatch)) { return new Stopwatch(); } this.hour = 3600000; this.minute = 60000; this.second = 1000; this.time = this.hour; this.interval = undefined; events.EventEmitter.call(this); _.bindAll(this, 'start', 'stop', 'reset', 'onTick'); }; util.inherits(Stopwatch, events.EventEmitter); Stopwatch.prototype.start = function() { console.log("Starting the Timer!"); this.interval = setInterval(this.onTick, this.second); this.emit('start'); }; Stopwatch.prototype.onTick = function() { var remainder = this.time, numhours, numMinutes, numSeconds, output = ""; if (this.time === 0) { this.stop(); return; } numHours = String(parseInt(remainder / this.hour, 10)); remainder -= this.hour * numHours; numMinutes = String(parseInt(remainder / this.minute, 10)); remainder -= this.minute * numMinutes; numSeconds = String(parseInt(remainder / this.second, 10)); output = _.map([numHours, numMinutes, numSeconds], function(str) { if (str.length === 1) { str = "0" + str; } return str; }).join(":"); this.emit('tick', output); this.time -= this.second; } module.exports = Stopwatch; } 

socket.js:

 var Stopwatch = require('../models/stopwatch'); var people = {}; var stopwatch = Stopwatch(); module.exports = function (io) { io.on('connection', function (socket) { //console.log("Someone joined..."); //socket.on('join', function () { if (Object.keys(people).length === 0) { console.log('host joined'); people[socket.id] = {"name": "host", "image": "fake picture", "host": true}; } else { console.log("Someone else joined"); people[socket.id] = {"name": "person", "image": "other picture", "host": false}; } console.log(people); //}); socket.on('startTimer', function() { stopwatch.start(); }); socket.on('disconnect', function() { delete people[socket.id]; console.log("someone left"); console.log(people); }); stopwatch.on('tick', function(time) { console.log(socket.id + '---stopwatch tick!' + time); socket.emit('timer', { countdown: time }); }); }); }; 

客户端上的javascript:

 var socket; $(document).ready(function() { socket = io(); socket.on('timer', function (data) { $('#timer').html(data.countdown); }); $('#start-timer').click(function() { socket.emit('startTimer'); }); }); 

提前致谢! 我不知道为什么它仍然logging每个用户和时间,即使他们从服务器断开连接。

发生的事情是,每当新的客户端打开一个到服务器的连接时,你都将一个事件监听器分配给stopwatch并且永远不会删除它,这会导致你正在经历的行为。 当套接字断开连接时,必须移除eventListener,如节点事件发射器类中所述 。

你应该得到这样的结果:

 io.on('connection', function (socket) { //console.log("Someone joined..."); //socket.on('join', function () { if (Object.keys(people).length === 0) { console.log('host joined'); people[socket.id] = {"name": "host", "image": "fake picture", "host": true}; } else { console.log("Someone else joined"); people[socket.id] = {"name": "person", "image": "other picture", "host": false}; } console.log(people); socket.on('startTimer', function() { stopwatch.start(); }); var _tickListener = function(time) { console.log(socket.id + '---stopwatch tick!' + time); socket.emit('timer', { countdown: time }); }; socket.on('disconnect', function() { delete people[socket.id]; stopwatch.removeListener('tick', _tickListener); console.log("someone left"); console.log(people); }); stopwatch.on('tick', _tickListener); });