如何使用 Node.js 创建自定义对象

发布于 2024-11-29 02:01:57 字数 649 浏览 0 评论 0原文

我有一个基于 java 的服务器,允许客户端应用程序通过其他编程语言(java、unity、obj-c)进行连接。我想知道如何使用node.js 和socket.io 将javascript 添加到此列表。服务器侦听设定的端口并接受简单的 json 数据以及以字节为单位的长度 int,它以相同的格式响应。 “数据包”的格式如下:

first four bytes are the length
00 00 00 1c

remaining bytes are the data
7b 22 69 64 22 3a 31 2c 22 6e 61 6d 65 22 3a 22 73 6f 6d 65 77 69 64 67 65 74 22 7d

数据通过 TCP 发送并以小端编码。源自 Java 的对象建模如下:

public class Widget {
    private int id;
    private String name;
    public int getId() { return id; }
    public String getName() { return name; }
}

JSON 为:

{"id":1,"name":"somewidget"}

I have a java-based server that allows client applications to connect via other programming languages (java, unity, obj-c). I would like to know how to add javascript to this list using node.js and socket.io. The server listens on a set port and accepts simple json data plus an int for length in bytes, it response in the same format. The format of the "packet" is like so:

first four bytes are the length
00 00 00 1c

remaining bytes are the data
7b 22 69 64 22 3a 31 2c 22 6e 61 6d 65 22 3a 22 73 6f 6d 65 77 69 64 67 65 74 22 7d

The data is sent over TCP and is encoded in little endian. The object in originating from Java is modeled like so:

public class Widget {
    private int id;
    private String name;
    public int getId() { return id; }
    public String getName() { return name; }
}

The JSON would be:

{"id":1,"name":"somewidget"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆で 2024-12-06 02:01:57

您将需要一个 TCP 套接字。将其连接到服务并侦听数据事件。当数据事件被触发时,查看缓冲区长度。如果它 <= 4 字节,您可能应该丢弃它*。否则使用 readUInt32() 读取前 4 个字节,并将“little”指定为字节序。然后使用该数字作为剩余缓冲区的长度。如果缓冲区短于给定长度,则仅“读取”剩余长度,否则“读取”给定长度。为此,请使用 toString 方法。您将获得一个可以使用 JSON.parse 方法进行解析的字符串,该方法将返回与 JSON 匹配的 JavaScript 对象。

您可以通过实例化缓冲区并将所有数据写入其中来以基本相同的方式构建数据包。

另请参阅 缓冲区net 文档了解详细信息。

* 我不知道节点何时触发其数据事件,但您的数据可能会收到碎片(分成多个数据事件)。这种情况可能发生,并且由于 tcp 协议的流式传输性质,如果 JSON 字符串足够长而无法放入单个帧,则很可能会发生这种情况。在这种情况下,您可能不应该简单地丢弃缓冲区,而应该尝试重新组装碎片数据。

You will need a TCP socket. Connect it to the service and listen for the data event. When the data event is fired look at the buffers length. If it is <= 4 byte, you propably should discard it*. Else read the first 4 bytes using readUInt32() specifying 'little' as the endianess. Then use this number for the length of the remainding buffer. If the buffer is shorter than the given length, only "read" the remaining length, else "read" the given length. Use the toString method for this. You will get a string that can be parsed using the JSON.parse method, which will return you the JavaScript object matching the JSON.

You can build your packets basicly the same way by instanciating a buffer and writing all the data to it.

Also see the Buffers and net documentation for details.

* I do not know when node fires it's data events but your data might be received fragmented (that is splitted up into multiple data events). It could happen and due to the streaming nature of the tcp protocol it most likely will happen if the JSON string is long enough to not fit into a single frame. In that case, you propably should not simply discard the buffer, but try to reassemble the fragmented data.

无所的.畏惧 2024-12-06 02:01:57

因此,如果您只想向服务器发送请求并获得响应,您可以这样做:

var net = require('net');

var soc = net.Socket();
soc.connect(8088);

soc.on('connect', function(){
  var data, request, header;

  data = {request : true};
  data = JSON.stringify(data);

  request = new Buffer(Buffer.byteLength(data));
  request.write(data);

  header = new Buffer(4);
  header.write(request.length.toString());


  // send request  
  soc.end(header.toString('binary') + request.toString('binary'));
});

soc.on('data', function(buffer){
  // Crop length bytes
  var data = JSON.parse(buffer.slice(4).toString('utf-8'));
  soc.destroy();

  console.log(data);
});

So if you want just send request to server and get response you could do this:

var net = require('net');

var soc = net.Socket();
soc.connect(8088);

soc.on('connect', function(){
  var data, request, header;

  data = {request : true};
  data = JSON.stringify(data);

  request = new Buffer(Buffer.byteLength(data));
  request.write(data);

  header = new Buffer(4);
  header.write(request.length.toString());


  // send request  
  soc.end(header.toString('binary') + request.toString('binary'));
});

soc.on('data', function(buffer){
  // Crop length bytes
  var data = JSON.parse(buffer.slice(4).toString('utf-8'));
  soc.destroy();

  console.log(data);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文