chatInformations
adb 4 years ago
parent a2b82ab615
commit 49aa6731df

@ -21,12 +21,19 @@ const wss = new ws.Server({
}); });
//WS handler //WS handler
let user = [];
wss.on('connection', (ws, req) => { 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} => ${msgJSON}`)
if (msg.type === 'message') wss.clients.forEach(client => client.send(msgJSON)) if (msg.type === 'message') wss.clients.forEach(client => client.send(msgJSON))
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))
}
}) })
let msg = { let msg = {
type: "info", type: "info",

@ -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",

@ -38,6 +38,14 @@ export default {
mounted() { mounted() {
sendMessage() sendMessage()
}, },
data(){
return {
roomInfo: {
name: "open chat",
user: []
}
}
},
methods: { methods: {
sendMessage(message){ sendMessage(message){
let msg = { let msg = {
@ -76,6 +84,10 @@ 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'){
this.roomInfo.user = msg.user
}
else if (msg.type === 'message'){ else if (msg.type === 'message'){
//just for now, ik it's dirty //just for now, ik it's dirty
element('messages').innerHTML += element('messages').innerHTML +=

@ -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