[WebSocketServer] Allow to extend the original WebSocket class#2007
Merged
lpinca merged 7 commits intowebsockets:masterfrom Feb 1, 2022
vansergen:extend
Merged
[WebSocketServer] Allow to extend the original WebSocket class#2007lpinca merged 7 commits intowebsockets:masterfrom vansergen:extend
WebSocket class#2007lpinca merged 7 commits intowebsockets:masterfrom
vansergen:extend
Conversation
Member
|
I'm not very happy with it. Are there big advantages over monkey patching the const WebSocket = require('ws');
Object.defineProperty(WebSocket.prototype, 'custom_field', {
get() {
return '123';
}
}); |
Contributor
Author
I do believe so. It would give more flexibility in terms of accessing the class CustomWebSocket extends WebSocket {
#sent;
#created_at;
constructor(...params) {
super(...params);
this.#sent = 0;
this.#created_at = new Date();
}
get sent() {
return this.#sent;
}
get created_at() {
return new Date(this.#created_at);
}
send(...params) {
this.#sent++;
super.send(...params);
}
}in the same manner as the import { IncomingMessage, ServerResponse, createServer } from 'node:http';
class CustomIncomingMessage extends IncomingMessage {}
class CustomServerResponse extends ServerResponse {}
createServer({
IncomingMessage: CustomIncomingMessage,
ServerResponse: CustomServerResponse
}).on('request', (request, response) => {
console.log(request instanceof CustomIncomingMessage);
console.log(response instanceof CustomServerResponse);
});In this way one can modify import { createServer } from 'http';
import { parse } from 'url';
import { WebSocketServer, WebSocket } from 'ws';
class WebSocket1 extends WebSocket {}
class WebSocket2 extends WebSocket {}
const server = createServer();
const wss1 = new WebSocketServer({ WebSocket: WebSocket1, noServer: true });
const wss2 = new WebSocketServer({ WebSocket: WebSocket2, noServer: true });
const wss3 = new WebSocketServer({ WebSocket, noServer: true });
wss1.on('connection', function connection(ws) {
// console.log(ws instanceof WebSocket1)
});
wss2.on('connection', function connection(ws) {
// console.log(ws instanceof WebSocket2)
});
wss3.on('connection', function connection(ws) {
// console.log(ws instanceof WebSocket)
});
server.on('upgrade', function upgrade(request, socket, head) {
const { pathname } = parse(request.url);
if (pathname === '/foo') {
wss1.handleUpgrade(request, socket, head, function done(ws) {
wss1.emit('connection', ws, request);
});
} else if (pathname === '/bar') {
wss2.handleUpgrade(request, socket, head, function done(ws) {
wss2.emit('connection', ws, request);
});
} else {
wss3.handleUpgrade(request, socket, head, function done(ws) {
wss3.emit('connection', ws, request);
});
}
});
server.listen(8080); |
Member
|
Hmm, ok. It might break if the class does not extend |
lpinca
reviewed
Feb 1, 2022
Co-authored-by: Luigi Pinca <luigipinca@gmail.com>
Co-authored-by: Luigi Pinca <luigipinca@gmail.com>
lpinca
reviewed
Feb 1, 2022
Co-authored-by: Luigi Pinca <luigipinca@gmail.com>
lpinca
approved these changes
Feb 1, 2022
Member
|
Thank you. |
6 tasks
8 tasks
Closed
8 tasks
2 tasks
8 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@lpinca What do you think about adding this feature?