Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket_io_abstraction wraps emitted arguments in a single object. #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions src/server/_socket_io_abstraction_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@
it("emits event when a client connects", async function() {
let socket;

const eventPromise = new Promise((resolve, reject) => {
socketIoAbstraction.once(SocketIoAbstraction.CLIENT_CONNECT, (clientId) => {
resolve(clientId);
});
const eventPromise = new Promise((resolve) => {
socketIoAbstraction.once(SocketIoAbstraction.CLIENT_CONNECT, resolve);
});

socket = await socketIoClient.createSocket();
const clientId = await eventPromise;
const { clientId } = await eventPromise;
assert.equal(clientId, socket.id, "client ID");

await socketIoClient.closeSocket(socket);
Expand All @@ -71,25 +69,21 @@
const socket = await socketIoClient.createSocket();
const socketId = socket.id;

const eventPromise = new Promise((resolve, reject) => {
socketIoAbstraction.once(SocketIoAbstraction.CLIENT_DISCONNECT, (clientId) => {
resolve(clientId);
});
const eventPromise = new Promise((resolve) => {
socketIoAbstraction.once(SocketIoAbstraction.CLIENT_DISCONNECT, resolve);
});

await socketIoClient.closeSocket(socket);
const clientId = await eventPromise;
const { clientId } = await eventPromise;
assert.equal(clientId, socketId, "client ID");
});

it("emits event when a Socket.IO event is received from client", async function() {
const socket = await socketIoClient.createSocket();
const eventToSend = new ClientRemovePointerEvent();

const eventPromise = new Promise((resolve, reject) => {
socketIoAbstraction.once(SocketIoAbstraction.CLIENT_EVENT_RECEIVED, (clientId, receivedEvent) => {
resolve({ clientId, receivedEvent });
});
const eventPromise = new Promise((resolve) => {
socketIoAbstraction.once(SocketIoAbstraction.CLIENT_EVENT_RECEIVED, resolve);
});

socket.emit(eventToSend.name(), eventToSend.payload());
Expand Down Expand Up @@ -170,10 +164,8 @@
});

function listenForOneClientSocketEvent(socket, event) {
return new Promise((resolve, reject) => {
socket.once(event.name(), (eventPayload) => {
resolve(eventPayload);
});
return new Promise((resolve) => {
socket.once(event.name(), resolve);
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/server/real_time_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
RealTimeServer.CLIENT_TIMEOUT = CLIENT_TIMEOUT;

function handleRealTimeEvents(self) {
self._socketIoAbstraction.on(SocketIoAbstraction.CLIENT_CONNECT, (clientId) => {
self._socketIoAbstraction.on(SocketIoAbstraction.CLIENT_CONNECT, ({ clientId }) => {
replayPreviousEvents(self, clientId);
handleClientEvents(self);

Expand All @@ -85,7 +85,7 @@
});
}, 100);

self._socketIoAbstraction.on(SocketIoAbstraction.CLIENT_CONNECT, (clientId) => {
self._socketIoAbstraction.on(SocketIoAbstraction.CLIENT_CONNECT, ({ clientId }) => {
self._lastActivity[clientId] = self._clock.now();

self._socketIoAbstraction.on(SocketIoAbstraction.CLIENT_EVENT_RECEIVED, () => {
Expand All @@ -105,8 +105,8 @@
}

function handleClientEvents(self) {
self._socketIoAbstraction.on(SocketIoAbstraction.CLIENT_EVENT_RECEIVED, (clientId, clientEvent) => {
processClientEvent(self, clientId, clientEvent);
self._socketIoAbstraction.on(SocketIoAbstraction.CLIENT_EVENT_RECEIVED, (args) => {
processClientEvent(self, args.clientId, args.receivedEvent);
});
}

Expand Down
18 changes: 11 additions & 7 deletions src/server/socket_io_abstraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,25 @@
// Inspired by isaacs
// https://github.com/isaacs/server-destroy/commit/71f1a988e1b05c395e879b18b850713d1774fa92
ioServer.on("connection", function(socket) {
const key = socket.id;
connections[key] = socket;
const clientId = socket.id;
connections[clientId] = socket;
socket.on("disconnect", function() {
delete connections[key];
self.emit(SocketIoAbstraction.CLIENT_DISCONNECT, key);
delete connections[clientId];
self.emit(SocketIoAbstraction.CLIENT_DISCONNECT, { clientId });
});
self.emit(SocketIoAbstraction.CLIENT_CONNECT, key, socket);
self.emit(SocketIoAbstraction.CLIENT_CONNECT, { clientId });
});
}
}

function listenForClientEvents(self, ioServer) {
ioServer.on("connect", (socket) => {
SUPPORTED_EVENTS.forEach(function(eventConstructor) {
socket.on(eventConstructor.EVENT_NAME, function(payload) {
self.emit(SocketIoAbstraction.CLIENT_EVENT_RECEIVED, socket.id, eventConstructor.fromPayload(payload));
let args = {
clientId: socket.id,
receivedEvent: eventConstructor.fromPayload(payload)
};
self.emit(SocketIoAbstraction.CLIENT_EVENT_RECEIVED, args);
});
});
});
Expand Down