This commit is contained in:
2026-02-23 23:56:39 +03:00
parent 25750fee51
commit 2cc21bb284
15 changed files with 340 additions and 94 deletions

View File

@@ -19,12 +19,12 @@ export interface User {
}
export interface WsData {
type: WsDataType
type: WsDataType2
// data: Chat | Message | User | User[] | Message[] | Chat[]
data: unknown
}
export enum WsDataType {
export enum WsDataType2 {
GET_CHATS = 'GET_CHATS',
GET_USERS = 'GET_USERS',
GET_MESSAGES = 'GET_MESSAGES',
@@ -36,7 +36,7 @@ export enum WsDataType {
ERROR = 'ERROR',
}
export enum COMMAND {
export enum COMMAND2 {
CONNECT = 'CONNECT',
SEND = 'SEND',
CLOSE = 'CLOSE',
@@ -68,24 +68,24 @@ export function useSharedWebSocket(options?: { url?: string; autoConnect?: true
const { type, data, connected, message } = event.data
switch (type) {
case WsDataType.USER:
case WsDataType2.USER:
break
case WsDataType.CHATS:
case WsDataType2.CHATS:
chats.value = data
break
case WsDataType.MESSAGES:
case WsDataType2.MESSAGES:
messages.value = data
// if (options.onMessage) {
// options.onMessage(data)
// }
break
case WsDataType.CREATE_MESSAGE:
case WsDataType2.CREATE_MESSAGE:
messages.value.push(data)
break
case WsDataType.STATUS:
case WsDataType2.STATUS:
isConnected.value = connected
break
case WsDataType.ERROR:
case WsDataType2.ERROR:
error.value = message
logout()
break
@@ -93,7 +93,7 @@ export function useSharedWebSocket(options?: { url?: string; autoConnect?: true
}
worker.value.port.postMessage({
command: COMMAND.CONNECT,
command: COMMAND2.CONNECT,
data: { url: url, token: getToken() },
})
}
@@ -109,7 +109,7 @@ export function useSharedWebSocket(options?: { url?: string; autoConnect?: true
const send = (data: WsData) => {
if (worker.value) {
worker.value.port.postMessage({
command: COMMAND.SEND,
command: COMMAND2.SEND,
data: data,
})
}
@@ -117,7 +117,7 @@ export function useSharedWebSocket(options?: { url?: string; autoConnect?: true
const close = () => {
if (worker.value) {
worker.value.port.postMessage({ command: COMMAND.CLOSE })
worker.value.port.postMessage({ command: COMMAND2.CLOSE })
}
}