Merge remote-tracking branch 'origin/master' into chatInformations

# Conflicts:
#	src/views/chat.vue
chatInformations
adb 4 years ago
commit 94eddf81a6

@ -21,17 +21,42 @@ const wss = new ws.Server({
}); });
//WS handler //WS handler
let user = [];
wss.on('connection', (ws, req) => { wss.on('connection', (ws, req) => {
let thisuser = ""
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') 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
msg.content.text = msg.content.text.replace(/</g, "&lt").replace(/>/g, "&gt").replace(/\n/g, "<br>")
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 = { ws.on('close', () => {
type: "info", user.splice(user.indexOf(thisuser), 1);
time: Date.now(), console.log(`${req.socket.remoteAddress} (${thisuser}) closed`)
content: "connected" })
}
ws.send(JSON.stringify(msg)) ws.send(JSON.stringify({type: "info", time: Date.now(), content: "connected"}))
}) })

@ -3,6 +3,10 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<!-- PWA support -->
<link rel="manifest" href="./manifest.json"/>
<meta name="theme-color" content="#2F3BA2"/>
<meta name="description" content="open chat"/>
<meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0'/> <meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0'/>
<link rel="icon" href="<%= BASE_URL %>favicon.ico"/> <link rel="icon" href="<%= BASE_URL %>favicon.ico"/>
<title><%= htmlWebpackPlugin.options.title %></title> <title><%= htmlWebpackPlugin.options.title %></title>

@ -0,0 +1,9 @@
{
"name": "open chat",
"short_name": "open chat",
"start_url": "/",
"display": "standalone",
"background_color": "#14181b",
"theme_color": "#00BCD4",
"orientation": "portrait-primary"
}

@ -32,5 +32,6 @@ export default {
background-color: #42b983; background-color: #42b983;
border-radius: 1rem 1rem 1rem 0; border-radius: 1rem 1rem 1rem 0;
text-align: left; text-align: left;
word-break: break-word;
} }
</style> </style>

