-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from janusvr/multiprocessing
Method pipelines
- Loading branch information
Showing
12 changed files
with
288 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ language: node_js | |
node_js: | ||
- "6" | ||
- "7" | ||
- "8" | ||
services: | ||
- mysql | ||
- redis-server | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// ## user chat ## | ||
function chat(message, next) { | ||
|
||
var data = { | ||
roomId: this.currentRoom.id, | ||
userId: this.id, | ||
message: message | ||
}; | ||
|
||
this.currentRoom.emit('user_chat', data); | ||
return next(); | ||
}; | ||
|
||
module.exports = [chat]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
function enterRoom(data, next) { | ||
if(data.roomId === undefined) { | ||
this.clientError('Missing roomId in data packet'); | ||
return; | ||
} | ||
|
||
var oldRoomId = null; | ||
if(this.currentRoom) { | ||
oldRoomId = this.currentRoom.id; | ||
this.currentRoom.emit('user_leave', { | ||
userId: this.id, | ||
roomId: this.currentRoom.id, | ||
newRoomId: data.roomId | ||
}); | ||
} | ||
//this._server._plugins.call("enter_room", data); | ||
// TODO | ||
this._server._userList[this.id].oldRoomId = oldRoomId; | ||
this._server._userList[this.id].roomId = data.roomId; | ||
if ((data.partyMode == true) || (data.partyMode == "true")) { | ||
if (this._server._partyList[this.id] === undefined) { | ||
this._server._partyList[this.id] = {}; | ||
|
||
} | ||
if ((data.roomUrl !== undefined) && (data.roomUrl.match('^https?://'))){ | ||
this._server._partyList[this.id].roomId = data.roomId; | ||
this._server._partyList[this.id].roomUrl = data.roomUrl; | ||
this._server._partyList[this.id].roomName = (data.roomName === undefined) ? "" : data.roomName; | ||
this._server._partyList[this.id].client_version = this.client_version; | ||
} | ||
} else { | ||
delete this._server._partyList[this.id]; | ||
} | ||
this._server.savePartyList(); | ||
this.currentRoom = this._server.getRoom(data.roomId); | ||
this.currentRoom.emit('user_enter', { | ||
userId: this.id, | ||
roomId: data.roomId, | ||
oldRoomId: oldRoomId | ||
}); | ||
return next(); | ||
} | ||
|
||
module.exports = [enterRoom]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var norm = require('path').join(__dirname); | ||
var fs = require('fs'); | ||
var middleware = require('../Middleware.js'); | ||
|
||
var imported = {}; | ||
|
||
var files = fs.readdirSync(norm); | ||
let jsfiles = files.filter(x => { | ||
let split = x.split('.'); | ||
return (split[split.length - 1] === 'js' && split[0] !== 'index') | ||
}); | ||
|
||
var modules = jsfiles.map(x => x.split('.')[0]); | ||
|
||
modules.forEach(method => { | ||
imported[method] = require("./"+method); | ||
}); | ||
|
||
function getMiddleware(context) { | ||
var keys = Object.keys(imported); | ||
var methods = {}; | ||
keys.forEach((key) => { | ||
methods[key] = (data) => { | ||
var tasks = imported[key].map(x => x.bind(context)); | ||
return middleware(data, tasks, () => {}); | ||
}; | ||
}); | ||
return methods; | ||
} | ||
module.exports = getMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
function validateLogon(data, next) { | ||
if(typeof data.userId !== "string" || data.userId === '') | ||
return this.clientError('Missing userId in data packet'); | ||
|
||
if (!data.userId.match('^[a-zA-Z0-9_]+$')) | ||
return this.clientError('illegal character in user name, only use alphanumeric and underscore');; | ||
|
||
if(data.roomId === undefined) | ||
return this.clientError('Missing roomId in data packet');; | ||
return next(); | ||
} | ||
|
||
function checkNameFree(data, next) { | ||
this._server.isNameFree(data.userId, (err, free) => { | ||
if (!free) return this.clientError('User name is already in use');; | ||
return next(); | ||
}); | ||
} | ||
|
||
function setLogonData(data, next) { | ||
this.id = data.userId; | ||
this._authed = true; | ||
this.client_version = | ||
(data.version === undefined)?"undefined":data.version; | ||
|
||
// TODO | ||
this._server._userList[data.userId] = { | ||
roomId: data.roomId, | ||
} | ||
return next(); | ||
} | ||
|
||
function setRoomData(data, next) { | ||
this.currentRoom = this._server.getRoom(data.roomId); | ||
log.info('User: ' + this.id + ' logged on'); | ||
return next(); | ||
} | ||
|
||
function subscribe(data, next) { | ||
if(data.roomId === undefined) | ||
return this.clientError('Missing roomId in data packet'); | ||
|
||
var room = this._server.getRoom(data.roomId); | ||
|
||
if(this._rooms.indexOf(room) === -1) { | ||
room.addSession(this); | ||
this._rooms.push(room); | ||
} | ||
this.clientOkay(); | ||
return next(); | ||
} | ||
module.exports = [validateLogon, checkNameFree, setLogonData, setRoomData, subscribe]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// ## user move ## | ||
function move(position, next) { | ||
|
||
var data = { | ||
roomId: this.currentRoom.id, | ||
userId: this.id, | ||
position: position | ||
}; | ||
|
||
this.currentRoom.emit('user_moved', data); | ||
return next(); | ||
}; | ||
|
||
module.exports = [move]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function portal(portal, next) { | ||
|
||
//TODO: Persist portals | ||
|
||
var data = { | ||
roomId: this.currentRoom.id, | ||
userId: this.id, | ||
url: portal.url, | ||
pos: portal.pos, | ||
fwd: portal.fwd | ||
}; | ||
|
||
this.currentRoom.emit('user_portal', data); | ||
this.clientOkay(); | ||
return next(); | ||
}; | ||
|
||
module.exports = [portal]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function subscribe(data, next) { | ||
|
||
if(data.roomId === undefined) | ||
return this.clientError('Missing roomId in data packet'); | ||
|
||
var room = this._server.getRoom(data.roomId); | ||
|
||
if(this._rooms.indexOf(room) === -1) { | ||
room.addSession(this); | ||
this._rooms.push(room); | ||
} | ||
|
||
this.clientOkay(); | ||
return next(); | ||
}; | ||
|
||
module.exports = [subscribe]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
function unsubscribe(data, next) { | ||
|
||
if(data.roomId === undefined) | ||
return this.clientError('Missing roomId in data packet'); | ||
|
||
var room = this._server.getRoom(data.roomId); | ||
var i = this._rooms.indexOf(room); | ||
if(i !== -1) { | ||
room.removeSession(this); | ||
this._rooms.splice(i,1); | ||
} | ||
if (room.isEmpty()) | ||
delete this._server._rooms[data.roomId]; | ||
|
||
this.clientOkay(); | ||
return next(); | ||
}; | ||
|
||
module.exports = [unsubscribe]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
function usersOnline(data, next) { | ||
var maxResults = config.maxUserResults; | ||
var count = 0; | ||
var results = Array(); | ||
|
||
if(data.maxResults !== undefined && data.maxResults < maxResults) maxResults = data.maxResults; | ||
|
||
if(data.roomId === undefined) { | ||
// TODO | ||
for(k in this._server._userList) { | ||
results.push(k); | ||
count++; | ||
if(count >= maxResults) break; | ||
} | ||
} | ||
else { | ||
for(k in this._server._userList) { | ||
if(this._server._userList[k].roomId == data.roomId) { | ||
results.push([k]); | ||
count++; | ||
if(count >= maxResults) break; | ||
} | ||
} | ||
} | ||
|
||
json = { "results": count, "roomId": data.roomId, "users": results }; | ||
this.send(this.makeMessage('users_online', json)); | ||
return next(); | ||
} | ||
|
||
module.exports = [usersOnline]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function middleware(data, tasks, cb) { | ||
function iterate(idx) { | ||
if (idx === tasks.length && typeof cb === "function") | ||
return cb(); | ||
const task = tasks[idx]; | ||
task(data, () => { | ||
iterate(idx + 1) | ||
}); | ||
} | ||
return iterate(0); | ||
} | ||
|
||
module.exports = middleware; |
Oops, something went wrong.