node-datachannel 0.10.0 → 0.11.0

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 (40) hide show
  1. package/.github/workflows/build-mac-m1.yml +7 -6
  2. package/.github/workflows/build-mac-x64.yml +5 -5
  3. package/.github/workflows/build-win.yml +3 -3
  4. package/.gitmodules +3 -0
  5. package/CMakeLists.txt +2 -2
  6. package/README.md +33 -31
  7. package/examples/client-server/client-benchmark.js +7 -9
  8. package/examples/client-server/client-periodic.js +7 -9
  9. package/examples/client-server/client.js +7 -9
  10. package/examples/client-server/package-lock.json +0 -27
  11. package/examples/client-server/package.json +0 -1
  12. package/examples/client-server/signaling-server.js +21 -10
  13. package/examples/electron-demo/README.md +1 -1
  14. package/examples/websocket/websocket-client.js +20 -0
  15. package/examples/websocket/websocket-server.js +22 -0
  16. package/lib/index.cjs +52 -9
  17. package/lib/index.js +22 -15
  18. package/lib/node-datachannel.js +7 -0
  19. package/lib/websocket-server.js +34 -0
  20. package/package.json +4 -4
  21. package/polyfill/Events.js +5 -3
  22. package/polyfill/Exception.js +23 -0
  23. package/polyfill/README.md +3 -3
  24. package/polyfill/RTCDataChannel.js +19 -11
  25. package/polyfill/RTCPeerConnection.js +148 -41
  26. package/polyfill/index.cjs +252 -59
  27. package/src/peer-connection-wrapper.cpp +11 -3
  28. package/src/web-socket-server-wrapper.cpp +23 -23
  29. package/src/web-socket-server-wrapper.h +8 -6
  30. package/src/web-socket-wrapper.cpp +7 -1
  31. package/test/connectivity.js +1 -23
  32. package/test/wpt-tests/README.md +46 -0
  33. package/test/wpt-tests/chrome-failed-tests.js +42 -0
  34. package/test/wpt-tests/index.js +96 -0
  35. package/test/wpt-tests/last-test-results.md +205 -0
  36. package/test/wpt-tests/package-lock.json +1712 -0
  37. package/test/wpt-tests/package.json +19 -0
  38. package/test/wpt-tests/wpt-test-list.js +137 -0
  39. package/test/wpt-tests/wpt.js +131 -0
  40. package/test/wpt.js +0 -50
@@ -6,25 +6,26 @@ on:
6
6
  # tags:
7
7
  # - v*
8
8
 
9
+
9
10
  env:
10
11
  ACTIONS_ALLOW_UNSECURE_COMMANDS: true
11
12
 
12
13
  jobs:
13
14
  build-macos:
14
- runs-on: macos-11
15
+ runs-on: macos-13
15
16
  strategy:
16
17
  matrix:
17
18
  node-version: [16.x]
18
19
 
19
20
  steps:
20
- - uses: actions/checkout@v3
21
+ - uses: actions/checkout@v4
21
22
  - name: Install OpenSSL
22
23
  run: |
23
- wget https://mac.r-project.org/bin/darwin20/arm64/openssl-1.1.1o-darwin.20-arm64.tar.xz
24
- tar -xf openssl-1.1.1o-darwin.20-arm64.tar.xz -C /tmp
25
- rm -rf openssl-1.1.1o-darwin.20-arm64.tar.xz
24
+ wget https://mac.r-project.org/bin/darwin20/arm64/openssl-1.1.1t-darwin.20-arm64.tar.xz
25
+ tar -xf openssl-1.1.1t-darwin.20-arm64.tar.xz -C /tmp
26
+ rm -rf openssl-1.1.1t-darwin.20-arm64.tar.xz
26
27
  - name: Use Node.js ${{ matrix.node-version }}
27
- uses: actions/setup-node@v3
28
+ uses: actions/setup-node@v4
28
29
  with:
29
30
  node-version: ${{ matrix.node-version }}
30
31
  - name: Build
@@ -11,25 +11,25 @@ env:
11
11
 
12
12
  jobs:
13
13
  build-macos:
14
- runs-on: macos-11
14
+ runs-on: macos-13
15
15
  strategy:
