Skip to content

Instantly share code, notes, and snippets.

@zhfnjust
Created May 15, 2019 14:53
Show Gist options
  • Save zhfnjust/4abd642f9d7eb3b6b4ffab3a50d70192 to your computer and use it in GitHub Desktop.
Save zhfnjust/4abd642f9d7eb3b6b4ffab3a50d70192 to your computer and use it in GitHub Desktop.
ping
/**
* 构建TCP客户端
*/
const fs = require('fs');
/* 引入net模块 */
var net = require("net");
const uuidv1 = require('uuid/v1');
async function main(){
for(let i = 0; i < 1; i++) {
rq();
}
}
main();
function rq(){
return new Promise(function(resolve, reject) {
let start = new Date().getTime();
/* 创建TCP客户端 */
var client = net.Socket();
/* 设置连接的服务器 */
client.connect(8000, 'xxx', function () {
console.log("connect the server");
client.write('aa');
})
/* 监听服务器传来的data数据 */
client.on("data", function (data) {
let end = new Date().getTime();
console.log("the data of server is " + data.toString(), (end - start));
})
/* 监听end事件 */
client.on("end", function () {
console.log("data end");
resolve();
})
client.on("close", function () {
console.log("data close");
resolve();
})
client.on("error", function (e) {
console.log("data error", e);
reject();
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment