mirror of
https://github.com/Threnklyn/MinecraftChat.git
synced 2026-05-28 00:38:30 +02:00
Run the interval on the clientside instead of the server side
This commit is contained in:
@@ -2,6 +2,13 @@ module.exports = function($scope, socket) {
|
||||
|
||||
$scope.players = [];
|
||||
|
||||
// request player list every 5000ms
|
||||
setInterval(function() {
|
||||
socket.emit('players');
|
||||
}, 5000);
|
||||
|
||||
|
||||
// when the player list is received
|
||||
socket.on('bot:players', function(data) {
|
||||
var players = [];
|
||||
|
||||
@@ -15,12 +22,14 @@ module.exports = function($scope, socket) {
|
||||
|
||||
});
|
||||
|
||||
// clear player list if socket is disconnected
|
||||
socket.on('disconnect', function() {
|
||||
$scope.$apply(function() {
|
||||
$scope.players = [];
|
||||
});
|
||||
});
|
||||
|
||||
// clear player list when bot disconnects
|
||||
socket.on('bot:disconnect', function() {
|
||||
$scope.$apply(function() {
|
||||
$scope.players = [];
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
module.exports = function(socket) {
|
||||
|
||||
var interval;
|
||||
|
||||
// send the player list as soon as the bot spawns
|
||||
socket.mcbot.on('spawn', function() {
|
||||
socket.emit('bot:players', socket.mcbot.players);
|
||||
|
||||
// update the client with the player list every 5 seconds
|
||||
interval = setInterval(function() {
|
||||
if (socket.mcbot && socket.mcbot.entity) {
|
||||
socket.emit('bot:players', socket.mcbot.players);
|
||||
} else {
|
||||
console.error('leaked interval!');
|
||||
}
|
||||
}, 5000);
|
||||
|
||||
});
|
||||
|
||||
socket.mcbot.on('end', function() {
|
||||
if (interval) {
|
||||
clearInterval(interval);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('disconnect', function() {
|
||||
if (interval) {
|
||||
clearInterval(interval);
|
||||
}
|
||||
});
|
||||
|
||||
};
|
||||
@@ -4,6 +4,7 @@ module.exports = function(socket) {
|
||||
socket.mcbot.on('spawn', function() {
|
||||
var pos = socket.mcbot.entity.position;
|
||||
socket.emit('buffer:info', 'Spawned at X:' + pos.x + ', Y:' + pos.y + ', Z:' + pos.z);
|
||||
socket.emit('bot:players', socket.mcbot.players);
|
||||
socket.emit('bot:spawn');
|
||||
});
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ module.exports = function(socket) {
|
||||
require('./events/login')(socket);
|
||||
require('./events/spawn')(socket);
|
||||
require('./events/message')(socket);
|
||||
require('./events/players')(socket);
|
||||
// require('./events/chat')(socket);
|
||||
require('./events/end')(socket);
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
module.exports = function(socket) {
|
||||
|
||||
socket.on('players', function() {
|
||||
if (socket.mcbot && socket.mcbot.players) {
|
||||
socket.emit('bot:players', socket.mcbot.players);
|
||||
}
|
||||
});
|
||||
|
||||
};
|
||||
@@ -6,6 +6,7 @@ module.exports = function(io) {
|
||||
require('./events/connection')(socket);
|
||||
require('./events/disconnection')(socket);
|
||||
require('./events/chat')(socket);
|
||||
require('./events/players')(socket);
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user