@nmtjs/ws-client 0.15.0-beta.1 → 0.15.0-beta.2

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.
Files changed (2) hide show
  1. package/dist/index.js +64 -0
  2. package/package.json +7 -7
package/dist/index.js ADDED
@@ -0,0 +1,64 @@
1
+ import { once } from '@nmtjs/common';
2
+ import { ConnectionType } from '@nmtjs/protocol';
3
+ export class WsTransportClient {
4
+ format;
5
+ protocol;
6
+ options;
7
+ type = ConnectionType.Bidirectional;
8
+ webSocket = null;
9
+ connecting = null;
10
+ constructor(format, protocol, options) {
11
+ this.format = format;
12
+ this.protocol = protocol;
13
+ this.options = options;
14
+ this.options = { debug: false, ...options };
15
+ }
16
+ async connect(params) {
17
+ const url = new URL(params.application ? `/${params.application}` : '/', this.options.url);
18
+ const secure = url.protocol === 'wss:' || url.protocol === 'https:';
19
+ url.protocol = secure ? 'wss:' : 'ws:';
20
+ url.searchParams.set('content-type', this.format.contentType);
21
+ url.searchParams.set('accept', this.format.contentType);
22
+ if (params.auth) {
23
+ url.searchParams.set('auth', params.auth);
24
+ }
25
+ const ws = this.options.WebSocket
26
+ ? new this.options.WebSocket(url)
27
+ : new WebSocket(url.toString());
28
+ ws.binaryType = 'arraybuffer';
29
+ this.connecting = new Promise((resolve, reject) => {
30
+ ws.addEventListener('open', () => {
31
+ params.onConnect();
32
+ this.connecting = null;
33
+ resolve();
34
+ });
35
+ ws.addEventListener('message', (event) => {
36
+ params.onMessage(new Uint8Array(event.data));
37
+ });
38
+ ws.addEventListener('error', (event) => {
39
+ reject(new Error('WebSocket error', { cause: event }));
40
+ });
41
+ ws.addEventListener('close', (event) => {
42
+ params.onDisconnect(event.reason);
43
+ this.webSocket = null;
44
+ });
45
+ });
46
+ this.webSocket = ws;
47
+ return this.connecting;
48
+ }
49
+ async disconnect() {
50
+ if (this.webSocket === null)
51
+ return;
52
+ const closing = once(this.webSocket, 'close');
53
+ this.webSocket.close(1000, 'client');
54
+ return closing;
55
+ }
56
+ async send(message, options) {
57
+ if (this.webSocket === null)
58
+ throw new Error('WebSocket is not connected');
59
+ await this.connecting;
60
+ if (!options.signal?.aborted)
61
+ this.webSocket.send(message);
62
+ }
63
+ }
64
+ export const WsTransportFactory = (params, options) => new WsTransportClient(params.format, params.protocol, options);
package/package.json CHANGED
@@ -5,23 +5,23 @@
5
5
  ".": "./dist/index.js"
6
6
  },
7
7
  "dependencies": {
8
- "@nmtjs/client": "0.15.0-beta.1",
9
- "@nmtjs/common": "0.15.0-beta.1"
8
+ "@nmtjs/common": "0.15.0-beta.2",
9
+ "@nmtjs/client": "0.15.0-beta.2"
10
10
  },
11
11
  "devDependencies": {
12
- "@nmtjs/protocol": "0.15.0-beta.1"
12
+ "@nmtjs/protocol": "0.15.0-beta.2"
13
13
  },
14
14
  "peerDependencies": {
15
- "@nmtjs/client": "0.15.0-beta.1",
16
- "@nmtjs/common": "0.15.0-beta.1",
17
- "@nmtjs/protocol": "0.15.0-beta.1"
15
+ "@nmtjs/common": "0.15.0-beta.2",
16
+ "@nmtjs/protocol": "0.15.0-beta.2",
17
+ "@nmtjs/client": "0.15.0-beta.2"
18
18
  },
19
19
  "files": [
20
20
  "dist",
21
21
  "LICENSE.md",
22
22
  "README.md"
23
23
  ],
24
- "version": "0.15.0-beta.1",
24
+ "version": "0.15.0-beta.2",
25
25
  "scripts": {
26
26
  "clean-build": "rm -rf ./dist",
27
27
  "build": "tsc",