@cuboapp/ws 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package-lock.json +111 -9692
- package/package.json +3 -3
- package/src/client/index.ts +15 -1
- package/src/server/index.ts +26 -1
- package/src/server/types/server/index.ts +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cuboapp/ws",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "WebSockets",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"repository": "git@cuboapp.gitlab.yandexcloud.net:cubo/ws.git",
|
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@cuboapp/http-server": "1.0.
|
|
15
|
-
"@cuboapp/utils": "1.0.
|
|
14
|
+
"@cuboapp/http-server": "1.0.8",
|
|
15
|
+
"@cuboapp/utils": "1.0.10",
|
|
16
16
|
"ws": "^8.19.0"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
package/src/client/index.ts
CHANGED
|
@@ -10,6 +10,9 @@ export * from './types'
|
|
|
10
10
|
export * from './utils'
|
|
11
11
|
|
|
12
12
|
export class WsClient<C extends unknown = {}> {
|
|
13
|
+
public id = ''
|
|
14
|
+
public auth: any = {}
|
|
15
|
+
|
|
13
16
|
constructor(private opts: WsClientOptions<C>) {
|
|
14
17
|
// handler response
|
|
15
18
|
this.registerHandler('response', ({ message }) => {
|
|
@@ -138,10 +141,17 @@ export class WsClient<C extends unknown = {}> {
|
|
|
138
141
|
|
|
139
142
|
this.ws = new WebSocket(url, token || undefined)
|
|
140
143
|
|
|
141
|
-
this.
|
|
144
|
+
this.registerHandler('init', ({ message }) => {
|
|
145
|
+
const { id, auth } = message.data as { id: string; auth: any }
|
|
146
|
+
|
|
147
|
+
this.id = id
|
|
148
|
+
this.auth = auth
|
|
149
|
+
|
|
142
150
|
resolve(true)
|
|
143
151
|
})
|
|
144
152
|
|
|
153
|
+
this.ws?.addEventListener('open', () => {})
|
|
154
|
+
|
|
145
155
|
this.ws?.addEventListener('message', (event: any) => {
|
|
146
156
|
this.handleMessage(JSON.parse(event.data))
|
|
147
157
|
})
|
|
@@ -155,6 +165,10 @@ export class WsClient<C extends unknown = {}> {
|
|
|
155
165
|
this.handlers.set(name, handler)
|
|
156
166
|
}
|
|
157
167
|
|
|
168
|
+
public async deleteHandler(name: string) {
|
|
169
|
+
this.handlers.delete(name)
|
|
170
|
+
}
|
|
171
|
+
|
|
158
172
|
public async request<T extends unknown>(dto: WsMessage | string, opts?: WsClientRequestOptions<true>): Promise<T>
|
|
159
173
|
public async request<T extends unknown>(dto: WsMessage | string, opts?: WsClientRequestOptions<false>): Promise<void>
|
|
160
174
|
public async request(dto: WsMessage | string, opts?: WsClientRequestOptions<boolean>) {
|
package/src/server/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type HttpServer } from '@cuboapp/http-server'
|
|
1
|
+
import { HttpServerMethod, HttpServerRequestDto, HttpServerRouteHandler, HttpServerRouteOptions, type HttpServer } from '@cuboapp/http-server'
|
|
2
2
|
import { uuid } from '@cuboapp/utils'
|
|
3
3
|
import { WebSocketServer } from 'ws'
|
|
4
4
|
|
|
@@ -38,6 +38,7 @@ export class WsServer<C = {}> {
|
|
|
38
38
|
this.httpServer = (await import('@cuboapp/http-server')).createHttpServer<C>({
|
|
39
39
|
host: this.opts.host,
|
|
40
40
|
port: this.opts.port,
|
|
41
|
+
debug: this.opts.debug ?? this.opts?.http?.debug ?? false,
|
|
41
42
|
...(this.opts?.http || {})
|
|
42
43
|
})
|
|
43
44
|
|
|
@@ -80,6 +81,13 @@ export class WsServer<C = {}> {
|
|
|
80
81
|
socket.id = uuid()
|
|
81
82
|
socket.auth = request.auth
|
|
82
83
|
|
|
84
|
+
socket.send(
|
|
85
|
+
JSON.stringify({
|
|
86
|
+
data: { id: socket.id, auth: socket.auth },
|
|
87
|
+
method: 'init'
|
|
88
|
+
})
|
|
89
|
+
)
|
|
90
|
+
|
|
83
91
|
if (this.opts.debug) {
|
|
84
92
|
console.log('Client "' + socket.id + '" connected')
|
|
85
93
|
}
|
|
@@ -90,6 +98,8 @@ export class WsServer<C = {}> {
|
|
|
90
98
|
}
|
|
91
99
|
|
|
92
100
|
this.clients.delete(socket)
|
|
101
|
+
|
|
102
|
+
this.opts?.onDisconnect?.(socket)
|
|
93
103
|
})
|
|
94
104
|
|
|
95
105
|
socket.addListener('message', (e) => {
|
|
@@ -107,6 +117,8 @@ export class WsServer<C = {}> {
|
|
|
107
117
|
})
|
|
108
118
|
|
|
109
119
|
this.clients.add(socket)
|
|
120
|
+
|
|
121
|
+
this.opts?.onConnect?.(socket)
|
|
110
122
|
})
|
|
111
123
|
|
|
112
124
|
this.inited = true
|
|
@@ -151,10 +163,23 @@ export class WsServer<C = {}> {
|
|
|
151
163
|
await this.httpServer.stop()
|
|
152
164
|
}
|
|
153
165
|
|
|
166
|
+
public registerHttpHandler<R extends HttpServerRequestDto>(
|
|
167
|
+
method: HttpServerMethod,
|
|
168
|
+
path: string,
|
|
169
|
+
handler: HttpServerRouteHandler<C, R>,
|
|
170
|
+
opts?: HttpServerRouteOptions
|
|
171
|
+
) {
|
|
172
|
+
return this.httpServer.registerRoute(method, path, handler, opts)
|
|
173
|
+
}
|
|
174
|
+
|
|
154
175
|
public async registerHandler<D extends unknown>(name: string, handler: WsServerHandler<C, D>) {
|
|
155
176
|
this.handlers.set(name, handler as any)
|
|
156
177
|
}
|
|
157
178
|
|
|
179
|
+
public async deleteHandler(name: string) {
|
|
180
|
+
this.handlers.delete(name)
|
|
181
|
+
}
|
|
182
|
+
|
|
158
183
|
public async clientRequest<T extends unknown, D extends unknown>(dto: WsMessage<D> | string, opts?: WsServerRequestOptions<C, true>): Promise<T[]>
|
|
159
184
|
public async clientRequest<T extends unknown, D extends unknown>(dto: WsMessage<D> | string, opts?: WsServerRequestOptions<C, false>): Promise<void>
|
|
160
185
|
public async clientRequest<T extends unknown, D extends unknown>(dto: WsMessage<D> | string, opts?: WsServerRequestOptions<C, boolean>) {
|
|
@@ -16,6 +16,9 @@ export type WsServerOptions<C extends unknown> = {
|
|
|
16
16
|
}
|
|
17
17
|
debug?: boolean
|
|
18
18
|
http?: Partial<HttpServerOptions<C>>
|
|
19
|
+
|
|
20
|
+
onConnect?: (socket: WsServerSocket<C>) => void | Promise<void>
|
|
21
|
+
onDisconnect?: (socket: WsServerSocket<C>) => void | Promise<void>
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
export type WsServerHandler<C, D extends unknown = object> = (
|