2023-03-17 22:57:54 -04:00
|
|
|
<script lang="ts">
|
2023-03-18 13:49:02 -04:00
|
|
|
import { ref, type Ref } from 'vue'
|
|
|
|
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
isLive: {
|
|
|
|
type: Boolean,
|
|
|
|
default() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
channelName: {
|
|
|
|
type: String
|
2023-03-17 22:57:54 -04:00
|
|
|
}
|
2023-03-18 13:49:02 -04:00
|
|
|
},
|
|
|
|
setup(props) {
|
|
|
|
let messages: Ref<
|
|
|
|
{ username: string; channel: string; message: string; messageType: string }[]
|
|
|
|
> = ref([])
|
|
|
|
let ws = new WebSocket('ws://localhost:7000')
|
|
|
|
|
|
|
|
return {
|
|
|
|
ws,
|
|
|
|
messages,
|
|
|
|
props
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
const chatList = this.$refs.chatList as Element
|
|
|
|
|
|
|
|
this.ws.onmessage = (message) => {
|
|
|
|
if (message.data !== 'OK') {
|
|
|
|
this.messages.push(JSON.parse(message.data))
|
|
|
|
this.scrollToBottom(chatList)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ws.onopen = (data) => {
|
|
|
|
console.log(data)
|
|
|
|
this.ws.send('JOIN ' + this.props.channelName)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getChat() {
|
|
|
|
return this.messages
|
|
|
|
},
|
|
|
|
scrollToBottom(el: Element) {
|
|
|
|
el.scrollTop = el.scrollHeight
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 22:57:54 -04:00
|
|
|
</script>
|
|
|
|
<template>
|
2023-03-18 13:49:02 -04:00
|
|
|
<div v-if="isLive" class="p-3 bg-ctp-crust rounded-lg w-full max-w-xs flex flex-col">
|
|
|
|
<ul class="overflow-y-scroll h-[82vh]" ref="chatList">
|
|
|
|
<li v-for="message in getChat()" :key="messages.indexOf(message)">
|
|
|
|
<div class="text-white inline-flex">
|
|
|
|
<p class="text-sm">
|
|
|
|
<strong class="text-ctp-pink font-bold text-sm">{{ message.username }}</strong
|
|
|
|
>: {{ message.message }}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</template>
|