16
16
  matrix:
17
17
  node-version: [16.x]
18
18
 
19
19
  steps:
20
- - uses: actions/checkout@v2
20
+ - uses: actions/checkout@v4
21
21
  - name: Install OpenSSL
22
22
  run: HOMEBREW_NO_INSTALL_CLEANUP=1 brew reinstall openssl@1.1
23
23
  - name: Use Node.js ${{ matrix.node-version }}
24
- uses: actions/setup-node@v1
24
+ uses: actions/setup-node@v4
25
25
  with:
26
26
  node-version: ${{ matrix.node-version }}
27
27
  - name: Build
28
28
  run: npm install --build-from-source
29
29
  env:
30
30
  CI: true
31
- OPENSSL_ROOT_DIR: /usr/local/opt/openssl
32
- OPENSSL_LIBRARIES: /usr/local/opt/openssl/lib
31
+ OPENSSL_ROOT_DIR: /usr/local/opt/openssl@1.1
32
+ OPENSSL_LIBRARIES: /usr/local/opt/openssl@1.1/lib
33
33
  - name: Test
34
34
  run: npm run test
35
35
  env:
@@ -17,12 +17,12 @@ jobs:
17
17
  node-version: [16.x]
18
18
 
19
19
  steps:
20
- - uses: actions/checkout@v2
20
+ - uses: actions/checkout@v4
21
21
  - uses: ilammy/msvc-dev-cmd@v1
22
22
  - name: Install OpenSSL
23
23
  run: choco install openssl
24
24
  - name: Use Node.js ${{ matrix.node-version }}
25
- uses: actions/setup-node@v1
25
+ uses: actions/setup-node@v4
26
26
  with:
27
27
  node-version: ${{ matrix.node-version }}
28
28
  - name: Build
@@ -46,7 +46,7 @@ jobs:
46
46
  node_arch:
47
47
  - x86
48
48
  steps:
49
- - uses: actions/checkout@v2
49
+ - uses: actions/checkout@v4
50
50
  - uses: ilammy/msvc-dev-cmd@v1
51
51
  with:
52
52
  arch: x86
package/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "test/wpt-tests/wpt"]
2
+ path = test/wpt-tests/wpt
3
+ url = https://github.com/web-platform-tests/wpt.git
package/CMakeLists.txt CHANGED
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
2
2
  cmake_policy(SET CMP0091 NEW)
3
3
  cmake_policy(SET CMP0042 NEW)
4
4
 
5
- project(node_datachannel VERSION 0.10.0)
5
+ project(node_datachannel VERSION 0.11.0)
6
6
 
7
7
  # -Dnapi_build_version=8
8
8
  add_definitions(-DNAPI_VERSION=8)
@@ -29,7 +29,7 @@ include(FetchContent)
29
29
  FetchContent_Declare(
30
30
  libdatachannel
31
31
  GIT_REPOSITORY https://github.com/paullouisageneau/libdatachannel.git
32
- GIT_TAG "v0.21.1"
32
+ GIT_TAG "v0.21.2"
33
33
  )
34
34
 
35
35
  option(NO_MEDIA "Disable media transport support in libdatachannel" OFF)
package/README.md CHANGED
@@ -1,16 +1,14 @@
1
- # Easy to use WebRTC data channels and media transport
1
+ # WebRTC For Node.js and Electron ( with WebSocket)
2
2
 