@ -9,8 +9,8 @@
</template> </template>
<script> <script>
import icon from './icon.vue'; import icon from '@/components/icon.vue';
import main from '../main.js'; import main from '@/main.js';
export default { export default {
name: "newMessage", name: "newMessage",
@ -23,7 +23,10 @@ export default {
this.msg.time = Date.now() this.msg.time = Date.now()
main.methods.sendWebSocket(this.msg) main.methods.sendWebSocket(this.msg)
this.msg.content.text = "" this.msg.content.text = ""
this.resizeMessageBanner() document.getElementById("messagesContainer").style.height = "calc(100% - 7rem)"
document.getElementById("newMessageInput").style.height = "1.25rem"
let msgContainer = document.getElementById("messagesContainer")
msgContainer.scrollTo(0, msgContainer.scrollHeight)
} }
}, },
resizeMessageBanner(){ resizeMessageBanner(){
@ -33,7 +36,6 @@ export default {
let msgContainer = document.getElementById("messagesContainer") let msgContainer = document.getElementById("messagesContainer")
msgContainer.style.height msgContainer.style.height
= `calc(100% - ${id.parentElement.clientHeight}px - 3rem)` = `calc(100% - ${id.parentElement.clientHeight}px - 3rem)`
//msgContainer.scrollTo(0, msgContainer.scrollHeight)
} }
}, },
data(){ data(){
@ -42,9 +44,11 @@ export default {
type: "message", type: "message",
time: Date.now(), time: Date.now(),
content: { content: {
user: "you",
text: "" text: ""
} }
} },
chatroom: main.data().chatroom
} }
} }
} }
@ -67,7 +71,7 @@ export default {
margin-bottom: 1rem; margin-bottom: 1rem;
left: 2rem; left: 2rem;
min-height: 1.25rem; min-height: 1.25rem;
max-height: 14rem; max-height: 10rem;
width: calc(100% - 7rem); width: calc(100% - 7rem);
height: 1.25rem; height: 1.25rem;
background-color: #fff0; background-color: #fff0;

@ -1,11 +1,13 @@
<template> <template>
<div class="topBanner"> <div class="topBanner">
<div> <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" /> <icon class="smallIcon" id="picTop" ic="./sym/supervisor_account-24px.svg" />
<div id="container"> <div id="container">
<div id="chatName">{{roomInfo.name}}</div> <div id="chatName">{{chatroom.name}}</div>
<div id="users">{{roomInfo.user.length}} members</div> <div id="users">{{chatroom.user.length}} members</div>
</div> </div>
<icon v-on:click.native="showChatInfo()" class="smallIcon" id="icon-menu" ic="./sym/menu-24px.svg" /> <icon v-on:click.native="showChatInfo()" class="smallIcon" id="icon-menu" ic="./sym/menu-24px.svg" />
</div> </div>
@ -13,6 +15,7 @@
</template> </template>
<script> <script>
import icon from '@/components/icon.vue'; import icon from '@/components/icon.vue';
import main from '@/main.js';
export default { export default {
name: "topBanner", name: "topBanner",
@ -26,10 +29,7 @@ export default {
}, },
data(){ data(){
return { return {
roomInfo: { chatroom: main.data().chatroom
name: "open chat",
user: []
}
} }
} }
} }
@ -40,7 +40,7 @@ export default {
width: 100%; width: 100%;
height: 3rem; height: 3rem;
background-color: #1d1d1d; background-color: #1d1d1d;
box-shadow: 0 3px 10px #111; box-shadow: 0 0px 5px #111;
} }
.smallIcon{ .smallIcon{
top: 0.25rem; top: 0.25rem;

@ -27,17 +27,22 @@ const router = new VueRouter({
] ]
}) })
new Vue({ let chatroom = {
el: '#app', name: "open chat",
router, user: [],
template: '<App/>', username: "you",
components: {App} messages: []
}).$mount('#app') };
export default { export default {
mounted() { mounted() {
sendMessage() sendMessage()
}, },
data(){
return {
chatroom: chatroom
}
},
methods: { methods: {
sendMessage(message){ sendMessage(message){
let msg = { let msg = {
@ -55,9 +60,22 @@ export default {
} }
} }
new Vue({
el: '#app',
router,
template: '<App/>',
components: {App},
data(){
return {
chatroom: chatroom
}
}
}).$mount('#app')
const wsurl = 'ws://127.0.0.1:8090' const wsurl = 'ws://127.0.0.1:8090'
const socket = new WebSocket(wsurl) //const wsurl = 'wss://chat.adb.sh:8080'
const socket = new WebSocket(wsurl)
function element(id){ return document.getElementById(id)} function element(id){ return document.getElementById(id)}
socket.onopen = () => { socket.onopen = () => {
@ -76,14 +94,15 @@ socket.onmessage = (e) => {
console.log(`data received => ${e.data}`) console.log(`data received => ${e.data}`)
let msg = JSON.parse(e.data) let msg = JSON.parse(e.data)
if (msg.type === 'error') show_error(msg.content) if (msg.type === 'error') show_error(msg.content)
else if (msg.type === 'route') router.push({path: msg.path})
else if (msg.type === 'room'){
chatroom.user = msg.user
}
else if (msg.type === "info") chatroom.username = msg.username
else if (msg.type === 'message'){ else if (msg.type === 'message'){
//just for now, ik it's dirty chatroom.messages.push(msg)
element('messages').innerHTML += let msgContainer = document.getElementById("messagesContainer")
`<div class="messageContainer" data-v-032da2b2=""> if (msgContainer.scrollHeight < msgContainer.scrollTop + 1000) msgContainer.scrollTo(0, msgContainer.scrollHeight)
<div class="message" data-v-032da2b2="">
${msg.content.text}
</div>
</div>`;
} }
} }

@ -2,32 +2,19 @@
<div> <div>
<div ref="msgContainer" id="messagesContainer" class="messagesContainer"> <div ref="msgContainer" id="messagesContainer" class="messagesContainer">
<div id="messages" class="messages"> <div id="messages" class="messages">
<message msg="Hey :D" /> <p style="text-align: center; font-style: italic;">you entered the chat</p>
<message msg="Du bist blööööd xD" /> <div v-for="(message, i) in chatroom.messages" :key="message.time">
<messageReceive msg="Du auch" /> <div v-if="message.content.user !== chatroom.username && function(){
<message msg="lol" /> return i===0 || chatroom.messages[i-1].content.user!==message.content.user;}()"
<messageReceive msg="Du bist voll blöd, ich hasse dich, warum tust du das?!" /> style="margin-left: 2rem; margin-top: 1rem">{{message.content.user}}
<message msg="Lorem ipsum dolor sit amet, consectetur adipiscing elit. </div>
Ut imperdiet vel risus tristique mollis. Proin aliquam felis non vehicula ornare. <messageReceive v-if="message.content.user !== chatroom.username" :msg=message.content.text />
Fusce scelerisque pellentesque erat quis sollicitudin. <message v-if="message.content.user === chatroom.username" :msg=message.content.text />
Quisque aliquet, ligula ut volutpat vulputate, ligula lorem dictum velit, et aliquam sapien orci sed magna. </div>
Nam suscipit ex eget urna accumsan pulvinar. Pellentesque fringilla placerat feugiat.
Aenean aliquam vestibulum metus. Nulla augue turpis, consectetur vitae quam ac, porttitor rhoncus nunc.
Nullam non turpis consequat, placerat lectus nec, ornare orci.
Fusce lorem tortor, viverra ac suscipit sit amet, scelerisque id eros.
Suspendisse et ultricies elit, vitae pretium ipsum. Suspendisse vel ex in turpis pulvinar feugiat. "
/>
<messageReceive msg="Du hast Pizza!" />
<message msg="und Kuchen :P" />
<message msg="und Kuchen :P" />
<message msg="und Kuchen :P" />
<message msg="und Kuchen :P" />
<message msg="und Kuchen :P" />
</div> </div>
</div> </div>
<newMessage /> <newMessage />
<topBanner /> <topBanner />
<chatInformation />
</div> </div>
</template> </template>
@ -35,8 +22,8 @@
import message from '@/components/message.vue'; import message from '@/components/message.vue';
import messageReceive from '@/components/messageReceive.vue'; import messageReceive from '@/components/messageReceive.vue';
import newMessage from '@/components/newMessage.vue'; import newMessage from '@/components/newMessage.vue';
import topBanner from "@/components/topBanner"; import topBanner from '@/components/topBanner.vue';
import chatInformation from "@/components/chatInformation"; import main from '@/main.js';
export default { export default {
name: 'chat', name: 'chat',
@ -44,8 +31,12 @@ export default {
message, message,
messageReceive, messageReceive,
newMessage, newMessage,
topBanner, topBanner
chatInformation },
data(){
return {
chatroom: main.data().chatroom
}
} }
} }
</script> </script>

@ -5,23 +5,31 @@
<label for="longurl-input"></label> <label for="longurl-input"></label>
<input v-model="session.content.user" class="input" id="longurl-input" type="text" autocomplete="off" maxlength="20" placeholder="chose nickname"> <input v-model="session.content.user" class="input" id="longurl-input" type="text" autocomplete="off" maxlength="20" placeholder="chose nickname">
</div> </div>
<input type="hidden" value="search" name="login"> <textbtn v-on:click.native="login()" text="login" />
<textbtn text="login" />
</div> </div>
</template> </template>
<script> <script>
import textbtn from '@/components/textbtn'; import textbtn from '@/components/textbtn';
import main from "@/main";
export default { export default {
name: "login.vue", name: "login.vue",
components: { components: {
textbtn textbtn
}, },
methods: {
login() {
if (this.session.content.user !== "") {
this.session.time = Date.now()
main.methods.sendWebSocket(this.session)
}
}
},
data(){ data(){
return { return {
session: { session: {
type: "session", type: "login",
time: Date.now(), time: Date.now(),
content: { content: {
user: "" user: ""

Loading…
Cancel
Save