uniwrtc 1.0.0 → 1.0.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.
- package/README.md +27 -16
- package/client-browser.js +11 -8
- package/client.js +5 -5
- package/demo.html +7 -7
- package/package.json +1 -1
- package/server.js +3 -4
- package/src/client-cloudflare.js +6 -6
- package/src/room.js +3 -4
package/README.md
CHANGED
|
@@ -16,19 +16,21 @@ A universal WebRTC signaling service that provides a simple and flexible WebSock
|
|
|
16
16
|
|
|
17
17
|
### Installation
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
#### From npm (recommended)
|
|
20
20
|
```bash
|
|
21
|
-
|
|
22
|
-
cd UniWRTC
|
|
21
|
+
npm install uniwrtc
|
|
23
22
|
```
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
Run the bundled server locally (installed binary is `uniwrtc` via npm scripts):
|
|
26
25
|
```bash
|
|
27
|
-
|
|
26
|
+
npx uniwrtc start # or: node server.js if using the cloned repo
|
|
28
27
|
```
|
|
29
28
|
|
|
30
|
-
|
|
29
|
+
#### From source
|
|
31
30
|
```bash
|
|
31
|
+
git clone https://github.com/draeder/UniWRTC.git
|
|
32
|
+
cd UniWRTC
|
|
33
|
+
npm install
|
|
32
34
|
npm start
|
|
33
35
|
```
|
|
34
36
|
|
|
@@ -160,6 +162,15 @@ The signaling server accepts WebSocket connections and supports the following me
|
|
|
160
162
|
|
|
161
163
|
### Client Library Usage
|
|
162
164
|
|
|
165
|
+
Use directly from npm:
|
|
166
|
+
```javascript
|
|
167
|
+
// ESM
|
|
168
|
+
import { UniWRTCClient } from 'uniwrtc/client-browser.js';
|
|
169
|
+
// or CommonJS
|
|
170
|
+
const { UniWRTCClient } = require('uniwrtc/client-browser.js');
|
|
171
|
+
// For Node.js signaling client use 'uniwrtc/client.js'
|
|
172
|
+
```
|
|
173
|
+
|
|
163
174
|
The `client.js` library provides a convenient wrapper for the signaling protocol:
|
|
164
175
|
|
|
165
176
|
```javascript
|
|
@@ -177,22 +188,22 @@ client.on('joined', (data) => {
|
|
|
177
188
|
});
|
|
178
189
|
|
|
179
190
|
client.on('peer-joined', (data) => {
|
|
180
|
-
console.log('New peer joined:', data.peerId
|
|
191
|
+
console.log('New peer joined:', data.peerId);
|
|
181
192
|
// Initiate WebRTC connection with new peer
|
|
182
193
|
});
|
|
183
194
|
|
|
184
195
|
client.on('offer', (data) => {
|
|
185
|
-
console.log('Received offer from:', data.peerId
|
|
196
|
+
console.log('Received offer from:', data.peerId);
|
|
186
197
|
// Handle WebRTC offer
|
|
187
198
|
});
|
|
188
199
|
|
|
189
200
|
client.on('answer', (data) => {
|
|
190
|
-
console.log('Received answer from:', data.peerId
|
|
201
|
+
console.log('Received answer from:', data.peerId);
|
|
191
202
|
// Handle WebRTC answer
|
|
192
203
|
});
|
|
193
204
|
|
|
194
205
|
client.on('ice-candidate', (data) => {
|
|
195
|
-
console.log('Received ICE candidate from:', data.peerId
|
|
206
|
+
console.log('Received ICE candidate from:', data.peerId);
|
|
196
207
|
// Add ICE candidate to peer connection
|
|
197
208
|
});
|
|
198
209
|
|
|
@@ -242,7 +253,7 @@ function createPeerConnection(peerId) {
|
|
|
242
253
|
|
|
243
254
|
// Handle new peer
|
|
244
255
|
client.on('peer-joined', async (data) => {
|
|
245
|
-
const pc = createPeerConnection(data.
|
|
256
|
+
const pc = createPeerConnection(data.peerId);
|
|
246
257
|
|
|
247
258
|
// Add local tracks
|
|
248
259
|
const stream = await navigator.mediaDevices.getUserMedia({
|
|
@@ -254,22 +265,22 @@ client.on('peer-joined', async (data) => {
|
|
|
254
265
|
// Create and send offer
|
|
255
266
|
const offer = await pc.createOffer();
|
|
256
267
|
await pc.setLocalDescription(offer);
|
|
257
|
-
client.sendOffer(offer, data.
|
|
268
|
+
client.sendOffer(offer, data.peerId);
|
|
258
269
|
});
|
|
259
270
|
|
|
260
271
|
// Handle incoming offer
|
|
261
272
|
client.on('offer', async (data) => {
|
|
262
|
-
const pc = createPeerConnection(data.
|
|
273
|
+
const pc = createPeerConnection(data.peerId);
|
|
263
274
|
|
|
264
275
|
await pc.setRemoteDescription(data.offer);
|
|
265
276
|
const answer = await pc.createAnswer();
|
|
266
277
|
await pc.setLocalDescription(answer);
|
|
267
|
-
client.sendAnswer(answer, data.
|
|
278
|
+
client.sendAnswer(answer, data.peerId);
|
|
268
279
|
});
|
|
269
280
|
|
|
270
281
|
// Handle incoming answer
|
|
271
282
|
client.on('answer', async (data) => {
|
|
272
|
-
const pc = peerConnections.get(data.
|
|
283
|
+
const pc = peerConnections.get(data.peerId);
|
|
273
284
|
if (pc) {
|
|
274
285
|
await pc.setRemoteDescription(data.answer);
|
|
275
286
|
}
|
|
@@ -277,7 +288,7 @@ client.on('answer', async (data) => {
|
|
|
277
288
|
|
|
278
289
|
// Handle ICE candidates
|
|
279
290
|
client.on('ice-candidate', async (data) => {
|
|
280
|
-
const pc = peerConnections.get(data.
|
|
291
|
+
const pc = peerConnections.get(data.peerId);
|
|
281
292
|
if (pc) {
|
|
282
293
|
await pc.addIceCandidate(data.candidate);
|
|
283
294
|
}
|
package/client-browser.js
CHANGED
|
@@ -122,7 +122,7 @@ class UniWRTCClient {
|
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
sendOffer(
|
|
125
|
+
sendOffer(offer, targetId) {
|
|
126
126
|
this.send({
|
|
127
127
|
type: 'offer',
|
|
128
128
|
offer: offer,
|
|
@@ -131,7 +131,7 @@ class UniWRTCClient {
|
|
|
131
131
|
});
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
sendAnswer(
|
|
134
|
+
sendAnswer(answer, targetId) {
|
|
135
135
|
this.send({
|
|
136
136
|
type: 'answer',
|
|
137
137
|
answer: answer,
|
|
@@ -140,7 +140,7 @@ class UniWRTCClient {
|
|
|
140
140
|
});
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
sendIceCandidate(
|
|
143
|
+
sendIceCandidate(candidate, targetId) {
|
|
144
144
|
this.send({
|
|
145
145
|
type: 'ice-candidate',
|
|
146
146
|
candidate: candidate,
|
|
@@ -197,14 +197,12 @@ class UniWRTCClient {
|
|
|
197
197
|
break;
|
|
198
198
|
case 'peer-joined':
|
|
199
199
|
this.emit('peer-joined', {
|
|
200
|
-
peerId: message.peerId
|
|
201
|
-
clientId: message.clientId
|
|
200
|
+
peerId: message.peerId
|
|
202
201
|
});
|
|
203
202
|
break;
|
|
204
203
|
case 'peer-left':
|
|
205
204
|
this.emit('peer-left', {
|
|
206
|
-
peerId: message.peerId
|
|
207
|
-
clientId: message.clientId
|
|
205
|
+
peerId: message.peerId
|
|
208
206
|
});
|
|
209
207
|
break;
|
|
210
208
|
case 'offer':
|
|
@@ -238,7 +236,7 @@ class UniWRTCClient {
|
|
|
238
236
|
case 'chat':
|
|
239
237
|
this.emit('chat', {
|
|
240
238
|
text: message.text,
|
|
241
|
-
|
|
239
|
+
peerId: message.peerId,
|
|
242
240
|
roomId: message.roomId
|
|
243
241
|
});
|
|
244
242
|
break;
|
|
@@ -247,3 +245,8 @@ class UniWRTCClient {
|
|
|
247
245
|
}
|
|
248
246
|
}
|
|
249
247
|
}
|
|
248
|
+
|
|
249
|
+
// Attach to window for non-module script usage
|
|
250
|
+
if (typeof window !== 'undefined') {
|
|
251
|
+
window.UniWRTCClient = UniWRTCClient;
|
|
252
|
+
}
|
package/client.js
CHANGED
|
@@ -159,29 +159,29 @@ class UniWRTCClient {
|
|
|
159
159
|
break;
|
|
160
160
|
case 'peer-joined':
|
|
161
161
|
this.emit('peer-joined', {
|
|
162
|
-
|
|
162
|
+
peerId: message.peerId
|
|
163
163
|
});
|
|
164
164
|
break;
|
|
165
165
|
case 'peer-left':
|
|
166
166
|
this.emit('peer-left', {
|
|
167
|
-
|
|
167
|
+
peerId: message.peerId
|
|
168
168
|
});
|
|
169
169
|
break;
|
|
170
170
|
case 'offer':
|
|
171
171
|
this.emit('offer', {
|
|
172
|
-
|
|
172
|
+
peerId: message.peerId,
|
|
173
173
|
offer: message.offer
|
|
174
174
|
});
|
|
175
175
|
break;
|
|
176
176
|
case 'answer':
|
|
177
177
|
this.emit('answer', {
|
|
178
|
-
|
|
178
|
+
peerId: message.peerId,
|
|
179
179
|
answer: message.answer
|
|
180
180
|
});
|
|
181
181
|
break;
|
|
182
182
|
case 'ice-candidate':
|
|
183
183
|
this.emit('ice-candidate', {
|
|
184
|
-
|
|
184
|
+
peerId: message.peerId,
|
|
185
185
|
candidate: message.candidate
|
|
186
186
|
});
|
|
187
187
|
break;
|
package/demo.html
CHANGED
|
@@ -460,7 +460,7 @@
|
|
|
460
460
|
updatePeersList(knownPeers);
|
|
461
461
|
|
|
462
462
|
// Update room info with dynamic peer count
|
|
463
|
-
updateRoomInfo(data.roomId,
|
|
463
|
+
updateRoomInfo(data.roomId, client.clientId);
|
|
464
464
|
});
|
|
465
465
|
|
|
466
466
|
client.on('peer-joined', (data) => {
|
|
@@ -481,7 +481,7 @@
|
|
|
481
481
|
});
|
|
482
482
|
|
|
483
483
|
client.on('peer-left', (data) => {
|
|
484
|
-
const peerId = data.peerId
|
|
484
|
+
const peerId = data.peerId;
|
|
485
485
|
log(`Peer left: ${peerId}`, 'warning');
|
|
486
486
|
// Remove from known peers
|
|
487
487
|
knownPeers = knownPeers.filter(id => id !== peerId);
|
|
@@ -511,7 +511,7 @@
|
|
|
511
511
|
await pc.setLocalDescription(answer);
|
|
512
512
|
|
|
513
513
|
// Send answer back
|
|
514
|
-
client.sendAnswer(data.peerId
|
|
514
|
+
client.sendAnswer(answer, data.peerId);
|
|
515
515
|
} catch (e) {
|
|
516
516
|
log(`Offer error: ${e.message}`, 'error');
|
|
517
517
|
}
|
|
@@ -552,8 +552,8 @@
|
|
|
552
552
|
});
|
|
553
553
|
|
|
554
554
|
client.on('chat', (data) => {
|
|
555
|
-
displayChatMessage(data.text, `${data.
|
|
556
|
-
log(`Chat from ${data.
|
|
555
|
+
displayChatMessage(data.text, `${data.peerId.substring(0, 6)}...`, false);
|
|
556
|
+
log(`Chat from ${data.peerId}: ${data.text}`, 'info');
|
|
557
557
|
});
|
|
558
558
|
|
|
559
559
|
await client.connect();
|
|
@@ -635,7 +635,7 @@
|
|
|
635
635
|
pc.onicecandidate = (event) => {
|
|
636
636
|
if (event.candidate) {
|
|
637
637
|
log(`Sending ICE candidate to ${peerId.substring(0, 6)}...`, 'info');
|
|
638
|
-
client.sendIceCandidate(
|
|
638
|
+
client.sendIceCandidate(event.candidate, peerId);
|
|
639
639
|
}
|
|
640
640
|
};
|
|
641
641
|
|
|
@@ -649,7 +649,7 @@
|
|
|
649
649
|
|
|
650
650
|
const offer = await pc.createOffer();
|
|
651
651
|
await pc.setLocalDescription(offer);
|
|
652
|
-
client.sendOffer(
|
|
652
|
+
client.sendOffer(offer, peerId);
|
|
653
653
|
log(`Sent offer to ${peerId.substring(0, 6)}...`, 'success');
|
|
654
654
|
}
|
|
655
655
|
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -208,8 +208,7 @@ function handleJoin(ws, message) {
|
|
|
208
208
|
// Notify other clients in the room
|
|
209
209
|
broadcastToRoom(roomId, {
|
|
210
210
|
type: 'peer-joined',
|
|
211
|
-
peerId: ws.clientId
|
|
212
|
-
clientId: ws.clientId
|
|
211
|
+
peerId: ws.clientId
|
|
213
212
|
}, ws);
|
|
214
213
|
}
|
|
215
214
|
|
|
@@ -228,7 +227,7 @@ function handleLeave(ws, message) {
|
|
|
228
227
|
// Notify other clients
|
|
229
228
|
broadcastToRoom(roomId, {
|
|
230
229
|
type: 'peer-left',
|
|
231
|
-
|
|
230
|
+
peerId: ws.clientId
|
|
232
231
|
});
|
|
233
232
|
|
|
234
233
|
// Clean up empty rooms
|
|
@@ -294,7 +293,7 @@ function handleChat(ws, message) {
|
|
|
294
293
|
client.send(JSON.stringify({
|
|
295
294
|
type: 'chat',
|
|
296
295
|
text: text,
|
|
297
|
-
|
|
296
|
+
peerId: ws.clientId,
|
|
298
297
|
roomId: roomId
|
|
299
298
|
}));
|
|
300
299
|
}
|
package/src/client-cloudflare.js
CHANGED
|
@@ -128,7 +128,7 @@ class UniWRTCClient {
|
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
sendOffer(
|
|
131
|
+
sendOffer(offer, targetId) {
|
|
132
132
|
this.send({
|
|
133
133
|
type: 'offer',
|
|
134
134
|
offer: offer,
|
|
@@ -136,7 +136,7 @@ class UniWRTCClient {
|
|
|
136
136
|
});
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
sendAnswer(
|
|
139
|
+
sendAnswer(answer, targetId) {
|
|
140
140
|
this.send({
|
|
141
141
|
type: 'answer',
|
|
142
142
|
answer: answer,
|
|
@@ -144,7 +144,7 @@ class UniWRTCClient {
|
|
|
144
144
|
});
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
-
sendIceCandidate(
|
|
147
|
+
sendIceCandidate(candidate, targetId) {
|
|
148
148
|
this.send({
|
|
149
149
|
type: 'ice-candidate',
|
|
150
150
|
candidate: candidate,
|
|
@@ -197,12 +197,12 @@ class UniWRTCClient {
|
|
|
197
197
|
break;
|
|
198
198
|
case 'peer-joined':
|
|
199
199
|
this.emit('peer-joined', {
|
|
200
|
-
|
|
200
|
+
peerId: message.peerId
|
|
201
201
|
});
|
|
202
202
|
break;
|
|
203
203
|
case 'peer-left':
|
|
204
204
|
this.emit('peer-left', {
|
|
205
|
-
|
|
205
|
+
peerId: message.peerId
|
|
206
206
|
});
|
|
207
207
|
break;
|
|
208
208
|
case 'offer':
|
|
@@ -236,7 +236,7 @@ class UniWRTCClient {
|
|
|
236
236
|
case 'chat':
|
|
237
237
|
this.emit('chat', {
|
|
238
238
|
text: message.text,
|
|
239
|
-
|
|
239
|
+
peerId: message.peerId,
|
|
240
240
|
roomId: message.roomId
|
|
241
241
|
});
|
|
242
242
|
break;
|
package/src/room.js
CHANGED
|
@@ -89,11 +89,11 @@ export class Room {
|
|
|
89
89
|
|
|
90
90
|
const client = this.clients.get(clientId);
|
|
91
91
|
if (client && client.readyState === WebSocket.OPEN) {
|
|
92
|
-
// Send joined confirmation
|
|
92
|
+
// Send joined confirmation (align with server schema)
|
|
93
93
|
client.send(JSON.stringify({
|
|
94
94
|
type: 'joined',
|
|
95
95
|
roomId: roomId,
|
|
96
|
-
|
|
96
|
+
clientId: clientId,
|
|
97
97
|
clients: peers
|
|
98
98
|
}));
|
|
99
99
|
}
|
|
@@ -101,8 +101,7 @@ export class Room {
|
|
|
101
101
|
// Notify other peers
|
|
102
102
|
this.broadcast({
|
|
103
103
|
type: 'peer-joined',
|
|
104
|
-
peerId:
|
|
105
|
-
clientId: clientId
|
|
104
|
+
peerId: clientId
|
|
106
105
|
}, clientId);
|
|
107
106
|
}
|
|
108
107
|
|