Files
vinlottis/api/chat.js
2020-03-14 14:04:07 +01:00

26 lines
623 B
JavaScript

module.exports = io => {
io.on("connection", socket => {
let username = null;
socket.on("username", msg => {
if (msg.username == null) {
username = null;
socket.emit("accept_username", false);
return;
}
if (msg.username.length > 3 && msg.username.length < 30) {
username = msg.username;
socket.emit("accept_username", true);
return;
}
socket.emit("accept_username", false);
});
socket.on("chat", msg => {
msg.username = username;
msg.timestamp = new Date().getTime();
io.emit("chat", msg);
});
});
};