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

@@ -3,6 +3,9 @@
"module": "index.ts",
"type": "module",
"private": true,
"scripts": {
"dev": "bun --watch ./src/index.ts"
},
"devDependencies": {
"@types/bcrypt": "^6.0.0",
"@types/bun": "latest",

View File

@@ -30,6 +30,7 @@ service MessageService {
// User
message CreateUserRequest {
string email = 1;
string name = 2;
}
message CreateUserResponse {
@@ -72,6 +73,7 @@ message User {
optional string token = 2;
string email = 3;
optional string description = 4;
optional string name = 5;
}
message UserForChatResponse {
@@ -86,8 +88,8 @@ message UserForChatResponse {
// Chat
message CreateChatRequest {
repeated UserForChat users = 1;
// int32 type_id = 2;
optional string name = 1;
repeated UserForChat users = 2;
}
message UserForChat {
@@ -118,8 +120,7 @@ message GetChatResponse {
message ListChatRequest {
int32 page = 1;
repeated int32 user_ids = 2;
repeated string chat_ids = 3;
int32 user_id = 2;
}
message ListChatResponse {
@@ -128,8 +129,9 @@ message ListChatResponse {
message Chat {
string id = 1;
int32 type_id = 3;
repeated UserForChatResponse users = 5;
int32 type_id = 2;
optional string name = 3;
repeated UserForChatResponse users = 4;
}
// Chat end
@@ -183,6 +185,8 @@ message Message {
optional string image = 4;
optional string video = 5;
optional string file = 6;
string created_at = 7;
string updated_at = 8;
}
// Message end

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)
}