我正在使用 socket.io 在 node.js 中构建一个应用程序,我希望人们能够对问题进行投票并包含一个计时器,这将为他们提供回答问题的时间限制。我遇到的问题(我将在下面突出显示)是每次有人连接到该页面时,它都会再次记录来自秒表对象的滴答声。即使在人们离开后,每次有人连接时它都会继续记录一次。到目前为止,这是我的项目代码。
秒表.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;
}
套接字.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');
});
});
提前致谢!我不明白为什么它仍然记录每个用户和时间,即使在他们与服务器断开连接之后也是如此。
最佳答案
发生的事情是,每次新客户端打开与服务器的连接时,您都会为 stopwatch
分配一个事件监听器并且永远不会删除它,这会导致您遇到的行为。如 node event emitter class 中所述,当套接字断开连接时,您必须删除 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);
});
关于javascript - Socket.io --- 一个对象用于多个用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33642901/