-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #273 from murat-dogan/websocket-examples
Convert ws to Native WebSockets
- Loading branch information
Showing
10 changed files
with
106 additions
and
103 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// createRequire is native in node version >= 12 | ||
import { createRequire } from 'module'; | ||
const require = createRequire(import.meta.url); | ||
|
||
const nodeDataChannel = require('../build/Release/node_datachannel.node'); | ||
|
||
export default nodeDataChannel; |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { EventEmitter } from 'events'; | ||
import nodeDataChannel from './node-datachannel.js'; | ||
|
||
export default class WebSocketServer extends EventEmitter { | ||
#server; | ||
#clients = []; | ||
|
||
constructor(options) { | ||
super(); | ||
this.#server = new nodeDataChannel.WebSocketServer(options); | ||
|
||
this.#server.onClient((client) => { | ||
this.emit('client', client); | ||
this.#clients.push(client); | ||
}); | ||
} | ||
|
||
port() { | ||
return this.#server?.port() || 0; | ||
} | ||
|
||
stop() { | ||
this.#clients.forEach((client) => { | ||
client?.close(); | ||
}); | ||
this.#server?.stop(); | ||
this.#server = null; | ||
this.removeAllListeners(); | ||
} | ||
|
||
onClient(cb) { | ||
if (this.#server) this.on('client', cb); | ||
} | ||
} |
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