我正在使用socket.io在node.js中构建一个应用程序,我希望人们能够对问题进行投票并包含一个计时器,这将为他们提供回答问题的时间限制。我遇到的问题(我将在下面重点介绍)是,每当有人连接到该页面时,它将再次记录来自秒表对象的勾号。即使人们离开,每次有人连接时,它都会继续记录一次。这是我到目前为止的项目代码。
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');
});
});
提前致谢!我无法弄清楚为什么它仍会记录每个用户和时间,即使他们与服务器断开连接。
答案 0 :(得分:2)
每当新客户端打开与服务器的连接时,您都会向stopwatch
分配一个事件监听器并且永远不会将其删除,这会导致您遇到的行为。当套接字断开连接时,您必须删除eventListener,如node event emitter class。
你应该得到这样的东西:
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);
});