Javascript 发布套接字类型对象tcp(全局)时出错
首先我想为我的英语道歉,我希望你能理解我 我的问题是,我不知道如何发布套接字类型的对象,以便能够从接口(例如ajax或通过事件等)使用Javascript 发布套接字类型对象tcp(全局)时出错,javascript,sockets,events,tcp,Javascript,Sockets,Events,Tcp,首先我想为我的英语道歉,我希望你能理解我 我的问题是,我不知道如何发布套接字类型的对象,以便能够从接口(例如ajax或通过事件等)使用 'use strict'; const net = require('net'); const PORT = 5000; const HOST = 'localhost'; class Server { //server = null; //socket = null; constructor(port, address) {
'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;
我等待你的答复,谢谢