Skip to content

Commit

Permalink
Merge pull request #313 from novoda/BOT-296/socket_io_client
Browse files Browse the repository at this point in the history
BOT-296/Server Modules
  • Loading branch information
ouchadam authored Aug 23, 2017
2 parents 00fd506 + e982951 commit d43a56b
Show file tree
Hide file tree
Showing 22 changed files with 670 additions and 530 deletions.
50 changes: 32 additions & 18 deletions TelepresenceBot/server-unit/core/botLocator.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
function BotLocator(rooms) {
this.rooms = rooms;
}
module.exports = function (locationRooms) {

BotLocator.prototype.locateFirstAvailableBotIn = function(room) {
var botsInRoom = this.rooms[room];

if(!botsInRoom) {
return;
const roomsThatMatch = function (toMatch) {
return function (room) {
return toMatch == room;
};
}

for (socketId in botsInRoom.sockets) {
var socketsInBotRoom = this.rooms[socketId];
const roomsThatContainProperty = function (property) {
return function (roomName) {
return locationRooms[roomName].hasOwnProperty(property);
};
}

if(botNotConnectedToHuman(socketsInBotRoom)) {
return socketId;
}
const roomsThatAreNotAlreadyConnectedToOtherSockets = function () {
return function (roomName) {
return locationRooms[roomName].length == 1;
};
}
}

function botNotConnectedToHuman(socketsInBotRoom) {
return socketsInBotRoom.length === 1;
}
const asEmptyBotRoom = function () {
return function (locationRoom) {
return Object.keys(locationRooms[locationRoom].sockets)
.filter(roomsThatContainProperty('length'))
.filter(roomsThatAreNotAlreadyConnectedToOtherSockets())
.pop();
};
}

module.exports = BotLocator;
return {
locateFirstAvailableBotIn: function (locationRoom) {
return Object.keys(locationRooms)
.filter(roomsThatMatch(locationRoom))
.filter(roomsThatContainProperty('sockets'))
.map(asEmptyBotRoom())
.pop();
}
};
};
16 changes: 7 additions & 9 deletions TelepresenceBot/server-unit/core/clientType.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
ClientType = {
BOT : "bot",
HUMAN : "human",
TEST : "test",
from : function(rawClientType) {
for(var key in this) {
if(ClientType[key] == rawClientType) {
module.exports = ClientType = {
BOT: 'bot',
HUMAN: 'human',
TEST: 'test',
from: function (rawClientType) {
for (let key in this) {
if (ClientType[key] == rawClientType) {
return ClientType[key];
}
}
return undefined;
}
}

module.exports = ClientType;
53 changes: 53 additions & 0 deletions TelepresenceBot/server-unit/core/disconnector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module.exports = function Disconnector(rooms, connectedClients) {

const roomsThatMatch = function (toMatch) {
return function (room) {
return toMatch == room;
};
}

const roomsThatContainProperty = function (property) {
return function (roomName) {
return rooms[roomName].hasOwnProperty(property);
};
}

const thosePresentIn = function (objectToSearch) {
return function (key) {
return objectToSearch[key]
};
}

const asConnectedClient = function () {
return function (key) {
return connectedClients[key];
};
}

const connectedClientsThatContainProperty = function (property) {
return function (connectedClient) {
return connectedClient.hasOwnProperty(property);
}
}

const disconnectClient = function () {
return function (connectedClient) {
connectedClient.disconnect();
}
}

return {
disconnectRoom: function (roomName) {
Object.keys(rooms)
.filter(roomsThatMatch(roomName))
.filter(roomsThatContainProperty('sockets'))
.map(function (roomKey) {
return Object.keys(rooms[roomKey].sockets)
.filter(thosePresentIn(connectedClients))
.map(asConnectedClient())
.filter(connectedClientsThatContainProperty('disconnect'))
.every(disconnectClient());
});
}
};
}
18 changes: 0 additions & 18 deletions TelepresenceBot/server-unit/core/loggingClient.js

This file was deleted.

17 changes: 17 additions & 0 deletions TelepresenceBot/server-unit/core/mover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = function Mover(clientsAndRooms, emitter) {

const emitToRoom = function (direction) {
return function (room) {
emitter.to(room).emit('direction', direction);
}
}

return {
moveIn: function (clientId, direction) {
const rooms = clientsAndRooms[clientId];

Object.keys(rooms || {})
.every(emitToRoom(direction));
}
};
}
7 changes: 7 additions & 0 deletions TelepresenceBot/server-unit/core/observer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function Observer() {
return {
notify: function (eventName, eventData) {
return true;
}
};
}
13 changes: 10 additions & 3 deletions TelepresenceBot/server-unit/core/public/js/connect.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
var intervalId;
const intervalId;

const socketOptions ={
transports: ['websocket'],
'force new connection': true,
query: 'clientType=human&room=London'
};

const socket = io(socketOptions);

$(document).ready(function(){

$("input").mousedown(function(){
console.log("mouseDown");
var message = $(this).val();
const message = $(this).val();
intervalId = setInterval(function() {
sendMessage(message);
}, 100);
});

function sendMessage(message) {
var socket = io();
socket.emit('chat message', message);
$('#messages').append($('<li>').text(message));
}
Expand Down
28 changes: 28 additions & 0 deletions TelepresenceBot/server-unit/core/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const ClientType = require('./clientType.js')

module.exports = function Router(botLocator) {
return {
route: function (query, next) {
const roomName = query.room;
const rawClientType = query.clientType;
const clientType = ClientType.from(rawClientType);

switch (clientType) {
case ClientType.BOT:
return next();
case ClientType.HUMAN:
const availableBot = botLocator.locateFirstAvailableBotIn(roomName);

if (availableBot) {
query.room = availableBot;
return next();
} else {
return next(new Error('No bots available'));
}

default:
return next(new Error('Unrecognised clientType: ' + rawClientType));
}
}
};
}
102 changes: 3 additions & 99 deletions TelepresenceBot/server-unit/core/server.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,4 @@
var io = require('socket.io').listen(5000);
var ClientType = require("./clientType.js");
var LoggingClient = require("./loggingClient.js");
var BotLocator = require("./botLocator.js");
const ServerCreator = require('./serverCreator');

var testClient = new LoggingClient();
var botLocator = new BotLocator(io.sockets.adapter.rooms);

io.use(function(client, next){

var roomName = client.handshake.query.room;
var rawClientType = client.handshake.query.clientType;
var clientType = ClientType.from(rawClientType);

switch(clientType) {
case ClientType.TEST:
case ClientType.BOT:
return next();
case ClientType.HUMAN:
var human = client;
var availableBot = botLocator.locateFirstAvailableBotIn(roomName);

if(availableBot) {
human.handshake.query.room = availableBot;
return next();
} else {
return next(new Error('No bots available'));
}
default:
return next(new Error('Unrecognised clientType: ' + rawClientType));

}

});

io.sockets.on('connection', function (client) {

var roomName = client.handshake.query.room;
var rawClientType = client.handshake.query.clientType;
var clientType = ClientType.from(rawClientType);

switch(clientType) {
case ClientType.HUMAN:
client.join(roomName);
testClient.emit('connected_human', asRoomsWithSocketIds());
break;
case ClientType.BOT:
client.join(roomName);
testClient.emit('connected_bot', asRoomsWithSocketIds());
break;
case ClientType.TEST:
console.log('switching test client');
testClient = new LoggingClient(client);
testClient.emit('connected', asRoomsWithSocketIds());
break;
default:
throw 'Unexpected rawClientType: ' + clientType;
}

client.on('disconnect', function() {
disconnectRoom(client.id);
testClient.emit('disconnected_human', asRoomsWithSocketIds());
testClient.emit('disconnected_bot', asRoomsWithSocketIds());
});

client.on('move_in', function(direction) {
var rooms = Object.keys(io.sockets.adapter.sids[client.id]);
for(var i = 0; i < rooms.length; i++) {
io.to(rooms[i]).emit('direction', direction);
testClient.emit('direction', direction);
}
});
});

function disconnectRoom(name) {
var room = io.sockets.adapter.rooms[name];

if(!room) {
return;
}

var clients = io.sockets.adapter.rooms[name].sockets;

for(var client in clients) {
var connectedClient = io.sockets.connected[client];
connectedClient.disconnect();
}
}

function asRoomsWithSocketIds() {
var roomNames = Object.keys(io.sockets.adapter.rooms);
var roomsWithSockets = {};

for(var i = 0; i < roomNames.length; i++) {
var room = io.sockets.adapter.rooms[roomNames[i]];
roomsWithSockets[roomNames[i]] = Object.keys(room.sockets);
}

return roomsWithSockets;
}
const serverCreator = new ServerCreator();
serverCreator.create();
Loading

0 comments on commit d43a56b

Please sign in to comment.