mirror of
https://github.com/Threnklyn/MinecraftChat.git
synced 2026-06-07 05:33:32 +02:00
More performant way of defining event listeners
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
|
|
||||||
socket.mcbot.on('end', () => {
|
function onEnd() {
|
||||||
socket.emit('bot:disconnect');
|
socket.emit('bot:disconnect');
|
||||||
delete socket.mcbot;
|
delete socket.mcbot;
|
||||||
});
|
}
|
||||||
|
|
||||||
|
socket.mcbot.on('end', onEnd);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
|
|
||||||
socket.mcbot.on('error', (error) => {
|
function onError(error) {
|
||||||
|
|
||||||
// this is okay because the connection was ended
|
// this is okay because the connection was ended
|
||||||
if (error.toString() === 'Error: write after end') return;
|
if (error.toString() === 'Error: write after end') return;
|
||||||
@@ -14,6 +14,8 @@ module.exports = (socket) => {
|
|||||||
// delete the bot
|
// delete the bot
|
||||||
delete socket.mcbot;
|
delete socket.mcbot;
|
||||||
|
|
||||||
});
|
}
|
||||||
|
|
||||||
|
socket.mcbot.on('error', onError);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
|
|
||||||
socket.mcbot.on('login', () => {
|
function onLogin() {
|
||||||
socket.emit('buffer:success', `Successfully logged in as ${socket.mcbot.username} with entity id ${socket.mcbot.entity.id}`);
|
socket.emit('buffer:success', `Successfully logged in as ${socket.mcbot.username} with entity id ${socket.mcbot.entity.id}`);
|
||||||
socket.emit('bot:connect', {
|
socket.emit('bot:connect', {
|
||||||
host: socket.connectionParams.hostname,
|
host: socket.connectionParams.hostname,
|
||||||
@@ -8,6 +8,8 @@ module.exports = (socket) => {
|
|||||||
username: socket.mcbot.username
|
username: socket.mcbot.username
|
||||||
});
|
});
|
||||||
console.log(`logged in > ${socket.connectionParams.hostname}:${socket.connectionParams.port} - Username: ${socket.mcbot.username}`);
|
console.log(`logged in > ${socket.connectionParams.hostname}:${socket.connectionParams.port} - Username: ${socket.mcbot.username}`);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
socket.mcbot.on('login', onLogin);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import {escapeHtml} from '../../utils';
|
|||||||
|
|
||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
|
|
||||||
socket.mcbot.on('message', (message) => {
|
function onMessage(message) {
|
||||||
|
|
||||||
// empty buffer
|
// empty buffer
|
||||||
var buffer = '';
|
var buffer = '';
|
||||||
@@ -44,6 +44,8 @@ module.exports = (socket) => {
|
|||||||
// send line back to the client
|
// send line back to the client
|
||||||
socket.emit('bot:message', buffer);
|
socket.emit('bot:message', buffer);
|
||||||
|
|
||||||
});
|
}
|
||||||
|
|
||||||
|
socket.mcbot.on('message', onMessage);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
|
|
||||||
socket.mcbot.on('spawn', () => {
|
function onSpawn() {
|
||||||
var pos = socket.mcbot.entity.position;
|
var pos = socket.mcbot.entity.position;
|
||||||
socket.emit('buffer:info', `Spawned at X:${pos.x}, Y:${pos.y}, Z:${pos.z}`);
|
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:players', socket.mcbot.players);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
socket.mcbot.on('spawn', onSpawn);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
|
|
||||||
socket.on('chat', (data) => {
|
function onChat(data) {
|
||||||
if (socket.mcbot && socket.mcbot.entity) {
|
if (socket.mcbot && socket.mcbot.entity) {
|
||||||
socket.mcbot.chat(data.message);
|
socket.mcbot.chat(data.message);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
socket.on('chat', onChat);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import events from '../../bot';
|
|||||||
|
|
||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
|
|
||||||
socket.on('server:connect', (data) => {
|
function onConnection(data) {
|
||||||
|
|
||||||
// log activity to console
|
// log activity to console
|
||||||
console.log(`connecting > ${data.hostname}:${data.port} - ${data.username}`);
|
console.log(`connecting > ${data.hostname}:${data.port} - ${data.username}`);
|
||||||
@@ -36,6 +36,8 @@ module.exports = (socket) => {
|
|||||||
// bind bot events
|
// bind bot events
|
||||||
events(socket);
|
events(socket);
|
||||||
|
|
||||||
});
|
}
|
||||||
|
|
||||||
|
socket.on('server:connect', onConnection);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,20 +1,14 @@
|
|||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
|
|
||||||
|
function onDisconnection() {
|
||||||
socket.on('disconnect', () => {
|
|
||||||
if (socket.mcbot) {
|
if (socket.mcbot) {
|
||||||
socket.mcbot.end();
|
socket.mcbot.end();
|
||||||
delete socket.mcbot;
|
delete socket.mcbot;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
|
||||||
socket.on('bot:disconnect', () => {
|
socket.on('disconnect', onDisconnection);
|
||||||
if (socket.mcbot) {
|
socket.on('bot:disconnect', onDisconnection);
|
||||||
socket.mcbot.end();
|
|
||||||
delete socket.mcbot;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
|
|
||||||
socket.on('players', () => {
|
function onPlayers() {
|
||||||
if (socket.mcbot && socket.mcbot.players) {
|
if (socket.mcbot && socket.mcbot.players) {
|
||||||
socket.emit('bot:players', socket.mcbot.players);
|
socket.emit('bot:players', socket.mcbot.players);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
socket.on('players', onPlayers);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user