react-native-ali-smartliving
Version:
Component implementation for smartliving WiFi SDK of Ali fy platform
28 lines (27 loc) • 778 B
JavaScript
var net = require('net');
var port = process.env.PORT || 8821;
var host = process.env.HOST || '127.0.0.1';
var client = new net.Socket();
// 创建socket客户端
client.setEncoding('binary');
// 连接到服务端
client.connect(port, host, function () {
console.log(
'connected server ' + host + ':' + port,
);
// 向端口写入数据到达服务端
client.write(JSON.stringify({hello: 'server'}));
});
client.on('data', function (data) {
// 得到服务端返回来的数据
console.log('from server ' + data);
});
client.on('error', function (exception) {
// 错误出现之后关闭连接
console.log('socket error ' + exception);
client.destory();
});
client.on('close', function () {
// 正常关闭连接
console.log('Connection closed');
});