想请教一下nodejs的net模块里面,想实现调用服务器JS代码里面的write写动作,但是获取不到soket

yingju3507 2017-06-03 12:17:12
之前实现是客户端代码,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;
}

/**
* @private
*/
destroyClients()
{
this.clients.forEach(client => client.destroy());
}

/**
* @private
* @param {Socket} socket
* @param {Buffer} data
*/
onClientWrite(socket, data)
{
console.log('onClientWrite');
const client = this.clients.get(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呢,他是一个返回值吗
...全文
134 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