-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.vue
62 lines (59 loc) · 1.85 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<script setup lang="ts">
import { io } from 'socket.io-client'
const socket = io();
const connected = ref(false)
const msg = ref('')
const getIp = ref('')
const getMsg = ref([])
const hello = ref({ hello: '' })
onMounted(() => {
socket.on('connect', () => {
connected.value = socket.connected
});
socket.on('disconnect', () => {
connected.value = socket.connected
});
socket.on('getIP', function (data) {
getIp.value = data
// socket.emit('my other event', { my: 'data' });
});
socket.on('hello', function (data) {
hello.value = data
// socket.emit('my other event', { my: 'data' });
});
socket.on('msg', function (data) {
getMsg.value.push(data)
// socket.emit('my other event', { my: 'data' });
});
})
const sendMsg = () => {
socket.emit('msg', {user: `User Address ${getIp.value}:`, text: `${msg.value}`})
msg.value = ''
}
</script>
<template>
<div class="max-w-1000px mx-auto">
<div class="flex items-center justify-between py-4">
<div>Chat connected: {{ connected }}</div>
<div>{{ hello.hello }}</div>
<div>Address IP: {{ getIp }}</div>
</div>
<div>
<div class="bg-[#003543] p-4 rounded-t">Message list</div>
<div class="bg-[#012a35] p-4 rounded-b space-y-2">
<div v-for="(item, index) in getMsg" :key="index">
<div class="flex items-center justify-between text-xs">
<div>{{item.user.user}}</div>
<div>{{item.time}}</div>
</div>
<div class="text-[#bae6fd] text-base">{{item.user.text}}</div>
</div>
<div v-if="!getMsg.length" class="text-center">Not data</div>
</div>
</div>
<div class="flex items-center mt-3">
<w-input v-model="msg" @keyup.enter="sendMsg" class="flex-1"></w-input>
<w-button @click="sendMsg" size="default">Send msg</w-button>
</div>
</div>
</template>