Skip to content

Commit

Permalink
convert ws to native WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
murat-dogan committed Jul 5, 2024
1 parent 12bf3c1 commit c311c11
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 74 deletions.
16 changes: 7 additions & 9 deletions examples/client-server/client-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const require = createRequire(import.meta.url);
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');

import WebSocket from 'ws';
import readline from 'readline';
import nodeDataChannel from '../../lib/index.js';

Expand Down Expand Up @@ -60,23 +59,22 @@ const rl = readline.createInterface({
output: process.stdout,
});

const ws = new WebSocket(wsUrl + '/' + id, {
perMessageDeflate: false,
});
const ws = new nodeDataChannel.WebSocket();
ws.open(wsUrl + '/' + id);

console.log(`The local ID is: ${id}`);
console.log(`Waiting for signaling to be connected...`);

ws.on('open', () => {
ws.onOpen(() => {
console.log('WebSocket connected, signaling ready');
readUserInput();
});

ws.on('error', (err) => {
ws.onError((err) => {
console.log('WebSocket Error: ', err);
});

ws.on('message', (msgStr) => {
ws.onMessage((msgStr) => {
let msg = JSON.parse(msgStr);
switch (msg.type) {
case 'offer':
Expand Down Expand Up @@ -184,10 +182,10 @@ function createPeerConnection(peerId) {
console.log('GatheringState: ', state);
});
peerConnection.onLocalDescription((description, type) => {
ws.send(JSON.stringify({ id: peerId, type, description }));
ws.sendMessage(JSON.stringify({ id: peerId, type, description }));
});
peerConnection.onLocalCandidate((candidate, mid) => {
ws.send(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
ws.sendMessage(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
});
peerConnection.onDataChannel((dc) => {
rl.close();
Expand Down
16 changes: 7 additions & 9 deletions examples/client-server/client-periodic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import WebSocket from 'ws';
import readline from 'readline';
import nodeDataChannel from '../../lib/index.js';

Expand All @@ -25,23 +24,22 @@ const rl = readline.createInterface({

// Signaling Server
const WS_URL = process.env.WS_URL || 'ws://localhost:8000';
const ws = new WebSocket(WS_URL + '/' + id, {
perMessageDeflate: false,
});
const ws = new nodeDataChannel.WebSocket();
ws.open(WS_URL + '/' + id);

console.log(`The local ID is: ${id}`);
console.log(`Waiting for signaling to be connected...`);

ws.on('open', () => {
ws.onOpen(() => {
console.log('WebSocket connected, signaling ready');
readUserInput();
});

ws.on('error', (err) => {
ws.onError((err) => {
console.log('WebSocket Error: ', err);
});

ws.on('message', (msgStr) => {
ws.onMessage((msgStr) => {
let msg = JSON.parse(msgStr);
switch (msg.type) {
case 'offer':
Expand Down Expand Up @@ -122,10 +120,10 @@ function createPeerConnection(peerId) {
console.log('GatheringState: ', state);
});
peerConnection.onLocalDescription((description, type) => {
ws.send(JSON.stringify({ id: peerId, type, description }));
ws.sendMessage(JSON.stringify({ id: peerId, type, description }));
});
peerConnection.onLocalCandidate((candidate, mid) => {
ws.send(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
ws.sendMessage(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
});
peerConnection.onDataChannel((dc) => {
rl.close();
Expand Down
16 changes: 7 additions & 9 deletions examples/client-server/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import WebSocket from 'ws';
import readline from 'readline';
import nodeDataChannel from '../../lib/index.js';

Expand All @@ -13,23 +12,22 @@ const id = randomId(4);

// Signaling Server
const WS_URL = process.env.WS_URL || 'ws://localhost:8000';
const ws = new WebSocket(WS_URL + '/' + id, {
perMessageDeflate: false,
});
const ws = new nodeDataChannel.WebSocket();
ws.open(WS_URL + '/' + id);

console.log(`The local ID is: ${id}`);
console.log(`Waiting for signaling to be connected...`);

ws.on('open', () => {
ws.onOpen(() => {
console.log('WebSocket connected, signaling ready');
readUserInput();
});

ws.on('error', (err) => {
ws.onError((err) => {
console.log('WebSocket Error: ', err);
});

ws.on('message', (msgStr) => {
ws.onMessage((msgStr) => {
let msg = JSON.parse(msgStr);
switch (msg.type) {
case 'offer':
Expand Down Expand Up @@ -86,10 +84,10 @@ function createPeerConnection(peerId) {
console.log('GatheringState: ', state);
});
peerConnection.onLocalDescription((description, type) => {
ws.send(JSON.stringify({ id: peerId, type, description }));
ws.sendMessage(JSON.stringify({ id: peerId, type, description }));
});
peerConnection.onLocalCandidate((candidate, mid) => {
ws.send(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
ws.sendMessage(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
});
peerConnection.onDataChannel((dc) => {
console.log('DataChannel from ' + peerId + ' received with label "', dc.getLabel() + '"');
Expand Down
27 changes: 0 additions & 27 deletions examples/client-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/client-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"ws": "^7.5.3",
"yargs": "^16.2.0"
}
}
54 changes: 35 additions & 19 deletions examples/client-server/signaling-server.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
import WebSocket from 'ws';
import nodeDataChannel from '../../lib/index.js';

// Init Logger
nodeDataChannel.initLogger('Debug');

const clients = {};

const wss = new WebSocket.Server({ port: 8000 });
const wsServer = new nodeDataChannel.WebSocketServer({ bindAddress: '127.0.0.1', port: 8000 });

wsServer.onClient((ws) => {
// path workaround
setTimeout(() => {
const id = ws.path().replace('/', '');
console.log(`New Connection from ${id}`);

wss.on('connection', (ws, req) => {
const id = req.url.replace('/', '');
console.log(`New Connection from ${id}`);
clients[id] = ws;
ws.onMessage((buffer) => {
let msg = JSON.parse(buffer);
let peerId = msg.id;
let peerWs = clients[peerId];

clients[id] = ws;
ws.on('message', (buffer) => {
let msg = JSON.parse(buffer);
let peerId = msg.id;
let peerWs = clients[peerId];
console.log(`Message from ${id} to ${peerId} : ${buffer}`);
if (!peerWs) return console.error(`Can not find peer with ID ${peerId}`);

console.log(`Message from ${id} to ${peerId} : ${buffer}`);
if (!peerWs) return console.error(`Can not find peer with ID ${peerId}`);
msg.id = id;
peerWs.sendMessage(JSON.stringify(msg));
});

msg.id = id;
peerWs.send(JSON.stringify(msg));
});
ws.onClosed(() => {
console.log(`${id} disconnected`);
delete clients[id];
});

ws.on('close', () => {
console.log(`${id} disconected`);
delete clients[id];
});
ws.onError((err) => {
console.error(err);
});
}, 100);
});

// There is a bug in the library that causes the WebSocketServer to be garbage collected
// This is a workaround to keep it alive
setInterval(() => {
console.log(wsServer);
}, 60 * 60 * 60);

0 comments on commit c311c11

Please sign in to comment.