This commit is contained in:
2026-02-21 20:12:30 +03:00
parent d632bf0f40
commit 6e8c068231
6 changed files with 97 additions and 22 deletions

View File

@@ -27,6 +27,29 @@ interface ListChatDto {
chat_ids?: string[]
}
interface ListMessageDto {
page: number
chat_id: string
}
interface CreateMessageDto {
chat_id: string
user_id: number
text?: string
image?: string
video?: string
file?: string
}
interface Message {
id: number
user_id: number
text?: string
image: string
video: string
file: string
}
interface Chat {
id: string
type_id: number
@@ -43,6 +66,8 @@ enum Services {
getUserByEmail = 'getUserByEmail',
listUser = 'listUser',
listChat = 'listChat',
listMessage = 'listMessage',
createMessage = 'createMessage',
}
class GrpcClient {
@@ -90,6 +115,14 @@ class GrpcClient {
getChatsByUser(dto: ListChatDto) {
return this.toPromise<ListChatDto, Chat[]>(this.messageClient, Services.listChat)(dto)
}
getMessagesByChatId(dto: ListMessageDto) {
return this.toPromise<ListMessageDto, Message[]>(this.messageClient, Services.listMessage)(dto)
}
createMessage(dto: CreateMessageDto) {
return this.toPromise<CreateMessageDto, Message>(this.messageClient, Services.createMessage)(dto)
}
}
export const grpcClient = new GrpcClient()

View File

@@ -1,8 +1,7 @@
import { parseJson } from './utils/utils.ts'
import { HttpStatusCodes } from './constants.ts'
import type { WebSocketData } from './types/types.ts'
import type { WebSocketData, WsData } from './types/types.ts'
import { loginRequest, wsRequest } from './handles.ts'
import { grpcClient } from './grpc/client.ts'
import { grpcClient as client } from './grpc/client.ts'
const GROUP = 'group'
const PORT = 3000
@@ -27,13 +26,13 @@ const server = Bun.serve({
const ipAddr = ws.remoteAddress
console.log('ipAddr', ipAddr)
const chatResponse = await grpcClient.getChatsByUser({ page: 0, user_ids: [1] })
const chatResponse = await client.getChatsByUser({ page: 0, user_ids: [1] })
chatResponse.data.forEach((el) => ws.subscribe(el.id))
console.log('chats', chatResponse.data)
console.log('subscriptions', ws.subscriptions)
ws.send(JSON.stringify({ type: 'chats', ...chatResponse }))
ws.send(JSON.stringify({ type: 'CHATS', ...chatResponse }))
} catch (error) {
console.log(error)
ws.close(1011, 'error')
@@ -46,22 +45,48 @@ const server = Bun.serve({
// text: 'connected'
// }))
},
message(ws, message) {
async message(ws, message) {
console.log('Websocket message', message)
// const result = ws.send(message);
// if (typeof message === 'string') {
try {
if (typeof message === 'string') {
const o = JSON.parse(message) as WsData
if (!o) {
console.log('wrong message')
return
}
if (o.type === 'CREATE_MESSAGE') {
console.log('create')
const message = await client.createMessage({
chat_id: o.data.chat_id,
user_id: ws.data.userId,
text: o.data.text
})
server.publish(o.data.chat_id, JSON.stringify({ type: 'CREATE_MESSAGE', ...message }))
}
if (o.type === 'GET_MESSAGES') {
console.log('get')
const messages = await client.getMessagesByChatId({ chat_id: o.data.chat_id, page: 1 })
server.publish(o.data.chat_id, JSON.stringify({ type: 'MESSAGES', ...messages }))
}
}
} catch (error) {
ws.send(JSON.stringify({ message: 'data error' }))
}
// const json = parseJson(message)
// server.publish(
// GROUP,
// JSON.stringify({
// id: Bun.randomUUIDv7(),
// createdAt: new Date().toISOString(),
// username: ws.data.username,
// text: json.text,
// }),
// )
// }
// ws.send(response.arrayBuffer()); // ArrayBuffer
// ws.send(new Uint8Array([1, 2, 3])); // TypedArray | DataView

View File

@@ -1,14 +1,24 @@
export interface WebSocketData {
chatId?: string
token?: string
userId?: number
userId: number
}
export type LoginDto = { email: string }
interface ChatData {
type: 'chat'
id: string
interface ListMessages {
type: 'GET_MESSAGES'
data: {
chat_id: string
}
}
interface CreateMessage {
type: 'CREATE_MESSAGE'
data: {
chat_id: string
text: string
}
}
interface UserData {
@@ -22,4 +32,4 @@ interface MessageData {
text: string
}
type WsData = ChatData | UserData | MessageData
export type WsData = ListMessages|CreateMessage

View File

@@ -1,6 +1,6 @@
export function parseJson(str: string) {
export function parseJson<T>(str: string) {
try {
return JSON.parse(str)
return JSON.parse(str) as T
} catch (e: any) {
console.error(e.message)
}