diff --git a/src/server.js b/src/server.js index a09552a0..d41d4788 100644 --- a/src/server.js +++ b/src/server.js @@ -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'; @@ -92,8 +93,8 @@ class Server extends EventTarget { } /* - * Remove event listener - */ + * Remove event listener + */ off(type, callback) { this.removeEventListener(type, callback); } @@ -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 }) diff --git a/src/socket-io.js b/src/socket-io.js index c1a24e66..43bf69a1 100644 --- a/src/socket-io.js +++ b/src/socket-io.js @@ -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 */