From cd83ef88f783205bc17d06b249fa62f9425957c6 Mon Sep 17 00:00:00 2001 From: 沈斌 <bluelazysb@hotmail.com> Date: Fri, 11 Aug 2017 09:54:33 +0800 Subject: [PATCH] 测试-不要频繁增加连接数 --- server.js | 41 ++++++++++++++++++++++++++++++----------- 1 files changed, 30 insertions(+), 11 deletions(-) diff --git a/server.js b/server.js index ae5b574..3951072 100644 --- a/server.js +++ b/server.js @@ -19,6 +19,8 @@ "5a000001000c": 20 //������������ }; +global.configs = {}; + function handleData(db, socket, value) { //1.��������������� @@ -39,7 +41,6 @@ if(value.startsWith('5a0000010003')) { var output = [ 0x6A, 0x00, 0x00, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6B ]; method.insertData(db, value, function(data, rank) { - //method.insertDocument2(db, value, rank, function(data) {}); var fields = method.padLeft(rank.toString(16), 8).match(/.{2}/g); output[6] = method.toDec(fields[0]); output[7] = method.toDec(fields[1]); @@ -112,12 +113,13 @@ } } -mongoClient.connect(config.URL, function(err, db) { - if (err) { - console.log(err.message); - return; - } - console.log('Connecting to Mongo DB at ' + config.URL); + +function doTCPSocket(db) { + //��������������������������������� + method.initConfigs(db); + + //��������������������������������������� + queue.listenToMQ("ex_data_config"); net.createServer().on('connection', function(socket){ console.log('CONNECTED: ' + socket.remoteAddress +':'+ socket.remotePort); @@ -133,8 +135,6 @@ console.log("Error - invalid data - less than 12 character long"); return; } - - //console.log(moment().format('YYYY-MM-DD HH:mm:ss') + " => " + value); doWork(db, socket, value); }); @@ -156,6 +156,25 @@ console.log('Closed socket: ' + socket.remoteAddress +' '+ socket.remotePort); }); }).listen(config.PORT, config.HOST); - + console.log('TCP Server listening on ' + config.HOST + ':' + config.PORT); -}); +} + +var db; + +if(db == null) { + mongoClient.connect(config.URL, function(err, database) { + if (err) { + console.log(err.message); + return; + } + console.log('Connecting to Mongo DB at ' + config.URL); + + db = database; + + doTCPSocket(db); + }); +} else { + console.log('==========*****=========='); + doTCPSocket(db); +} -- Gitblit v1.8.0