Skip to content

Instantly share code, notes, and snippets.

@Cacodaimon
Created October 15, 2012 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cacodaimon/3894125 to your computer and use it in GitHub Desktop.
Save Cacodaimon/3894125 to your computer and use it in GitHub Desktop.
nodejs simple chat server used@cacodaemon.de
var net = require('net');
function Clients () {
var clients = new Array()
var clientCounter = 0
var Client = function (socket, name) {
this.send = function(msg) {
this.socket.write(msg + '\n')
}
this.toString = function() {
return this.name
}
this.socket = socket
this.name = name
this.send('Hello, use /help to display available commands.')
}
var getClientBySocket = function(socket) {
for (var i = clients.length - 1; i >= 0; i--) {
if (clients[i].socket == socket) {
return clients[i]
}
}
return null
}
var getClientByname = function(name) {
for (var i = clients.length - 1; i >= 0; i--) {
if (clients[i].name == name) {
return clients[i]
}
}
return null
}
this.addClient = function (socket) {
var client = new Client(socket, 'User_' + ++clientCounter)
this.sendToAll('New user logged in.')
clients.push(client)
return client
}
this.sendToAll = function (msg) {
for (var i = clients.length - 1; i >= 0; i--) {
if (!clients[i]) {
continue
}
clients[i].send(msg)
}
}
this.sendToAllFromClient = function (msg, socket) {
var client = getClientBySocket(socket)
for (var i = clients.length - 1; i >= 0; i--) {
if (!clients[i]) {
continue
}
if (clients[i].socket == client.socket) {
continue
}
clients[i].send(client + ': ' + msg)
}
}
this.removeClient = function (socket) {
for (var i = clients.length - 1; i >= 0; i--) {
if (clients[i].socket != socket) {
continue
}
clients.splice(i)
return
}
}
this.handleCommand = function (command, socket) {
var client = getClientBySocket(socket)
if (command.substring(0, 1) != '/') { //check is command
return false
}
command = command.split(' ')
var type = command[0]
var value = command.length > 1 ? command[1] : null
switch (type) {
case '/name':
this.sendToAll(client.name + ' changed his name to ' + value)
client.name = value
break
case '/kick':
var cmdClient = getClientByname(value)
if (cmdClient == null) {
client.send('User not found!')
return
}
this.removeClient(cmdClient.socket)
cmdClient.socket.end()
break
case '/quit':
this.removeClient(socket)
socket.end()
break
case '/private':
var cmdClient = getClientByname(value)
if (cmdClient == null) {
client.send('User not found!')
return
}
var message = command.splice(2, command.length).join(' ')
cmdClient.send(client + ' sends you a private message: ' + message)
break
case '/users':
var users = 'Users online: ' + clients.join(', ')
client.send(users)
break
default:
client.send('Unkown command: ' + type)
case '/help':
client.send('Available commands are:' +
'/name <YouNewName>, ' +
'/kick <UserName>, ' +
'/private <UserName> <your private message>, ' +
'/quit, ' +
'/users, ' +
'/help')
break
}
return true
}
}
var clients = new Clients();
net.createServer(function (socket) {
clients.addClient(socket)
socket.on('data', function(msgBuffer) {
var msg = msgBuffer.toString()
msg = msg.substring(0, msg.length - 1) //remove newline
console.log(msg)
if (clients.handleCommand(msg, socket)) {
return
}
clients.sendToAllFromClient(msg, socket)
});
socket.on('end', function() {
clients.removeClient(socket)
});
}).listen(1337);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment