Skip to content

Instantly share code, notes, and snippets.

@davethesoftwaredev
Last active December 17, 2015 03:48
Show Gist options
  • Save davethesoftwaredev/5545679 to your computer and use it in GitHub Desktop.
Save davethesoftwaredev/5545679 to your computer and use it in GitHub Desktop.
A simple chat server written in node.js. Implements usernames, some commands. Port 9001.
// Simple Node.Js chat server
// My first attempt at a node application. Supports usernames, and a few text-based commands.
// Dave The Software Dev - davethesoftwaredev@gmail.com
// Hope this helps you
var net = require('net');
var connection = function(id, connection, server) {
this.id = id;
this.conn = connection;
this.server = server;
this.username = '';
var _this = this;
this.conn.on('error', function(e) {
_this.server.close(_this.id);
});
this.conn.on('close', function(e) {
_this.server.close(_this.id);
});
this.conn.on('data', function(data) {
var text = data.toString();
if(_this.username == '' && text.match(/^\/username/)) {
var user = text.substring(9).trim();
if(_this.username != '') {
_this.send('You may not change your username.\n');
}
else if(user != '<Anonymous>' && _this.server.doesUserExist(user) == false) {
_this.username = text.substring(10).trim();
_this.server.broadcastAll(_this.username + ' has joined the chat.\n');
}
else {
_this.send('That username is not available.\n');
}
}
else if(text.match(/^\/exit/)) {
_this.server.close(_this.id);
}
else if(text.match(/^\/users/)) {
var users = _this.server.getUserList();
if(users.length == 0) {
_this.send('No users exist in chat.\n');
}
else {
for(var i = 0; i < users.length; i++) {
_this.send(users[i] + '\n');
}
}
}
else if(_this.username == '') {
_this.send('Please choose a username by sending /username <name>\n');
}
else {
_this.server.broadcastAll(_this.username + ': ' + text.trim() + '\n');
}
});
this.close = function() {
_this.conn.destroy();
}
this.send = function(data) {
_this.conn.write(data);
}
console.log('New connection from ' + connection.remoteAddress);
this.send("Welcome to Dave's simple chat server.\n");
this.send("Commands:\n");
this.send(" /username <user> - Sets your username.\n");
this.send(" /users - Returns a list of users in the chat.\n");
this.send(" /exit - Leaves the chat.\n");
this.send("Please set your username prior to sending text.\n");
this.send("-----------------------------------------------------------\n");
}
var server = function() {
var _this = this;
this.broadcastAll = function(data) {
for(var i = 0; i < this.connections.length; i++) {
_this.connections[i].send(data);
}
}
this.broadcastAllExcept = function(id, data) {
for(var i = 0; i < _this.connections.length; i++) {
if(_this.connections[i].id != id)
_this.connections[i].send(data);
}
}
this.handleConnection = function(conn) {
var conn = new connection(_this.currentConnectionId++, conn, _this);
_this.connections.push(conn);
}
this.start = function(port) {
_this.server.listen(port);
}
this.close = function(id) {
for(var i = 0; i < _this.connections.length; i++) {
if(_this.connections[i].id == id) {
if(_this.connections[i].username == '') {
_this.broadcastAllExcept(id, '<Anonymous> has left the chat.\n');
}
else {
_this.broadcastAllExcept(id, _this.connections[i].username + ' has left the chat.\n');
}
_this.connections[i].close();
_this.connections.splice(i, 1);
break;
}
}
}
this.getUserList = function() {
var users = [];
for(var i = 0; i < _this.connections.length; i++) {
if(_this.connections[i].username == '') {
users.push('<Anonymous>');
}
else {
users.push(_this.connections[i].username);
}
}
return users;
}
this.doesUserExist = function(username) {
for(var i = 0; i < _this.connections.length; i++) {
if(_this.connections[i].username == username) {
return true;
}
}
return false;
}
this.server = net.createServer(this.handleConnection);
this.currentConnectionId = 0;
this.connections = [];
}
var mainServer = new server();
mainServer.start(9001);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment