2020-11-02 19:38:27 +00:00
|
|
|
const ws = require('ws')
|
|
|
|
|
|
|
|
//WS server
|
|
|
|
const wss = new ws.Server({
|
2020-11-02 22:08:19 +00:00
|
|
|
port: 8090,
|
2020-11-02 19:38:27 +00:00
|
|
|
perMessageDeflate: {
|
|
|
|
zlibDeflateOptions: {
|
|
|
|
chunkSize: 1024,
|
|
|
|
memLevel: 7,
|
|
|
|
level: 3
|
|
|
|
},
|
|
|
|
zlibInflateOptions: {
|
|
|
|
chunkSize: 10 * 1024
|
|
|
|
},
|
2020-11-02 22:08:19 +00:00
|
|
|
clientNoContextTakeover: true,
|
|
|
|
serverNoContextTakeover: true,
|
|
|
|
serverMaxWindowBits: 10,
|
|
|
|
concurrencyLimit: 10,
|
|
|
|
threshold: 1024
|
2020-11-02 19:38:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//WS handler
|
2020-11-07 19:31:55 +00:00
|
|
|
let user = [];
|
2020-11-02 22:08:19 +00:00
|
|
|
wss.on('connection', (ws, req) => {
|
2020-11-08 13:35:10 +00:00
|
|
|
let thisuser = ""
|
2020-11-02 22:08:19 +00:00
|
|
|
console.log(`${req.socket.remoteAddress} connected`)
|
2020-11-03 18:26:01 +00:00
|
|
|
ws.on('message', msgJSON => {
|
|
|
|
let msg = JSON.parse(msgJSON)
|
2020-11-08 22:36:13 +00:00
|
|
|
console.log(`${req.socket.remoteAddress} (${thisuser}) => ${msgJSON}`)
|
2020-11-08 13:35:10 +00:00
|
|
|
if (msg.type === 'message')
|
|
|
|
if (thisuser === ""){
|
|
|
|
ws.send(JSON.stringify({type: "error", content: "please login before writing"}))
|
|
|
|
ws.send('{"type":"route","path":"/login"}')
|
|
|
|
}else if (msg.content.text === "")
|
|
|
|
ws.send(JSON.stringify({type: "error", content: "your message was empty"}))
|
2020-11-10 23:21:04 +00:00
|
|
|
else if (msg.content.text.length >= 1000)
|
|
|
|
ws.send(JSON.stringify({type: "error", content: "your message is too long"}))
|
2020-11-08 13:35:10 +00:00
|
|
|
else{
|
|
|
|
msg.content.user = thisuser
|
2020-11-09 00:13:01 +00:00
|
|
|
//msg.content.text = msg.content.text.replace(/</g, "<").replace(/>/g, ">").replace(/\n/g, "<br>")
|
2020-11-08 22:36:13 +00:00
|
|
|
wss.clients.forEach(client => client.send(JSON.stringify(msg)))
|
2020-11-08 13:35:10 +00:00
|
|
|
}
|
2020-11-07 19:31:55 +00:00
|
|
|
else if (msg.type === 'login' && msg.content.user !== ""){
|
2020-11-10 23:21:04 +00:00
|
|
|
if (thisuser !== ""){
|
|
|
|
ws.send(JSON.stringify({type: "error", content: "you are already logged in"}))
|
|
|
|
ws.send('{"type":"route","path":"/chat"}')
|
|
|
|
}
|
|
|
|
else if (msg.content.user.length >= 20) ws.send(JSON.stringify({type: "error", content: "username is too long"}))
|
2020-11-08 22:36:13 +00:00
|
|
|
else if (msg.content.user === "you" || user.indexOf(msg.content.user) !== -1)
|
|
|
|
ws.send(JSON.stringify({type: "error", content: "username already exist"}))
|
2020-11-08 13:35:10 +00:00
|
|
|
else{
|
|
|
|
thisuser = msg.content.user
|
|
|
|
user.push(msg.content.user)
|
|
|
|
ws.send('{"type":"route","path":"/chat"}')
|
2020-11-08 22:36:13 +00:00
|
|
|
ws.send(JSON.stringify({type: "info", username: thisuser}))
|
2020-11-08 13:35:10 +00:00
|
|
|
wss.clients.forEach(client =>
|
|
|
|
client.send(JSON.stringify({type: "room", name: "open chat", user: user})))
|
|
|
|
}
|
2020-11-07 19:31:55 +00:00
|
|
|
}
|
2020-11-02 19:38:27 +00:00
|
|
|
})
|
2020-11-08 22:36:13 +00:00
|
|
|
ws.on('close', () => {
|
|
|
|
user.splice(user.indexOf(thisuser), 1);
|
|
|
|
console.log(`${req.socket.remoteAddress} (${thisuser}) closed`)
|
2020-11-09 03:28:41 +00:00
|
|
|
wss.clients.forEach(client =>
|
|
|
|
client.send(JSON.stringify({type: "room", name: "open chat", user: user})))
|
2020-11-08 22:36:13 +00:00
|
|
|
})
|
|
|
|
|
2020-11-08 13:35:10 +00:00
|
|
|
ws.send(JSON.stringify({type: "info", time: Date.now(), content: "connected"}))
|
2020-11-02 19:38:27 +00:00
|
|
|
})
|