wip
This commit is contained in:
@@ -3,6 +3,9 @@
|
|||||||
"module": "index.ts",
|
"module": "index.ts",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"private": true,
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"dev": "bun --watch ./src/index.ts"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bcrypt": "^6.0.0",
|
"@types/bcrypt": "^6.0.0",
|
||||||
"@types/bun": "latest",
|
"@types/bun": "latest",
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ service MessageService {
|
|||||||
// User
|
// User
|
||||||
message CreateUserRequest {
|
message CreateUserRequest {
|
||||||
string email = 1;
|
string email = 1;
|
||||||
|
string name = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
message CreateUserResponse {
|
message CreateUserResponse {
|
||||||
@@ -72,6 +73,7 @@ message User {
|
|||||||
optional string token = 2;
|
optional string token = 2;
|
||||||
string email = 3;
|
string email = 3;
|
||||||
optional string description = 4;
|
optional string description = 4;
|
||||||
|
optional string name = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
message UserForChatResponse {
|
message UserForChatResponse {
|
||||||
@@ -86,8 +88,8 @@ message UserForChatResponse {
|
|||||||
|
|
||||||
// Chat
|
// Chat
|
||||||
message CreateChatRequest {
|
message CreateChatRequest {
|
||||||
repeated UserForChat users = 1;
|
optional string name = 1;
|
||||||
// int32 type_id = 2;
|
repeated UserForChat users = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
message UserForChat {
|
message UserForChat {
|
||||||
@@ -118,8 +120,7 @@ message GetChatResponse {
|
|||||||
|
|
||||||
message ListChatRequest {
|
message ListChatRequest {
|
||||||
int32 page = 1;
|
int32 page = 1;
|
||||||
repeated int32 user_ids = 2;
|
int32 user_id = 2;
|
||||||
repeated string chat_ids = 3;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message ListChatResponse {
|
message ListChatResponse {
|
||||||
@@ -128,8 +129,9 @@ message ListChatResponse {
|
|||||||
|
|
||||||
message Chat {
|
message Chat {
|
||||||
string id = 1;
|
string id = 1;
|
||||||
int32 type_id = 3;
|
int32 type_id = 2;
|
||||||
repeated UserForChatResponse users = 5;
|
optional string name = 3;
|
||||||
|
repeated UserForChatResponse users = 4;
|
||||||
}
|
}
|
||||||
// Chat end
|
// Chat end
|
||||||
|
|
||||||
@@ -183,6 +185,8 @@ message Message {
|
|||||||
optional string image = 4;
|
optional string image = 4;
|
||||||
optional string video = 5;
|
optional string video = 5;
|
||||||
optional string file = 6;
|
optional string file = 6;
|
||||||
|
string created_at = 7;
|
||||||
|
string updated_at = 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message end
|
// Message end
|
||||||
|
|||||||
@@ -27,6 +27,29 @@ interface ListChatDto {
|
|||||||
chat_ids?: string[]
|
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 {
|
interface Chat {
|
||||||
id: string
|
id: string
|
||||||
type_id: number
|
type_id: number
|
||||||
@@ -43,6 +66,8 @@ enum Services {
|
|||||||
getUserByEmail = 'getUserByEmail',
|
getUserByEmail = 'getUserByEmail',
|
||||||
listUser = 'listUser',
|
listUser = 'listUser',
|
||||||
listChat = 'listChat',
|
listChat = 'listChat',
|
||||||
|
listMessage = 'listMessage',
|
||||||
|
createMessage = 'createMessage',
|
||||||
}
|
}
|
||||||
|
|
||||||
class GrpcClient {
|
class GrpcClient {
|
||||||
@@ -90,6 +115,14 @@ class GrpcClient {
|
|||||||
getChatsByUser(dto: ListChatDto) {
|
getChatsByUser(dto: ListChatDto) {
|
||||||
return this.toPromise<ListChatDto, Chat[]>(this.messageClient, Services.listChat)(dto)
|
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()
|
export const grpcClient = new GrpcClient()
|
||||||
|
|||||||
43
src/index.ts
43
src/index.ts
@@ -1,8 +1,7 @@
|
|||||||
import { parseJson } from './utils/utils.ts'
|
|
||||||
import { HttpStatusCodes } from './constants.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 { loginRequest, wsRequest } from './handles.ts'
|
||||||
import { grpcClient } from './grpc/client.ts'
|
import { grpcClient as client } from './grpc/client.ts'
|
||||||
|
|
||||||
const GROUP = 'group'
|
const GROUP = 'group'
|
||||||
const PORT = 3000
|
const PORT = 3000
|
||||||
@@ -27,13 +26,13 @@ const server = Bun.serve({
|
|||||||
const ipAddr = ws.remoteAddress
|
const ipAddr = ws.remoteAddress
|
||||||
console.log('ipAddr', ipAddr)
|
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))
|
chatResponse.data.forEach((el) => ws.subscribe(el.id))
|
||||||
|
|
||||||
console.log('chats', chatResponse.data)
|
console.log('chats', chatResponse.data)
|
||||||
console.log('subscriptions', ws.subscriptions)
|
console.log('subscriptions', ws.subscriptions)
|
||||||
|
|
||||||
ws.send(JSON.stringify({ type: 'chats', ...chatResponse }))
|
ws.send(JSON.stringify({ type: 'CHATS', ...chatResponse }))
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
ws.close(1011, 'error')
|
ws.close(1011, 'error')
|
||||||
@@ -46,22 +45,48 @@ const server = Bun.serve({
|
|||||||
// text: 'connected'
|
// text: 'connected'
|
||||||
// }))
|
// }))
|
||||||
},
|
},
|
||||||
message(ws, message) {
|
async message(ws, message) {
|
||||||
console.log('Websocket message', message)
|
console.log('Websocket message', message)
|
||||||
// const result = ws.send(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)
|
// const json = parseJson(message)
|
||||||
// server.publish(
|
// server.publish(
|
||||||
// GROUP,
|
// GROUP,
|
||||||
// JSON.stringify({
|
// JSON.stringify({
|
||||||
// id: Bun.randomUUIDv7(),
|
// id: Bun.randomUUIDv7(),
|
||||||
// createdAt: new Date().toISOString(),
|
|
||||||
// username: ws.data.username,
|
// username: ws.data.username,
|
||||||
// text: json.text,
|
// text: json.text,
|
||||||
// }),
|
// }),
|
||||||
// )
|
// )
|
||||||
// }
|
|
||||||
|
|
||||||
// ws.send(response.arrayBuffer()); // ArrayBuffer
|
// ws.send(response.arrayBuffer()); // ArrayBuffer
|
||||||
// ws.send(new Uint8Array([1, 2, 3])); // TypedArray | DataView
|
// ws.send(new Uint8Array([1, 2, 3])); // TypedArray | DataView
|
||||||
|
|||||||
@@ -1,14 +1,24 @@
|
|||||||
export interface WebSocketData {
|
export interface WebSocketData {
|
||||||
chatId?: string
|
chatId?: string
|
||||||
token?: string
|
token?: string
|
||||||
userId?: number
|
userId: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export type LoginDto = { email: string }
|
export type LoginDto = { email: string }
|
||||||
|
|
||||||
interface ChatData {
|
interface ListMessages {
|
||||||
type: 'chat'
|
type: 'GET_MESSAGES'
|
||||||
id: string
|
data: {
|
||||||
|
chat_id: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CreateMessage {
|
||||||
|
type: 'CREATE_MESSAGE'
|
||||||
|
data: {
|
||||||
|
chat_id: string
|
||||||
|
text: string
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UserData {
|
interface UserData {
|
||||||
@@ -22,4 +32,4 @@ interface MessageData {
|
|||||||
text: string
|
text: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type WsData = ChatData | UserData | MessageData
|
export type WsData = ListMessages|CreateMessage
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
export function parseJson(str: string) {
|
export function parseJson<T>(str: string) {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(str)
|
return JSON.parse(str) as T
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e.message)
|
console.error(e.message)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user