-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12bf3c1
commit c311c11
Showing
6 changed files
with
56 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"ws": "^7.5.3", | ||
"yargs": "^16.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |