fix notes, chat log
This commit is contained in:
parent
33033e150c
commit
239a65a241
@ -11,36 +11,49 @@ var wss = new WebSocketServer({
|
||||
clientTracking: true
|
||||
});
|
||||
|
||||
wss.broadcast = function (msg) {
|
||||
if (typeof msg == "object") msg = JSON.stringify(msg);
|
||||
for (let ws of wss.clients) ws.send(msg);
|
||||
}
|
||||
wss.chatlog = [];
|
||||
var chatlog = [];
|
||||
|
||||
wss.on("connection", (ws, req) => {
|
||||
req.query = qs.parse(req.url.substr(req.url.indexOf('?')+1));
|
||||
|
||||
function broadcast(msg, excludeSelf) {
|
||||
if (typeof msg == "object" && !(msg instanceof Buffer)) msg = JSON.stringify(msg);
|
||||
for (let ows of wss.clients) if (!(ows == ws && excludeSelf)) ows.send(msg);
|
||||
}
|
||||
function broadcastChat(message) {
|
||||
broadcast({type: "chat", message});
|
||||
chatlog.push(message);
|
||||
if (chatlog.length >= 100) chatlog.unshift();
|
||||
}
|
||||
|
||||
ws.user = {
|
||||
id: wss.clients.size,
|
||||
uid: req.socket.remoteAddress,
|
||||
nick: req.query.nick || "potato-chan",
|
||||
color: [Math.floor(Math.random()*255),Math.floor(Math.random()*255),Math.floor(Math.random()*255)]
|
||||
}
|
||||
|
||||
console.log("join", ws.user);
|
||||
|
||||
ws.send(JSON.stringify({
|
||||
type: "load",
|
||||
id: ws.user.id,
|
||||
uid: ws.user.uid,
|
||||
users: Array.from(wss.clients).map(x => x.user),
|
||||
chatlog: []
|
||||
chatlog
|
||||
}));
|
||||
|
||||
wss.broadcast({type: "chat", message: {type: "join", id: ws.user.id, uid: ws.user.uid, nick: ws.user.nick}});
|
||||
ws.on("close", () => wss.broadcast({type: "chat", message: {type: "leave", id: ws.user.id, uid: ws.user.uid, nick: ws.user.nick}}));
|
||||
broadcast({type: "join", id: ws.user.id, uid: ws.user.uid, nick: ws.user.nick, color: ws.user.color}, true);
|
||||
broadcastChat({type: "join", id: ws.user.id, uid: ws.user.uid, nick: ws.user.nick});
|
||||
ws.on("close", () => {
|
||||
console.log("leave", ws.user);
|
||||
broadcast({type: "leave", id: ws.user.id, uid: ws.user.uid, nick: ws.user.nick, color: ws.user.color}, true);
|
||||
broadcastChat({type: "leave", id: ws.user.id, uid: ws.user.uid, nick: ws.user.nick})
|
||||
});
|
||||
|
||||
ws.on("message", (msg, isBinary) => {
|
||||
if (isBinary) {
|
||||
wss.broadcast(Buffer.concat([msg, Buffer.from([ws.user.id])]));
|
||||
broadcast(Buffer.concat([msg, Buffer.from([ws.user.id])]), true);
|
||||
} else {
|
||||
msg = msg.toString();
|
||||
try {
|
||||
@ -49,14 +62,12 @@ wss.on("connection", (ws, req) => {
|
||||
console.log(msg);
|
||||
switch (msg.type) {
|
||||
case "chat":
|
||||
let message = {type: "message", content: msg.message, user: {uid: ws.user.uid, nick: ws.user.nick, color: ws.user.color}};
|
||||
wss.broadcast({type: "chat", message});
|
||||
wss.chatlog.push(message);
|
||||
broadcastChat({type: "message", content: msg.message, user: ws.user});
|
||||
break;
|
||||
case "nick":
|
||||
ws.user.nick = msg.nick;
|
||||
wss.broadcast({type: "nick", nick: ws.user.nick, id: ws.user.id, uid: ws.user.uid});
|
||||
wss.broadcast({type: "chat", message: {type: "nick", nick: ws.user.nick, id: ws.user.id}})
|
||||
broadcast({type: "nick", nick: ws.user.nick, id: ws.user.id, uid: ws.user.uid});
|
||||
broadcastChat({type: "nick", nick: ws.user.nick, id: ws.user.id});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user