Skip to content

Commit

Permalink
Do not normalize data when emitting to socket.io sockets (#376)
Browse files Browse the repository at this point in the history
* Do not normalize data when emitting to socket.io sockets

* Readability improvements
  • Loading branch information
Frank3K authored Feb 14, 2023
1 parent ff4a5ee commit 631fc3a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import URL from 'url-parse';
import WebSocket from './websocket';
import { SocketIO } from './socket-io';
import dedupe from './helpers/dedupe';
import EventTarget from './event/target';
import { CLOSE_CODES } from './constants';
Expand Down Expand Up @@ -92,8 +93,8 @@ class Server extends EventTarget {
}

/*
* Remove event listener
*/
* Remove event listener
*/
off(type, callback) {
this.removeEventListener(type, callback);
}
Expand Down Expand Up @@ -139,29 +140,31 @@ class Server extends EventTarget {
websockets = networkBridge.websocketsLookup(this.url);
}

let normalizedData;
if (typeof options !== 'object' || arguments.length > 3) {
data = Array.prototype.slice.call(arguments, 1, arguments.length);
data = data.map(item => normalizeSendData(item));
normalizedData = data.map(item => normalizeSendData(item));
} else {
data = normalizeSendData(data);
normalizedData = normalizeSendData(data);
}

websockets.forEach(socket => {
if (Array.isArray(data)) {
const messageData = socket instanceof SocketIO ? data : normalizedData;
if (Array.isArray(messageData)) {
socket.dispatchEvent(
createMessageEvent({
type: event,
data,
data: messageData,
origin: this.url,
target: socket.target
}),
...data
...messageData
);
} else {
socket.dispatchEvent(
createMessageEvent({
type: event,
data,
data: messageData,
origin: this.url,
target: socket.target
})
Expand Down
2 changes: 1 addition & 1 deletion src/socket-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createEvent, createMessageEvent, createCloseEvent } from './event/facto
*
* http://socket.io/docs/
*/
class SocketIO extends EventTarget {
export class SocketIO extends EventTarget {
/*
* @param {string} url
*/
Expand Down

0 comments on commit 631fc3a

Please sign in to comment.