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<
|
2023-03-29 10:26:55 -04:00
|
|
|
{
|
|
|
|
username: string
|
|
|
|
channel: string
|
|
|
|
message: string
|
|
|
|
messageType: string
|
|
|
|
tags: Record<string, string>
|
|
|
|
}[]
|
2023-03-18 13:49:02 -04:00
|
|
|
> = ref([])
|
|
|
|
let ws = new WebSocket('ws://localhost:7000')
|
|
|
|
|
|
|
|
return {
|
|
|
|
ws,
|
|
|
|
messages,
|
|
|
|
props
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
const chatList = this.$refs.chatList as Element
|
2023-03-19 19:51:14 -04:00
|
|
|
const chatStatusMessage = this.$refs.initConnectingStatus as Element
|
2023-03-18 13:49:02 -04:00
|
|
|
|
|
|
|
this.ws.onmessage = (message) => {
|
2023-03-19 19:51:14 -04:00
|
|
|
if (message.data == 'OK') {
|
|
|
|
chatStatusMessage.textContent = `Connected to ${this.channelName}`
|
|
|
|
} else {
|
2023-03-18 13:49:02 -04:00
|
|
|
this.messages.push(JSON.parse(message.data))
|
2023-03-27 16:40:48 -04:00
|
|
|
this.clearMessages()
|
2023-03-18 13:49:02 -04:00
|
|
|
this.scrollToBottom(chatList)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ws.onopen = (data) => {
|
|
|
|
console.log(data)
|
2023-03-19 19:51:14 -04:00
|
|
|
this.ws.send('JOIN ' + this.props.channelName?.toLowerCase())
|
2023-03-18 13:49:02 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getChat() {
|
|
|
|
return this.messages
|
|
|
|
},
|
|
|
|
scrollToBottom(el: Element) {
|
|
|
|
el.scrollTop = el.scrollHeight
|
2023-03-27 16:40:48 -04:00
|
|
|
},
|
|
|
|
clearMessages() {
|
|
|
|
if (this.messages.length > 50) {
|
|
|
|
this.messages.shift
|
|
|
|
}
|
2023-03-18 13:49:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 22:57:54 -04:00
|
|
|
</script>
|
|
|
|
<template>
|
2023-03-19 19:51:14 -04:00
|
|
|
<div v-if="isLive" class="p-3 bg-ctp-crust rounded-lg w-full max-w-[15.625rem] flex flex-col">
|
2023-03-29 10:26:55 -04:00
|
|
|
<ul
|
|
|
|
class="overflow-y-scroll overflow-x-hidden whitespace-pre-wrap h-[46.875rem]"
|
|
|
|
ref="chatList"
|
|
|
|
>
|
2023-03-19 19:51:14 -04:00
|
|
|
<li>
|
2023-03-24 18:48:45 -04:00
|
|
|
<p ref="initConnectingStatus" class="text-gray-500 text-sm italic">
|
|
|
|
Connecting to {{ channelName }}.
|
|
|
|
</p>
|
2023-03-19 19:51:14 -04:00
|
|
|
</li>
|
2023-03-18 13:49:02 -04:00
|
|
|
<li v-for="message in getChat()" :key="messages.indexOf(message)">
|
|
|
|
<div class="text-white inline-flex">
|
|
|
|
<p class="text-sm">
|
2023-03-29 10:26:55 -04:00
|
|
|
<strong
|
|
|
|
:style="message.tags.color ? `color: ${message.tags.color};` : ``"
|
|
|
|
class="text-ctp-pink font-bold text-sm"
|
|
|
|
>{{ message.username }}</strong
|
2023-03-18 13:49:02 -04:00
|
|
|
>: {{ message.message }}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</template>
|