Skip to content

Commit ad7e099

Browse files
author
d3x0r
committed
Improve wrapped websocket somewhat
1 parent 9179a6e commit ad7e099

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

apps/http-ws/server-protocol.mjs

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@ function loopBack( that, to ) {
1515
export class Protocol extends Events {
1616
protocol = null;
1717
server = null;
18+
#opts = null;
19+
static #WS = null;
1820
/**
1921
* @param {object} opts - options for the protocol
2022
*/
2123
constructor( opts ) {
2224
super();
25+
this.#opts = opts|| { resourcePath:"ui", port:Number(process.env.PORT)||4321 };
2326
// resource path is from current working directory (where it ran from)
2427
if( opts && opts.protocol ) Protocol.protocol = opts.portocol;
25-
this.server = openServer( opts || { resourcePath:"ui", port:Number(process.env.PORT)||4321 }
28+
if( "WS" in opts ) {
29+
Protocol.#WS = opts.WS;
30+
}
31+
this.server = openServer( this.#opts
2632
, loopBack( this, this.#accept ), loopBack( this, this.#connect ) );
2733
//this.on( "close", )
2834
}
@@ -46,7 +52,7 @@ export class Protocol extends Events {
4652
}
4753

4854
#connect(ws) {
49-
const myWS = new WS( ws );
55+
const myWS = Protocol.#WS?new Protocol.#WS(ws) : new WS( ws );
5056
const this_ = this;
5157
//console.log( "--------------- NEW CONNECTION ------------------" );
5258
const results = this.on( "connect", [ws, myWS] );
@@ -121,7 +127,7 @@ export class Protocol extends Events {
121127

122128

123129

124-
class WS extends Events{
130+
export class WS extends Events{
125131
ws = null;
126132
constructor(ws){
127133
super();

0 commit comments

Comments
 (0)