javascript - 发布套接字类型对象 tcp(全局)时出错
问题描述
首先我要为我的英语道歉,希望你能理解我。
我的问题是我看不到发布套接字类型对象以便能够从接口使用的方法,例如 ajax 或通过事件等。
'use strict';
const net = require('net');
const PORT = 5000;
const HOST = 'localhost';
class Server {
//server = null;
//socket = null;
constructor(port, address) {
this.port = port || PORT;
this.address = address || HOST;
this.server = net.createServer();
this.socket = null;
this.init();
}
init() {
this.server.listen(this.port, this.address);
this.server.on('data', (data) => {
console.log(data);
this.socket.write(data);
});
this.server.on('close', () => {
console.log('Cerrado');
});
this.server.on('error', (err) => {
console.log('Error');
});
this.server.on('connection', function(sock) {
console.log('Nuevo cliente conectado');
this.socket = sock;
//console.log(this.socket);
});
}
prueba (accion) {
//console.log(this.socket); // null;
this.socket.write(accion);
}
}
module.exports = Server;
期待你的回复,谢谢