Created
November 20, 2012 15:46
-
-
Save flada-auxv/4118710 to your computer and use it in GitHub Desktop.
node.js入門勉強会第1回目で作った簡単なTCPサーバ
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 readline = require('readline'); | |
var util = require('./util'); | |
var server = net.createServer(); | |
server.maxConnections = 3; | |
function Client(socket) { | |
this.socket = socket; | |
} | |
Client.prototype.writeData = function(d) { | |
var socket = this.socket; | |
if (socket.writable) { | |
var key = util.getKey(socket); | |
process.stdout.write('[' + key + '] - ' + d); | |
socket.write('[R' + util.getCount() + '] ' + d); | |
} | |
}; | |
var clients = {}; | |
server.on('connection', function(socket) { | |
var status = util.getConnectionStatus(server); | |
var key = util.getKey(socket); | |
console.log('Connection Start(' + status + ') - ' + key); | |
clients[key] = new Client(socket); | |
}); | |
server.on('connection', function(socket) { | |
var data = ''; | |
var newline = /\r\n|\n/; | |
socket.on('data', function(chunk) { | |
data += chunk | |
var key = util.getKey(socket); | |
if (newline.test(data)) { | |
clients[key].writeData(data); | |
data = ''; | |
} | |
}); | |
}); | |
server.on('connection', function(socket) { | |
var key = util.getKey(socket); | |
socket.on('end', function() { | |
var status = util.getConnectionStatus(server); | |
console.log('Connection End(' + status + ') - ' + key); | |
delete clients[key]; | |
}); | |
}); | |
server.on('close', function() { | |
console.log('Server Closed'); | |
}); | |
server.listen(11111, '127.0.0.1', function() { | |
var addr = server.address(); | |
console.log('Listening Start on Server - ' + addr.address + ':' + addr.port); | |
}); | |
var rl = readline.createInterface(process.stdin, process.stdout); | |
rl.on('SIGINT', function() { | |
server.close(); | |
for(var i in clients) { | |
var socket = clients[i].socket; | |
socket.end(); | |
} | |
/* | |
* ここでserver.close()だと問題ある。 | |
* socketの数が多かったりしてfor文の箇所で実行に時間が掛かると、 | |
* 新しく接続を受け付けてしまうかもしれない。 | |
* なので、さきにserver.close()として、新規接続を受け付けない状態にしてからsocket.end() | |
* socketが全部死ぬと"close"イベントが発生する。 | |
*/ | |
// server.close(); | |
rl.close(); | |
}); | |
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
// このcountは、外部公開されない。クロージャ!! | |
var count = 1; | |
module.exports = { | |
getKey: function(socket) { | |
return socket.remoteAddress + ':' + socket.remotePort; | |
}, | |
getConnectionStatus: function(server) { | |
return server.connections + '/' + server.maxConnections; | |
}, | |
getCount: function() { | |
return count++; | |
}, | |
resetCount: function() { | |
count = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment