|
| 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]; |
0 commit comments