login backend
This commit is contained in:
parent
49aa6731df
commit
e86f0635c1
36
api.js
36
api.js
@ -23,22 +23,36 @@ const wss = new ws.Server({
|
||||
//WS handler
|
||||
let user = [];
|
||||
wss.on('connection', (ws, req) => {
|
||||
let thisuser = ""
|
||||
console.log(`${req.socket.remoteAddress} connected`)
|
||||
ws.on('message', msgJSON => {
|
||||
let msg = JSON.parse(msgJSON)
|
||||
console.log(`${req.socket.remoteAddress} => ${msgJSON}`)
|
||||
if (msg.type === 'message') wss.clients.forEach(client => client.send(msgJSON))
|
||||
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"}))
|
||||
else{
|
||||
msg.content.user = thisuser
|
||||
wss.clients.forEach(client => {
|
||||
if (client !== ws) client.send(JSON.stringify(msg))})
|
||||
}
|
||||
else if (msg.type === 'login' && msg.content.user !== ""){
|
||||
user.push(msg.content.user)
|
||||
ws.send('{"type":"route","path":"/chat"}')
|
||||
let msg = {type: "room", name: "open chat", user: user}
|
||||
ws.send(JSON.stringify(msg))
|
||||
if (msg.content.user >= 20) ws.send(JSON.stringify({type: "error", content: "username is too long"}))
|
||||
else if (function(){
|
||||
for (let i = 0; i < user.length; i++) if (user[i] === msg.content.user) return true
|
||||
return false
|
||||
}() === true) ws.send(JSON.stringify({type: "error", content: "username already exist"}))
|
||||
else{
|
||||
thisuser = msg.content.user
|
||||
user.push(msg.content.user)
|
||||
ws.send('{"type":"route","path":"/chat"}')
|
||||
wss.clients.forEach(client =>
|
||||
client.send(JSON.stringify({type: "room", name: "open chat", user: user})))
|
||||
}
|
||||
}
|
||||
})
|
||||
let msg = {
|
||||
type: "info",
|
||||
time: Date.now(),
|
||||
content: "connected"
|
||||
}
|
||||
ws.send(JSON.stringify(msg))
|
||||
ws.send(JSON.stringify({type: "info", time: Date.now(), content: "connected"}))
|
||||
})
|
@ -1,7 +1,9 @@
|
||||
<template>
|
||||
<div class="topBanner">
|
||||
<div>
|
||||
<icon class="smallIcon" id="icon-arrow" ic="./sym/arrow_back-24px.svg" />
|
||||
<router-link to="login">
|
||||
<icon class="smallIcon" id="icon-arrow" ic="./sym/arrow_back-24px.svg" />
|
||||
</router-link>
|
||||
<icon class="smallIcon" id="picTop" ic="./sym/supervisor_account-24px.svg" />
|
||||
<div id="container">
|
||||
<div id="chatName">{{roomInfo.name}}</div>
|
||||
|
Loading…
Reference in New Issue
Block a user