|
|
@ -27,7 +27,7 @@ wss.on('connection', (ws, req) => {
|
|
|
|
console.log(`${req.socket.remoteAddress} connected`)
|
|
|
|
console.log(`${req.socket.remoteAddress} connected`)
|
|
|
|
ws.on('message', msgJSON => {
|
|
|
|
ws.on('message', msgJSON => {
|
|
|
|
let msg = JSON.parse(msgJSON)
|
|
|
|
let msg = JSON.parse(msgJSON)
|
|
|
|
console.log(`${req.socket.remoteAddress} => ${msgJSON}`)
|
|
|
|
console.log(`${req.socket.remoteAddress} (${thisuser}) => ${msgJSON}`)
|
|
|
|
if (msg.type === 'message')
|
|
|
|
if (msg.type === 'message')
|
|
|
|
if (thisuser === ""){
|
|
|
|
if (thisuser === ""){
|
|
|
|
ws.send(JSON.stringify({type: "error", content: "please login before writing"}))
|
|
|
|
ws.send(JSON.stringify({type: "error", content: "please login before writing"}))
|
|
|
@ -37,23 +37,26 @@ wss.on('connection', (ws, req) => {
|
|
|
|
else{
|
|
|
|
else{
|
|
|
|
msg.content.user = thisuser
|
|
|
|
msg.content.user = thisuser
|
|
|
|
msg.content.text = msg.content.text.replace(/</g, "<").replace(/>/g, ">").replace(/\n/g, "<br>")
|
|
|
|
msg.content.text = msg.content.text.replace(/</g, "<").replace(/>/g, ">").replace(/\n/g, "<br>")
|
|
|
|
wss.clients.forEach(client => {
|
|
|
|
wss.clients.forEach(client => client.send(JSON.stringify(msg)))
|
|
|
|
if (client !== ws) client.send(JSON.stringify(msg))})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (msg.type === 'login' && msg.content.user !== ""){
|
|
|
|
else if (msg.type === 'login' && msg.content.user !== ""){
|
|
|
|
if (msg.content.user >= 20) ws.send(JSON.stringify({type: "error", content: "username is too long"}))
|
|
|
|
if (msg.content.user.length >= 20) ws.send(JSON.stringify({type: "error", content: "username is too long"}))
|
|
|
|
else if (function(){
|
|
|
|
else if (msg.content.user === "you" || user.indexOf(msg.content.user) !== -1)
|
|
|
|
for (let i = 0; i < user.length; i++) if (user[i] === msg.content.user) return true
|
|
|
|
ws.send(JSON.stringify({type: "error", content: "username already exist"}))
|
|
|
|
return false
|
|
|
|
|
|
|
|
}() === true) ws.send(JSON.stringify({type: "error", content: "username already exist"}))
|
|
|
|
|
|
|
|
else{
|
|
|
|
else{
|
|
|
|
thisuser = msg.content.user
|
|
|
|
thisuser = msg.content.user
|
|
|
|
user.push(msg.content.user)
|
|
|
|
user.push(msg.content.user)
|
|
|
|
ws.send('{"type":"route","path":"/chat"}')
|
|
|
|
ws.send('{"type":"route","path":"/chat"}')
|
|
|
|
|
|
|
|
ws.send(JSON.stringify({type: "info", username: thisuser}))
|
|
|
|
wss.clients.forEach(client =>
|
|
|
|
wss.clients.forEach(client =>
|
|
|
|
client.send(JSON.stringify({type: "room", name: "open chat", user: user})))
|
|
|
|
client.send(JSON.stringify({type: "room", name: "open chat", user: user})))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
ws.on('close', () => {
|
|
|
|
|
|
|
|
user.splice(user.indexOf(thisuser), 1);
|
|
|
|
|
|
|
|
console.log(`${req.socket.remoteAddress} (${thisuser}) closed`)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
ws.send(JSON.stringify({type: "info", time: Date.now(), content: "connected"}))
|
|
|
|
ws.send(JSON.stringify({type: "info", time: Date.now(), content: "connected"}))
|
|
|
|
})
|
|
|
|
})
|