Created
January 27, 2012 09:24
-
-
Save zeuxisoo/1687942 to your computer and use it in GitHub Desktop.
node.js uncompleted chat socket test
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
var net = require('net'); | |
var client = net.connect(5891, function() { | |
console.log('Connected to server'); | |
}); | |
client.on('data', function(data) { | |
var command = JSON.parse(new Buffer(data).toString()); | |
switch(command.name) { | |
case 'SHOW OPTIONS': | |
console.log('Please select options:'); | |
console.log('1. Say'); | |
console.log('2. List messages'); | |
console.log('3. List users'); | |
console.log('4. Logout'); | |
ask("Your options: ", /[1-4]/, function(options) { | |
client.write('OPTIONS ' + options); | |
}); | |
break; | |
case 'LIST USERS': | |
var user_list = command.value; | |
for(var i in user_list) { | |
console.log('- %s', user_list[i].ip); | |
} | |
break; | |
case 'LOGOUT': | |
console.log('Bye!'); | |
process.exit(1); | |
break; | |
} | |
}); | |
function ask(question, format, callback) { | |
var stdin = process.stdin, stdout = process.stdout; | |
stdin.resume(); | |
stdout.write(question); | |
stdin.once('data', function(data) { | |
data = data.toString().trim(); | |
if (format.test(data)) { | |
callback(data); | |
} else { | |
stdout.write("It should match: "+ format +"\n"); | |
ask(question, format, callback); | |
} | |
}); | |
} |
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
var net = require('net'); | |
var users = []; | |
var server = net.createServer({ allowHalfOpen: true }, function(socket) { | |
socket.setEncoding("utf8"); | |
socket.on('connect', function() { | |
console.log('[Connected] ' + socket.remoteAddress); | |
socket.write(command('SHOW OPTIONS'), function() { | |
users[socket] = socket.remoteAddress; | |
}); | |
}); | |
socket.on('data', function(data) { | |
var data_parts = data.split(' '); | |
var command = data_parts[0]; | |
var value = data_parts[1]; | |
switch(command) { | |
case 'OPTIONS': | |
if (value < 1 && value > 4) { | |
socket.write(command('SHOW OPTIONS')); | |
}else{ | |
runOptions(socket, value); | |
} | |
break; | |
} | |
}); | |
socket.on('end', function() { | |
users[socket] = null; | |
console.log('[Disconnect] ' + socket.remoteAddress); | |
}); | |
}).listen(5891, function() { | |
console.log('Server started at ' + server.address().address + ':' + server.address().port); | |
}); | |
function command(name, value) { | |
return JSON.stringify({ | |
name: name, | |
value: value | |
}); | |
} | |
function runOptions(socket, options) { | |
if (options == 3) { | |
var user_list = []; | |
for(var i in users) { | |
user_list.push({ | |
ip: users[i], | |
owner: i == socket | |
}); | |
} | |
console.log(user_list); | |
socket.write(command('LIST USERS', user_list), function() { | |
socket.write(command('SHOW OPTIONS')); | |
}); | |
}else if (options == 4) { | |
socket.write(command('LOGOUT')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment