1var tcp = require('tcp'); 2 3function onConnect() { 4 tcpClient.send({ 5 message: 'hello, this is tcp client test', 6 success: function() { 7 console.log('tcp send success'); 8 }, 9 fail: function() { 10 console.log('tcp send failed'); 11 } 12 }); 13} 14var tcpClient = tcp.createClient({ 15 host: '47.101.151.113', 16 port: 50020, 17 success: function() { 18 console.log('tcp client connect success'); 19 }, 20 fail: function() { 21 console.log('tcp client connect failed'); 22 } 23}); 24 25tcpClient.on('message', function(data) { 26 console.log('tcp receive data: ' + data); 27 tcpClient.close(); 28}); 29 30tcpClient.on('connect', function() { 31 console.log('tcp client connected'); 32 onConnect(); 33}); 34 35tcpClient.on('close', function() { 36 console.log('tcp client closed'); 37}); 38 39tcpClient.on('error', function(err) { 40 console.log('tcp client error: ' + err); 41});