3
3
  ![Linux CI Build](https://github.com/murat-dogan/node-datachannel/workflows/Build%20-%20Linux/badge.svg) ![Windows CI Build](https://github.com/murat-dogan/node-datachannel/workflows/Build%20-%20Win/badge.svg) ![Mac x64 CI Build](https://github.com/murat-dogan/node-datachannel/workflows/Build%20-%20Mac%20x64/badge.svg) ![Mac M1 CI Build](https://github.com/murat-dogan/node-datachannel/workflows/Build%20-%20Mac%20M1/badge.svg)
4
4
 
5
- - Easy to use
6
5
  - Lightweight
7
6
  - No need to deal with WebRTC stack!
8
- - Small binary sizes
7
+ - Small binary sizes (~8MB for Linux x64)
9
8
  - Type infos for Typescript
9
+ - Integrated WebSocket Client & Server Implementation
10
10
 
11
- This project is NodeJS bindings for [libdatachannel](https://github.com/paullouisageneau/libdatachannel) library.
12
-
13
- Please check [libdatachannel](https://github.com/paullouisageneau/libdatachannel) for Compatibility & WebRTC details.
11
+ This project is Node.js bindings for [libdatachannel](https://github.com/paullouisageneau/libdatachannel) library.
14
12
 
15
13
  ## Install
16
14
 
@@ -20,11 +18,11 @@ npm install node-datachannel
20
18
 
21
19
  ## Supported Platforms
22
20
 
23
- `node-datachannel` targets N-API version 8 and supports NodeJS v16 and above. It is tested on Linux, Windows and MacOS. For N-API compatibility please check [here](https://nodejs.org/api/n-api.html#n_api_n_api_version_matrix).
21
+ `node-datachannel` targets N-API version 8 and supports Node.js v16 and above. It is tested on Linux, Windows and MacOS. For N-API compatibility please check [here](https://nodejs.org/api/n-api.html#n_api_n_api_version_matrix).
24
22
 
25
- | | Linux [x64,armv7,arm64] (1) | Windows [x86,x64] | Mac [M1,x64] |
26
- | ----------------------- | :-------------------------: | :---------------: | :----------: |
27
- | NAPI V8 (>= NodeJS V16) | + | + | + |
23
+ | | Linux [x64,armv7,arm64] (1) | Windows [x86,x64] | Mac [M1,x64] |
24
+ | ------------------------- | :-------------------------: | :---------------: | :----------: |
25
+ | N-API v8 (>= Node.js v16) | + | + | + |
28
26
 
29
27
  **(1)** For Linux musl + libc
30
28
 
@@ -34,6 +32,22 @@ npm install node-datachannel
34
32
 
35
33
  Please check [electron demo](/examples/electron-demo)
36
34
 
35
+ ## WebRTC Polyfills
36
+
37
+ WebRTC polyfills to be used for libraries like `simple-peer`.
38
+
39
+ Please check [here for more](/polyfill)
40
+
41
+ ### web-platform-tests
42
+
43
+ Please check actual situation [here](/test/wpt-tests/)
44
+
45
+ ## WebSocket Client & Server
46
+
47
+ Integrated WebSocket Client & Server is available, which can be used separately or for signaling.
48
+
49
+ For an example usage, [check here](/examples/websocket)
50
+
37
51
  ## Example Usage
38
52
 
39
53
  ```js
@@ -42,34 +56,30 @@ import nodeDataChannel from 'node-datachannel';
42
56
  // Log Level
43
57
  nodeDataChannel.initLogger('Debug');
44
58
 
59
+ // Integrated WebSocket available and can be used for signaling etc
60
+ // const ws = new nodeDataChannel.WebSocket();
61
+
45
62
  let dc1 = null;
46
63
  let dc2 = null;
47
64
 
48
65
  let peer1 = new nodeDataChannel.PeerConnection('Peer1', { iceServers: ['stun:stun.l.google.com:19302'] });
49
66
 
50
- // Set Callbacks
51
67
  peer1.onLocalDescription((sdp, type) => {
52
- console.log('Peer1 SDP:', sdp, ' Type:', type);
53
68
  peer2.setRemoteDescription(sdp, type);
54
69
  });
55
70
  peer1.onLocalCandidate((candidate, mid) => {
56
- console.log('Peer1 Candidate:', candidate);
57
71
  peer2.addRemoteCandidate(candidate, mid);
58
72
  });
59
73
 
60
74
  let peer2 = new nodeDataChannel.PeerConnection('Peer2', { iceServers: ['stun:stun.l.google.com:19302'] });
61
75
 
62
- // Set Callbacks
63
76
  peer2.onLocalDescription((sdp, type) => {
64
- console.log('Peer2 SDP:', sdp, ' Type:', type);
65
77
  peer1.setRemoteDescription(sdp, type);
66
78
  });
67
79
  peer2.onLocalCandidate((candidate, mid) => {
68
- console.log('Peer2 Candidate:', candidate);
69
80
  peer1.addRemoteCandidate(candidate, mid);
70
81
  });
71
82
  peer2.onDataChannel((dc) => {
72
- console.log('Peer2 Got DataChannel: ', dc.getLabel());
73
83
  dc2 = dc;
74
84
  dc2.onMessage((msg) => {
75
85
  console.log('Peer2 Received Msg:', msg);
@@ -86,19 +96,11 @@ dc1.onOpen(() => {
86
96
  dc1.onMessage((msg) => {
87
97
  console.log('Peer1 Received Msg:', msg);
88
98
  });
89
-
90
- setTimeout(() => {
91
- dc1.close();
92
- dc2.close();
93
- peer1.close();
94
- peer2.close();
95
- nodeDataChannel.cleanup();
96
- }, 10 * 1000);
97
99
  ```
98
100
 
99
- ## WebRTC Polyfills
101
+ ## Examples
100
102
 
101
- Please check [here](/polyfill)
103
+ Please check [examples](/examples/) folder
102
104
 
103
105
  ## Test
104
106
 
@@ -111,14 +113,14 @@ node test/connectivity.js # Connectivity
111
113
 
112
114
  Please check [here](/BULDING.md)
113
115
 
114
- ## Examples
115
-
116
- Please check [examples](/examples/) folder
117
-
118
116
  ## API Docs
119
117
 
120
118
  Please check [docs](/API.md) page
121
119
 
120
+ ## Contributing
121
+
122
+ Contributions are welcome!
123
+
122
124
  ## Thanks
123
125
 
124
126
  Thanks to [Streamr](https://streamr.network/) for supporting this project by being a Sponsor!
@@ -6,7 +6,6 @@ const require = createRequire(import.meta.url);
6
6
  const yargs = require('yargs/yargs');
7
7
  const { hideBin } = require('yargs/helpers');
8
8
 
9
- import WebSocket from 'ws';
10
9
  import readline from 'readline';
11
10
  import nodeDataChannel from '../../lib/index.js';
12
11
 
@@ -60,23 +59,22 @@ const rl = readline.createInterface({
60
59
  output: process.stdout,
61
60
  });
62
61
 
63
- const ws = new WebSocket(wsUrl + '/' + id, {
64
- perMessageDeflate: false,
65
- });
62
+ const ws = new nodeDataChannel.WebSocket();
63
+ ws.open(wsUrl + '/' + id);
66
64
 
67
65
  console.log(`The local ID is: ${id}`);
68
66
  console.log(`Waiting for signaling to be connected...`);
69
67
 
70
- ws.on('open', () => {
68
+ ws.onOpen(() => {
71
69
  console.log('WebSocket connected, signaling ready');
72
70
  readUserInput();
73
71
  });
74
72
 
75
- ws.on('error', (err) => {
73
+ ws.onError((err) => {
76
74
  console.log('WebSocket Error: ', err);
77
75
  });
78
76
 
79
- ws.on('message', (msgStr) => {
77
+ ws.onMessage((msgStr) => {
80
78
  let msg = JSON.parse(msgStr);
81
79
  switch (msg.type) {
82
80
  case 'offer':
@@ -184,10 +182,10 @@ function createPeerConnection(peerId) {
184
182
  console.log('GatheringState: ', state);
185
183
  });
186
184
  peerConnection.onLocalDescription((description, type) => {
187
- ws.send(JSON.stringify({ id: peerId, type, description }));
185
+ ws.sendMessage(JSON.stringify({ id: peerId, type, description }));
188
186
  });
189
187
  peerConnection.onLocalCandidate((candidate, mid) => {
190
- ws.send(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
188
+ ws.sendMessage(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
191
189
  });
192
190
  peerConnection.onDataChannel((dc) => {
193
191
  rl.close();
@@ -1,4 +1,3 @@
1
- import WebSocket from 'ws';
2
1
  import readline from 'readline';
3
2
  import nodeDataChannel from '../../lib/index.js';
4
3
 
@@ -25,23 +24,22 @@ const rl = readline.createInterface({
25
24
 
26
25
  // Signaling Server
27
26
  const WS_URL = process.env.WS_URL || 'ws://localhost:8000';
28
- const ws = new WebSocket(WS_URL + '/' + id, {
29
- perMessageDeflate: false,
30
- });
27
+ const ws = new nodeDataChannel.WebSocket();
28
+ ws.open(WS_URL + '/' + id);
31
29
 
32
30
  console.log(`The local ID is: ${id}`);
33
31
  console.log(`Waiting for signaling to be connected...`);
34
32
 
35
- ws.on('open', () => {
33
+ ws.onOpen(() => {
36
34
  console.log('WebSocket connected, signaling ready');
37
35
  readUserInput();
38
36
  });
39
37
 
40
- ws.on('error', (err) => {
38
+ ws.onError((err) => {
41
39
  console.log('WebSocket Error: ', err);
42
40
  });
43
41
 
44
- ws.on('message', (msgStr) => {
42
+ ws.onMessage((msgStr) => {
45
43
  let msg = JSON.parse(msgStr);
46
44
  switch (msg.type) {
47
45
  case 'offer':
@@ -122,10 +120,10 @@ function createPeerConnection(peerId) {
122
120
  console.log('GatheringState: ', state);
123
121
  });
124
122
  peerConnection.onLocalDescription((description, type) => {
125
- ws.send(JSON.stringify({ id: peerId, type, description }));
123
+ ws.sendMessage(JSON.stringify({ id: peerId, type, description }));
126
124
  });
127
125
  peerConnection.onLocalCandidate((candidate, mid) => {
128
- ws.send(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
126
+ ws.sendMessage(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
129
127
  });
130
128
  peerConnection.onDataChannel((dc) => {
131
129
  rl.close();
@@ -1,4 +1,3 @@
1
- import WebSocket from 'ws';
2
1
  import readline from 'readline';
3
2
  import nodeDataChannel from '../../lib/index.js';
4
3
 
@@ -13,23 +12,22 @@ const id = randomId(4);
13
12
 
14
13
  // Signaling Server
15
14
  const WS_URL = process.env.WS_URL || 'ws://localhost:8000';
16
- const ws = new WebSocket(WS_URL + '/' + id, {
17
- perMessageDeflate: false,
18
- });
15
+ const ws = new nodeDataChannel.WebSocket();
16
+ ws.open(WS_URL + '/' + id);
19
17
 
20
18
  console.log(`The local ID is: ${id}`);
21
19
  console.log(`Waiting for signaling to be connected...`);
22
20
 
23
- ws.on('open', () => {
21
+ ws.onOpen(() => {
24
22
  console.log('WebSocket connected, signaling ready');
25
23
  readUserInput();
26
24
  });
27
25
 
28
- ws.on('error', (err) => {
26
+ ws.onError((err) => {
29
27
  console.log('WebSocket Error: ', err);
30
28
  });
31
29
 
32
- ws.on('message', (msgStr) => {
30
+ ws.onMessage((msgStr) => {
33
31
  let msg = JSON.parse(msgStr);
34
32
  switch (msg.type) {
35
33
  case 'offer':
@@ -86,10 +84,10 @@ function createPeerConnection(peerId) {
86
84
  console.log('GatheringState: ', state);
87
85
  });
88
86
  peerConnection.onLocalDescription((description, type) => {
89
- ws.send(JSON.stringify({ id: peerId, type, description }));
87
+ ws.sendMessage(JSON.stringify({ id: peerId, type, description }));
90
88
  });
91
89
  peerConnection.onLocalCandidate((candidate, mid) => {
92
- ws.send(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
90
+ ws.sendMessage(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
93
91
  });
94
92
  peerConnection.onDataChannel((dc) => {
95
93
  console.log('DataChannel from ' + peerId + ' received with label "', dc.getLabel() + '"');
@@ -9,7 +9,6 @@
9
9
  "version": "1.0.0",
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
- "ws": "^7.5.3",
13
12
  "yargs": "^16.2.0"
14
13
  }
15
14
  },
@@ -138,26 +137,6 @@
138
137
  "url": "https://github.com/chalk/wrap-ansi?sponsor=1"
139
138
  }
140
139
  },
141
- "node_modules/ws": {
142
- "version": "7.5.10",
143
- "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.10.tgz",
144
- "integrity": "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==",
145
- "engines": {
146
- "node": ">=8.3.0"
147
- },
148
- "peerDependencies": {
149
- "bufferutil": "^4.0.1",
150
- "utf-8-validate": "^5.0.2"
151
- },
152
- "peerDependenciesMeta": {
153
- "bufferutil": {
154
- "optional": true
155
- },
156
- "utf-8-validate": {
157
- "optional": true
158
- }
159
- }
160
- },
161
140
  "node_modules/y18n": {
162
141
  "version": "5.0.8",
163
142
  "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
@@ -282,12 +261,6 @@
282
261
  "strip-ansi": "^6.0.0"
283
262
  }
284
263
  },
285
- "ws": {
286
- "version": "7.5.10",
287
- "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.10.tgz",
288
- "integrity": "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==",
289
- "requires": {}
290
- },
291
264
  "y18n": {
292
265
  "version": "5.0.8",
293
266
  "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
@@ -11,7 +11,6 @@
11
11
  "author": "",
12
12
  "license": "ISC",
13
13
  "dependencies": {
14
- "ws": "^7.5.3",
15
14
  "yargs": "^16.2.0"
16
15
  }
17
16
  }
@@ -1,15 +1,22 @@
1
- import WebSocket from 'ws';
1
+ import nodeDataChannel from '../../lib/index.js';
2
+
3
+ // Init Logger
4
+ nodeDataChannel.initLogger('Debug');
2
5
 
3
6
  const clients = {};
4
7
 
5
- const wss = new WebSocket.Server({ port: 8000 });
8
+ const wsServer = new nodeDataChannel.WebSocketServer({ bindAddress: '127.0.0.1', port: 8000 });
9
+
10
+ wsServer.onClient((ws) => {
11
+ let id = '';
6
12
 
7
- wss.on('connection', (ws, req) => {
8
- const id = req.url.replace('/', '');
9
- console.log(`New Connection from ${id}`);
13
+ ws.onOpen(() => {
14
+ id = ws.path().replace('/', '');
15
+ console.log(`New Connection from ${id}`);
16
+ clients[id] = ws;
17
+ });
10
18
 
11
- clients[id] = ws;
12
- ws.on('message', (buffer) => {
19
+ ws.onMessage((buffer) => {
13
20
  let msg = JSON.parse(buffer);
14
21
  let peerId = msg.id;
15
22
  let peerWs = clients[peerId];
@@ -18,11 +25,15 @@ wss.on('connection', (ws, req) => {
18
25
  if (!peerWs) return console.error(`Can not find peer with ID ${peerId}`);
19
26
 
20
27
  msg.id = id;
21
- peerWs.send(JSON.stringify(msg));
28
+ peerWs.sendMessage(JSON.stringify(msg));
22
29
  });
23
30
 
24
- ws.on('close', () => {
25
- console.log(`${id} disconected`);
31
+ ws.onClosed(() => {
32
+ console.log(`${id} disconnected`);
26
33
  delete clients[id];
27
34
  });
35
+
36
+ ws.onError((err) => {
37
+ console.error(err);
38
+ });
28
39
  });
@@ -6,7 +6,7 @@ Project created with
6
6
 
7
7
  ## Electron Compatibility and Rebuilding
8
8
 
9
- `node-datachannel` uses N-API for creating binaries. Normally `electron` is compatible with N-API binarıes, so you should not need to rebuild the binaries.
9
+ `node-datachannel` uses N-API for creating binaries. Normally `electron` is compatible with N-API binaries, so you should not need to rebuild the binaries.
10
10
 
11
11
  ### If you need anyway, you can use `electron-rebuild` package with some modifications.
12
12
 
@@ -0,0 +1,20 @@
1
+ import * as nodeDataChannel from '../../lib/index.js';
2
+
3
+ const clientSocket = new nodeDataChannel.WebSocket();
4
+
5
+ clientSocket.open('ws://127.0.0.1:3000');
6
+
7
+ clientSocket.onOpen(() => {
8
+ console.log('Client socket opened');
9
+ clientSocket.sendMessage('Echo this!');
10
+ });
11
+
12
+ clientSocket.onMessage((message) => {
13
+ console.log('Message from server: ' + message);
14
+ });
15
+
16
+ clientSocket.onClosed(() => {
17
+ console.log('Client socket closed');
18
+ });
19
+
20
+ // clientSocket.close();
@@ -0,0 +1,22 @@
1
+ import * as nodeDataChannel from '../../lib/index.js';
2
+
3
+ const ws = new nodeDataChannel.WebSocketServer({ bindAddress: '127.0.0.1', port: 3000 });
4
+
5
+ ws.onClient((clientSocket) => {
6
+ clientSocket.onOpen(() => {
7
+ console.log('Client socket opened');
8
+ clientSocket.sendMessage('Hello from server');
9
+ });
10
+
11
+ clientSocket.onMessage((message) => {
12
+ console.log('Message from client: ' + message);
13
+ clientSocket.sendMessage(message);
14
+ });
15
+
16
+ clientSocket.onClosed(() => {
17
+ console.log('Client socket closed');
18
+ });
19
+ });
20
+
21
+ // When done, close the server
22
+ // ws.stop();
package/lib/index.cjs CHANGED
@@ -4,8 +4,14 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var module$1 = require('module');
6
6
  var stream = require('stream');
7
+ var events = require('events');
7
8
 
8
9
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
10
+ // createRequire is native in node version >= 12
11
+ const require$1 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
12
+
13
+ const nodeDataChannel = require$1('../build/Release/node_datachannel.node');
14
+
9
15
  /**
10
16
  * Turns a node-datachannel DataChannel into a real Node.js stream, complete with buffering,
11
17
  * backpressure (up to a point - if the buffer fills up, messages are dropped), and
@@ -101,10 +107,37 @@ class DataChannelStream extends stream.Duplex {
101
107
  }
102
108
  }
103
109
 
104
- // createRequire is native in node version >= 12
105
- const require$1 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
110
+ class WebSocketServer extends events.EventEmitter {
111
+ #server;
112
+ #clients = [];
106
113
 
107
- const nodeDataChannel = require$1('../build/Release/node_datachannel.node');
114
+ constructor(options) {
115
+ super();
116
+ this.#server = new nodeDataChannel.WebSocketServer(options);
117
+
118
+ this.#server.onClient((client) => {
119
+ this.emit('client', client);
120
+ this.#clients.push(client);
121
+ });
122
+ }
123
+
124
+ port() {
125
+ return this.#server?.port() || 0;
126
+ }
127
+
128
+ stop() {
129
+ this.#clients.forEach((client) => {
130
+ client?.close();
131
+ });
132
+ this.#server?.stop();
133
+ this.#server = null;
134
+ this.removeAllListeners();
135
+ }
136
+
137
+ onClient(cb) {
138
+ if (this.#server) this.on('client', cb);
139
+ }
140
+ }
108
141
 
109
142
  const {
110
143
  initLogger,
@@ -118,14 +151,8 @@ const {
118
151
  DataChannel,
119
152
  PeerConnection,
120
153
  WebSocket,
121
- WebSocketServer,
122
154
  } = nodeDataChannel;
123
155
 
124
- var index = {
125
- ...nodeDataChannel,
126
- DataChannelStream,
127
- };
128
-
129
156
  const DescriptionType = {
130
157
  Unspec: 'unspec',
131
158
  Offer: 'offer',
@@ -134,6 +161,22 @@ const DescriptionType = {
134
161
  Rollback: 'rollback',
135
162
  };
136
163
 
164
+ var index = {
165
+ initLogger,
166
+ cleanup,
167
+ preload,
168
+ setSctpSettings,
169
+ RtcpReceivingSession,
170
+ Track,
171
+ Video,
172
+ Audio,
173
+ DataChannel,
174
+ PeerConnection,
175
+ WebSocket,
176
+ WebSocketServer,
177
+ DataChannelStream,
178
+ };
179
+
137
180
  exports.Audio = Audio;
138
181
  exports.DataChannel = DataChannel;
139
182
  exports.DataChannelStream = DataChannelStream;