@cuboapp/ws 1.0.4 → 1.0.6

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.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cuboapp/ws",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
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.7",
15
- "@cuboapp/utils": "1.0.9",
14
+ "@cuboapp/http-server": "1.0.9",
15
+ "@cuboapp/utils": "1.0.10",
16
16
  "ws": "^8.19.0"
17
17
  },
18
18
  "devDependencies": {
@@ -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.ws?.addEventListener('open', () => {
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>) {
@@ -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
  }
@@ -155,10 +163,23 @@ export class WsServer<C = {}> {
155
163
  await this.httpServer.stop()
156
164
  }
157
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
+
158
175
  public async registerHandler<D extends unknown>(name: string, handler: WsServerHandler<C, D>) {
159
176
  this.handlers.set(name, handler as any)
160
177
  }
161
178
 
179
+ public async deleteHandler(name: string) {
180
+ this.handlers.delete(name)
181
+ }
182
+
162
183
  public async clientRequest<T extends unknown, D extends unknown>(dto: WsMessage<D> | string, opts?: WsServerRequestOptions<C, true>): Promise<T[]>
163
184
  public async clientRequest<T extends unknown, D extends unknown>(dto: WsMessage<D> | string, opts?: WsServerRequestOptions<C, false>): Promise<void>
164
185
  public async clientRequest<T extends unknown, D extends unknown>(dto: WsMessage<D> | string, opts?: WsServerRequestOptions<C, boolean>) {