Create a gist now

Instantly share code, notes, and snippets.

"use strict";
var User = require('./User.js').User;
var Message = require('./Message.js').Message;
var PrivateMessage = require('./Message.js').PrivateMessage;
var Command = require('./Message.js').Command;
var SessionHandler = require('./SessionHandler.js').SessionHandler;
module.exports.ChatServer = function (port, debug) {
var http = require('http');
var qs = require('querystring');
var url = require("url");
var sessionHandler = new SessionHandler();
var clientCounter = 0;
var port = port ? port : 8888;
var debug = debug ? debug : false;
this.forEachUser = function (callBack) {
sessionHandler.forEachSession(function(session) {
if (!session.user) {
return;
}
callBack(session.user);
});
}
this.getUsers = function () {
var users = new Array();
sessionHandler.forEachSession(function(session) {
if (!session.user || !session.user.loggedin) {
return;
}
users.push(session.user);
});
return users;
}
this.getUserByName = function (name) {
var searchUser = null;
this.forEachUser(function (user) {
if (user.name == name) {
searchUser = user;
}
});
return searchUser;
}
this.removeUser = function (user) {
sessionHandler.deleteSession(user.session);
this.addMessageToAll(new Command('removed_users', new Array(user)));
}
this.addMessageToAll = function (msg) {
this.forEachUser(function (user, i) {
user.addMessage(msg);
});
}
this.command = function (user, command) {
switch (command.name) {
case 'change_name':
this.addMessageToAll(new Command('name_changed', {from: user.name, to: command.params.newName, user: user}));
user.name = command.params.newName;
break;
case 'login':
user.name = command.params.name;
user.loggedin = true;
this.addMessageToAll(new Command('new_user', {user: user}));
user.addMessage(new Command('welcome', command.params));
break;
case 'logout':
this.removeUser(user);
break;
case 'users':
this.addMessageToAll(new Command('users', this.getUsers()));
break;
}
}
this.perform = function (session, msg) {
var user = session.user;
switch (msg.type) {
case 'normal':
this.addMessageToAll(new Message(msg.text, msg.type, user));
break;
case 'private':
var recipient = this.getUserByName(msg.recipient);
var privateMessage = new PrivateMessage(msg.text, user, recipient);
recipient.addMessage(privateMessage);
user.addMessage(privateMessage);
break;
case 'command':
this.command(user, msg);
break;
}
}
var thath = this;
http.createServer(function (request, response) {
var query = url.parse(request.url, true).query;
var session = sessionHandler.getSession(request, response);
if (!session.user) {
session.user = new User('User_' + ++clientCounter, session);
}
if (query.q) {
var clientMessage = JSON.parse(decodeURIComponent(query.q));
thath.perform(session, clientMessage);
}
response.writeHead(200, {'Content-Type': 'application/x-javascript'});
if (query.ChatResponse) {
response.end(query.ChatResponse + '(' + JSON.stringify(session.user.messages()) + ');');
}
else {
response.end(JSON.stringify(session.user.messages()));
}
}).listen(port);
}
module.exports.Message = function (text, type, user) {
var text = text;
var type = ['normal', 'private', 'command'].indexOf(type) !== -1 ? type : 'normal';
var user = user;
var time = new Date().getTime();
this.toString = function (msg) {
return text;
}
this.toJSON = function() {
return {
text: text,
type: type,
user: user,
time: time
};
}
}
module.exports.PrivateMessage = function (text, user, recipient) {
var text = text;
var type = 'private';
var recipient = recipient;
var time = new Date().getTime();
this.toString = function (msg) {
return text;
}
this.toJSON = function() {
return {
text: text,
type: type,
user: user,
recipient: recipient,
time: time
};
}
}
module.exports.PrivateMessage.prototype = new module.exports.Message ();
module.exports.Command = function (name, params) {
if (!params instanceof Array) {
params = new Array(params);
}
var name = name;
var params = params;
var type = 'command';
var time = new Date().getTime();
this.toString = function (msg) {
return name;
}
this.toJSON = function() {
return {
name: name,
params: params,
type: type,
time: time
};
}
}
module.exports.Command.prototype = new module.exports.Message ();
var Message = require('./Message.js').Message;
var PrivateMessage = require('./Message.js').PrivateMessage;
var Command = require('./Message.js').Command;
module.exports.User = function (name, session) {
this.name = name;
this.session = session;
var messageQueue = new Array();
this.loggedin = false;
if (!module.exports.User.id) {
module.exports.User.id = 1;
}
this.id = module.exports.User.id++;
this.addMessage = function (msg) {
if (!this.loggedin) {
return;
}
messageQueue.push(msg);
}
this.toString = function () {
return this.name;
}
this.normalized = function () {
return this.name.replace(/[^a-zA-Z0-9]+/g,'_').toLowerCase();
}
this.toJSON = function () {
return {
name: this.name,
id: this.id
};
}
this.readMessages = function() {
return messageQueue;
}
this.messages = function() {
if (!this.loggedin) {
return new Array(new Command('do_login'));
}
if (!messageQueue.length) {
return messageQueue;
}
var tempQueue = messageQueue;
messageQueue = new Array();
return tempQueue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment