Skip to content

Commit b80cc65

Browse files
authored
Merge pull request #65 from janusvr/multiprocessing
Method pipelines
2 parents 9cbdb66 + f8f9166 commit b80cc65

12 files changed

Lines changed: 288 additions & 233 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: node_js
22
node_js:
33
- "6"
44
- "7"
5+
- "8"
56
services:
67
- mysql
78
- redis-server

src/MethodMiddleware/chat.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ## user chat ##
2+
function chat(message, next) {
3+
4+
var data = {
5+
roomId: this.currentRoom.id,
6+
userId: this.id,
7+
message: message
8+
};
9+
10+
this.currentRoom.emit('user_chat', data);
11+
return next();
12+
};
13+
14+
module.exports = [chat];
15+

src/MethodMiddleware/enter_room.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function enterRoom(data, next) {
2+
if(data.roomId === undefined) {
3+
this.clientError('Missing roomId in data packet');
4+
return;
5+
}
6+
7+
var oldRoomId = null;
8+
if(this.currentRoom) {
9+
oldRoomId = this.currentRoom.id;
10+
this.currentRoom.emit('user_leave', {
11+
userId: this.id,
12+
roomId: this.currentRoom.id,
13+
newRoomId: data.roomId
14+
});
15+
}
16+
//this._server._plugins.call("enter_room", data);
17+
// TODO
18+
this._server._userList[this.id].oldRoomId = oldRoomId;
19+
this._server._userList[this.id].roomId = data.roomId;
20+
if ((data.partyMode == true) || (data.partyMode == "true")) {
21+
if (this._server._partyList[this.id] === undefined) {
22+
this._server._partyList[this.id] = {};
23+
24+
}
25+
if ((data.roomUrl !== undefined) && (data.roomUrl.match('^https?://'))){
26+
this._server._partyList[this.id].roomId = data.roomId;
27+
this._server._partyList[this.id].roomUrl = data.roomUrl;
28+
this._server._partyList[this.id].roomName = (data.roomName === undefined) ? "" : data.roomName;
29+
this._server._partyList[this.id].client_version = this.client_version;
30+
}
31+
} else {
32+
delete this._server._partyList[this.id];
33+
}
34+
this._server.savePartyList();
35+
this.currentRoom = this._server.getRoom(data.roomId);
36+
this.currentRoom.emit('user_enter', {
37+
userId: this.id,
38+
roomId: data.roomId,
39+
oldRoomId: oldRoomId
40+
});
41+
return next();
42+
}
43+
44+
module.exports = [enterRoom];

src/MethodMiddleware/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var norm = require('path').join(__dirname);
2+
var fs = require('fs');
3+
var middleware = require('../Middleware.js');
4+
5+
var imported = {};
6+
7+
var files = fs.readdirSync(norm);
8+
let jsfiles = files.filter(x => {
9+
let split = x.split('.');
10+
return (split[split.length - 1] === 'js' && split[0] !== 'index')
11+
});
12+
13+
var modules = jsfiles.map(x => x.split('.')[0]);
14+
15+
modules.forEach(method => {
16+
imported[method] = require("./"+method);
17+
});
18+
19+
function getMiddleware(context) {
20+
var keys = Object.keys(imported);
21+
var methods = {};
22+
keys.forEach((key) => {
23+
methods[key] = (data) => {
24+
var tasks = imported[key].map(x => x.bind(context));
25+
return middleware(data, tasks, () => {});
26+
};
27+
});
28+
return methods;
29+
}
30+
module.exports = getMiddleware;

