Created
September 11, 2014 23:58
-
-
Save danneu/456f4f3fa47063d17ed0 to your computer and use it in GitHub Desktop.
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
// 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 | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In one terminal (server):
In another terminal (client):