react-native-ali-smartliving
Version:
Component implementation for smartliving WiFi SDK of Ali fy platform
37 lines (36 loc) • 1.07 kB
JavaScript
var net = require('net');
var listenPort = process.env.PORT || 8821; // 监听端口
var server = net
.createServer(function (socket) {
// 创建socket服务端
console.log(
'client connected ' + socket.remoteAddress + ':' + socket.remotePort,
);
socket.setEncoding('binary');
// 接收到数据
socket.on('data', function (data) {
console.log('client sent ' + data);
});
socket.write(JSON.stringify({hello: 'client'}));
// socket.pipe(socket);
// 数据错误事件
socket.on('error', function (exception) {
console.log('socket error ' + exception);
socket.end();
});
// 客户端关闭事件
socket.on('close', function (data) {
console.log(
'client closed ' + socket.remoteAddress + ':' + socket.remotePort,
);
});
})
.listen(listenPort);
// 服务器监听事件
server.on('listening', function () {
console.log(server.address());
});
// 服务器错误事件
server.on('error', function (exception) {
console.log('server error ' + exception);
});