之前实现是客户端代码,master.js通过建立好的socket进行write操作, master.js代码里面有一个完成 start(requestId) { if (!this.frame || !this.transport.update(this.frame, requestId)) { this.frame = this.transport.encode(requestId, this.transaction.unit, this.transaction.request.toBuffer()); }
this.requestId = requestId;
this.timeoutTimer = setTimeout(this.onTimeout.bind(this), this.transaction.timeout);
//console.log('this.connection.socket.remoteAddress ',this.connection.socket);
this.connection.write(this.frame); //这里完成写数据 //this.connection.emit(‘write’,this.frame); // this.connection.onClientWrite(this.connection.socket,this.frame); this.transaction.emit(‘request’, requestId); } 客户端有个tcpconnection.js write(data) { this.emit(‘write’, data);
if (!this.isOpen())
{
return;
}
try
{
this.socket.write(data);
}
catch (err)
{
this.emit('error', err);
}
}
现在想在服务器端也用master.js进行写数据,服务器端tcplistener有段代码,
setUpClient(socket) {
console.log('setUpClient sep: %s', socket.remoteAddress);
console.log('setUpClient:sep %d', socket.remotePort);
const client = new RemoteClient({
address: socket.remoteAddress,
onClientWriteport: socket.remotePort,
family: socket.remoteFamily
});
client.on('close', this.onClientClose.bind(this, socket));
client.on('write', this.onClientWrite.bind(this, socket));
client.write(writedata.toString()); const socketListeners = { close: client.destroy.bind(client), error: client.emit.bind(client, ‘error’), // writeable:function() // { // client.emit(‘write’, data); // }, readable: function() { const data = socket.read();
if (data !== null)
{
client.emit('data', data);
console.log('client.emit(writedata)');
}
}
};
socket.on('close', socketListeners.close);
socket.on('error', socketListeners.error);
socket.on('readable', socketListeners.readable);
socket.setNoDelay(this.serverOptions.noDelay !== false);
this.socketListeners.set(socket, socketListeners); this.clients.set(socket, client);
//this.onClientWrite(socket, writedata.toString());
//client.emit('write',writedata.toString());
this.emit('client', client);
return socket;
}
/**
/**
if (!client) { console.log(‘client null’); return; }
try
{
socket.write(data);
}
catch (err)
{
client.emit('error', err);
}
} 我想调用这个里面的onClientWrite,把master.js里面的代码改成 start(requestId) { if (!this.frame || !this.transport.update(this.frame, requestId)) { this.frame = this.transport.encode(requestId, this.transaction.unit, this.transaction.request.toBuffer()); }
this.requestId = requestId;
this.timeoutTimer = setTimeout(this.onTimeout.bind(this), this.transaction.timeout);
//console.log('this.connection.socket.remoteAddress ',this.connection.socket);
// this.connection.write(this.frame); //之前的客户端代码 this.connection.onClientWrite(this.connection.socket,this.frame); //现在修改的代码 // this.connection.onClientWrite(this.connection.socket,this.frame); this.transaction.emit(‘request’, requestId); } 发现获取不到this.connection.socket这个连接,单独把onClientWrite改成这种写法也不行 write(data) {
this.socket.write(data);
}
服务器和客户端相关连接函数我都发出来 onServerConnection(socket) {
this.setUpClient(socket);
} setUpClient(socket) {
console.log('setUpClient sep: %s', socket.remoteAddress);
console.log('setUpClient:sep %d', socket.remotePort);
const client = new RemoteClient({
address: socket.remoteAddress,
onClientWriteport: socket.remotePort,
family: socket.remoteFamily
});
client.on('close', this.onClientClose.bind(this, socket));
client.on('write', this.onClientWrite.bind(this, socket));
client.write(writedata.toString()); const socketListeners = { close: client.destroy.bind(client), error: client.emit.bind(client, ‘error’), // writeable:function() // { // client.emit(‘write’, data); // }, readable: function() { const data = socket.read();
if (data !== null)
{
client.emit('data', data);
console.log('client.emit(writedata)');
}
}
};
socket.on('close', socketListeners.close);
socket.on('error', socketListeners.error);
socket.on('readable', socketListeners.readable);
socket.setNoDelay(this.serverOptions.noDelay !== false);
this.socketListeners.set(socket, socketListeners); this.clients.set(socket, client);
//this.onClientWrite(socket, writedata.toString());
//client.emit('write',writedata.toString());
this.emit('client', client);
} 客户端代码 setUpSocket(socket) { if (!socket) { socket = new Socket(); }
socket.on('connect', this.onSocketConnect);
socket.on('close', this.onSocketClose);
socket.on('error', this.onSocketError);
socket.on('readable', this.onSocketReadable);
socket.setNoDelay(this.socketOptions.noDelay !== false);
return socket;
} 本人存小白,想了解一下有什么办法能实现获取到这个服务器js里面的socket呢,他是一个返回值吗