diff --git a/api.js b/api.js index a001add..c18fd77 100644 --- a/api.js +++ b/api.js @@ -21,17 +21,42 @@ 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)) + console.log(`${req.socket.remoteAddress} (${thisuser}) => ${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 + msg.content.text = msg.content.text.replace(//g, ">").replace(/\n/g, "
") + wss.clients.forEach(client => client.send(JSON.stringify(msg))) + } + else if (msg.type === 'login' && msg.content.user !== ""){ + if (msg.content.user.length >= 20) ws.send(JSON.stringify({type: "error", content: "username is too long"})) + else if (msg.content.user === "you" || user.indexOf(msg.content.user) !== -1) + 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"}') + ws.send(JSON.stringify({type: "info", username: thisuser})) + 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.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"})) }) \ No newline at end of file diff --git a/public/index.html b/public/index.html index 212c8ea..61a5cfe 100644 --- a/public/index.html +++ b/public/index.html @@ -3,6 +3,10 @@ + + + + <%= htmlWebpackPlugin.options.title %> diff --git a/public/manifest.json b/public/manifest.json new file mode 100644 index 0000000..1aa6a1e --- /dev/null +++ b/public/manifest.json @@ -0,0 +1,9 @@ +{ + "name": "open chat", + "short_name": "open chat", + "start_url": "/", + "display": "standalone", + "background_color": "#14181b", + "theme_color": "#00BCD4", + "orientation": "portrait-primary" +} \ No newline at end of file diff --git a/src/components/messageReceive.vue b/src/components/messageReceive.vue index 5c4e5ea..2a1b2ff 100644 --- a/src/components/messageReceive.vue +++ b/src/components/messageReceive.vue @@ -32,5 +32,6 @@ export default { background-color: #42b983; border-radius: 1rem 1rem 1rem 0; text-align: left; + word-break: break-word; } \ No newline at end of file diff --git a/src/components/newMessage.vue b/src/components/newMessage.vue index 74f91bb..2645085 100644 --- a/src/components/newMessage.vue +++ b/src/components/newMessage.vue @@ -9,8 +9,8 @@ diff --git a/src/views/login.vue b/src/views/login.vue index c216853..7cd978b 100644 --- a/src/views/login.vue +++ b/src/views/login.vue @@ -5,23 +5,31 @@ - - +