nodejs tcp socket write

时间:2015-10-23 14:01:28

标签: node.js sockets flex

我在将某些数据写入nodejs中的tcp套接字时遇到问题。

这是我要替换的adobe flex代码。

private function SendRequestHello():void
{
    writeUnsignedInt( 0);                                                   /* Request Type. */
    writeUnsignedInt( 16);                                                  /* Length. */
    writeByte( OPCODE_HELLO);                                               /* Opcode. */
    writeUnsignedInt( m_nOwnID / 0x100000000);                              /* GPS Server ID. */
    writeUnsignedInt( m_nOwnID & 0xffffffff);                               /* GPS Server ID part 2. */
    writeUnsignedInt( 3 << 24);                                             /* GPS Server Version (my version). */
    writeUnsignedInt( 3 << 24);                                             /* Oldest supported Server Version. */
    FlushWithCatch();
}

这是我在节点

中的尝试
clients[socket.id].fwd.connect(26000, clients[socket.id].serverip, function() {
    console.info(socket.id + ' connected to gps forwarder: ' + clients[socket.id].serverip + ':26000');
    clients[socket.id].fwdConnected = true;

    clients[socket.id].fwd.write( 0);                                                   /* Request Type. */
    clients[socket.id].fwd.write( 16);                                                  /* Length. */
    clients[socket.id].fwd.write( 1);                                                   /* Opcode. */
    clients[socket.id].fwd.write( clients[socket.id].AppID / 0x100000000);              /* GPS Server ID. */
    clients[socket.id].fwd.write( clients[socket.id].AppID & 0xffffffff);               /* GPS Server ID part 2. */
    clients[socket.id].fwd.write( 3 << 24);                                             /* GPS Server Version (my version). */
    clients[socket.id].fwd.write( 3 << 24);                                             /* Oldest supported Server Version. */
}); 

我得到 TypeError:无效数据

TypeError: invalid data
at Socket.write (net.js:617:11)
at Socket.<anonymous> (C:\**\index.js:84:30)
at Socket.g (events.js:260:16)
at emitNone (events.js:72:20)
at Socket.emit (events.js:166:7)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1045:10)

我应该在这里使用任何不同的写选项吗?

编辑:https://github.com/yi/node-bytearray看起来像一个有趣的插件,但我不明白它的用法。我如何'writeUnsignedInt'进入我的网络套接字?

1 个答案:

答案 0 :(得分:0)

我的解决方案

var buffer = new Buffer(25);
var writer = new BinaryWriter(buffer);
writer.writeUInt32BE(0);            
writer.writeUInt32BE(16);           
writer.writeUInt8(1);                           
writer.writeUInt32BE(client.AppID / 0x100000000);   
writer.writeUInt32BE(client.AppID & 0xffffffff);    
writer.writeUInt32BE(3 << 24);                      
writer.writeUInt32BE(3 << 24);                      

client.fwd.write(buffer);