Skip to content

Commit

Permalink
allowSynchronousEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
dimdenGD committed Oct 5, 2024
1 parent 2484d65 commit 279b8c2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Below is the list of supported features and their compatibility:
`ws` options:

- ✅ autoPong (maps to uWS `sendPingsAutomatically`, default `true`)
- allowSynchronousEvents
- allowSynchronousEvents (low performance when `false`, default `true`)
- ❌ backlog
- ✅ clientTracking
- ✅ handleProtocols
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ultimate-ws",
"version": "1.0.4",
"version": "1.0.5",
"description": "The Ultimate WebSocket server. ws-compatible server, based on uWebSockets.",
"main": "src/index.js",
"directories": {
Expand Down
27 changes: 24 additions & 3 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ module.exports = class WebSocketServer extends EventEmitter {
if(typeof options.WebSocket === 'undefined') {
options.WebSocket = WebSocket;
}
if(typeof options.allowSynchronousEvents === 'undefined') {
options.allowSynchronousEvents = true;
}
if(typeof options.closeOnBackpressureLimit === 'undefined') {
options.closeOnBackpressureLimit = false;
}
Expand Down Expand Up @@ -167,13 +170,31 @@ module.exports = class WebSocketServer extends EventEmitter {
ws.client.bufferIncomingMessage(message, isBinary);
return;
}
ws.client.emit("message", ws.client.parseMessage(message, isBinary), isBinary);
if(this.options.allowSynchronousEvents) {
ws.client.emit("message", ws.client.parseMessage(message, isBinary), isBinary);
} else {
setImmediate(() => {
ws.client.emit("message", ws.client.parseMessage(message, isBinary), isBinary);
});
}
},
ping: (ws, message) => {
ws.client.emit("ping", ws.client.parseMessage(message));
if(this.options.allowSynchronousEvents) {
ws.client.emit("ping", ws.client.parseMessage(message));
} else {
setImmediate(() => {
ws.client.emit("ping", ws.client.parseMessage(message));
});
}
},
pong: (ws, message) => {
ws.client.emit("pong", ws.client.parseMessage(message));
if(this.options.allowSynchronousEvents) {
ws.client.emit("pong", ws.client.parseMessage(message));
} else {
setImmediate(() => {
ws.client.emit("pong", ws.client.parseMessage(message));
});
}
},
dropped: (ws, message, isBinary) => {
ws.client.emit("dropped", ws.client.parseMessage(message), isBinary);
Expand Down

0 comments on commit 279b8c2

Please sign in to comment.