src/MethodMiddleware/logon.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function validateLogon(data, next) {
2+
if(typeof data.userId !== "string" || data.userId === '')
3+
return this.clientError('Missing userId in data packet');
4+
5+
if (!data.userId.match('^[a-zA-Z0-9_]+$'))
6+
return this.clientError('illegal character in user name, only use alphanumeric and underscore');;
7+
8+
if(data.roomId === undefined)
9+
return this.clientError('Missing roomId in data packet');;
10+
return next();
11+
}
12+
13+
function checkNameFree(data, next) {
14+
this._server.isNameFree(data.userId, (err, free) => {
15+
if (!free) return this.clientError('User name is already in use');;
16+
return next();
17+
});
18+
}
19+
20+
function setLogonData(data, next) {
21+
this.id = data.userId;
22+
this._authed = true;
23+
this.client_version =
24+
(data.version === undefined)?"undefined":data.version;
25+
26+
// TODO
27+
this._server._userList[data.userId] = {
28+
roomId: data.roomId,
29+
}
30+
return next();
31+
}
32+
33+
function setRoomData(data, next) {
34+
this.currentRoom = this._server.getRoom(data.roomId);
35+
log.info('User: ' + this.id + ' logged on');
36+
return next();
37+
}
38+
39+
function subscribe(data, next) {
40+
if(data.roomId === undefined)
41+
return this.clientError('Missing roomId in data packet');
42+
43+
var room = this._server.getRoom(data.roomId);
44+
45+
if(this._rooms.indexOf(room) === -1) {
46+
room.addSession(this);
47+
this._rooms.push(room);
48+
}
49+
this.clientOkay();
50+
return next();
51+
}
52+
module.exports = [validateLogon, checkNameFree, setLogonData, setRoomData, subscribe];

src/MethodMiddleware/move.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// ## user move ##
2+
function move(position, next) {
3+
4+
var data = {
5+
roomId: this.currentRoom.id,
6+
userId: this.id,
7+
position: position
8+
};
9+
10+
this.currentRoom.emit('user_moved', data);
11+
return next();
12+
};
13+
14+
module.exports = [move];

src/MethodMiddleware/portal.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function portal(portal, next) {
2+
3+
//TODO: Persist portals
4+
5+
var data = {
6+
roomId: this.currentRoom.id,
7+
userId: this.id,
8+
url: portal.url,
9+
pos: portal.pos,
10+
fwd: portal.fwd
11+
};
12+
13+
this.currentRoom.emit('user_portal', data);
14+
this.clientOkay();
15+
return next();
16+
};
17+
18+
module.exports = [portal];

src/MethodMiddleware/subscribe.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function subscribe(data, next) {
2+
3+
if(data.roomId === undefined)
4+
return this.clientError('Missing roomId in data packet');
5+
6+
var room = this._server.getRoom(data.roomId);
7+
8+
if(this._rooms.indexOf(room) === -1) {
9+
room.addSession(this);
10+
this._rooms.push(room);
11+
}
12+
13+
this.clientOkay();
14+
return next();
15+
};
16+
17+
module.exports = [subscribe];
18+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
function unsubscribe(data, next) {
3+
4+
if(data.roomId === undefined)
5+
return this.clientError('Missing roomId in data packet');
6+
7+
var room = this._server.getRoom(data.roomId);
8+
var i = this._rooms.indexOf(room);
9+
if(i !== -1) {
10+
room.removeSession(this);
11+
this._rooms.splice(i,1);
12+
}
13+
if (room.isEmpty())
14+
delete this._server._rooms[data.roomId];
15+
16+
this.clientOkay();
17+
return next();
18+
};
19+
20+
module.exports = [unsubscribe];
21+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function usersOnline(data, next) {
2+
var maxResults = config.maxUserResults;
3+
var count = 0;
4+
var results = Array();
5+
6+
if(data.maxResults !== undefined && data.maxResults < maxResults) maxResults = data.maxResults;
7+
8+
if(data.roomId === undefined) {
9+
// TODO
10+
for(k in this._server._userList) {
11+
results.push(k);
12+
count++;
13+
if(count >= maxResults) break;
14+
}
15+
}
16+
else {
17+
for(k in this._server._userList) {
18+
if(this._server._userList[k].roomId == data.roomId) {
19+
results.push([k]);
20+
count++;
21+
if(count >= maxResults) break;
22+
}
23+
}
24+
}
25+
26+
json = { "results": count, "roomId": data.roomId, "users": results };
27+
this.send(this.makeMessage('users_online', json));
28+
return next();
29+
}
30+
31+
module.exports = [usersOnline];
32+

0 commit comments

Comments
 (0)