Skip to content

Instantly share code, notes, and snippets.

@danneu
Created September 11, 2014 23:58
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 danneu/456f4f3fa47063d17ed0 to your computer and use it in GitHub Desktop.
Save danneu/456f4f3fa47063d17ed0 to your computer and use it in GitHub Desktop.
// mkdir mud
// cd mud
// npm install --save sprintf
var net = require('net');
var sprintf = require('sprintf').sprintf;
var vsprintf = require('sprintf').vsprintf;
// A convenient combination of console.log + sprintf formatting
// Example: log('[Client %s] Hello', 42); -> '[Client 42] Hello'
function log(msg) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length === 0) {
console.log(msg);
} else {
var str = vsprintf(msg, args);
console.log(str);
}
}
// Generates a unique client id to represent each connected client (socket conn)
var genClientId = (function() {
var _id = 0;
return function() {
return _id += 1;
}
})();
var server = net.createServer(function() {
console.log('Server connected');
});
server.listen(3000, function() {
console.log('Server listening on 3000');
});
server.on('connection', function(csock) {
var clientId = genClientId();
log('[Client %s] Connected', clientId);
handleClientSocket(csock, clientId);
csock.write('> ');
});
function handleClientSocket(csock, clientId) {
csock.on('data', function(buf) {
var line = buf.toString();
log('[Client %s] Sent line: %s', clientId, line);
handleCommand(csock, line);
csock.write('> ');
});
csock.on('end', function() {
log('[Client %s] Disconnected', clientId);
});
}
/* //// Handle client commands
Dispatch on the first word a client sends us
*/
// If `line` is "say What is your name?"
// Then `firstWord` will be "say"
// And `restOfLine` will be "What is your name?"
// If firstWord is in our `commandFns` lookup map, then dispatch command
function handleCommand(csock, line) {
var firstWord = line.split(/[ ]+/)[0].trim();
var restOfLine = (function() {
var match = line.match(/\W(.+)/);
if (match) {
return match[1];
} else {
return null;
}
})();
var commandFn = commandFns[firstWord];
if (commandFn) {
commandFn(csock, restOfLine);
} else {
csock.write('What?\r\n');
}
}
function goNorth(csock, line) {
csock.write('You went north\r\n');
}
function goSouth(csock, line) {
csock.write('You went south\r\n');
}
function goEast(csock, line) {
csock.write('You went east\r\n');
}
function goWest(csock, line) {
csock.write('You went west\r\n');
}
function say(csock, line) {
csock.write(sprintf('You say "%s"\r\n', line));
}
var commandFns = {
/* Movement */
n: goNorth,
north: goNorth,
s: goSouth,
south: goSouth,
e: goEast,
east: goEast,
w: goWest,
west: goWest,
/* Communication */
say: say
};
@danneu
Copy link
Author

danneu commented Sep 12, 2014

In one terminal (server):

danneu ~/Code/Node/mud
$ nodemon server.js
Server listening on 3000
Server connected
[Client 1] Connected
[Client 1] Sent line: n
[Client 1] Sent line: s
[Client 1] Sent line: e
[Client 1] Sent line: w
[Client 1] Sent line: say Hello world!
[Client 1] Disconnected
$

In another terminal (client):

danneu ~/Code/Node/mud
$ telnet localhost 3000
Trying ::1...
Connected to localhost.
Escape character is '^]'.
> n
You went north
> s
You went south
> e
You went east
> w
You went west
> say Hello world!
You say "Hello world!"
> ^]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment