在Socket.io Java客户端中是否可以发送字节数组到JSON?

huangapple 未分类评论43阅读模式
英文:

Is it possible to send a byte array in a json over socket.io java client

问题

这实际上是两个问题,但它们密切相关。

我已经多次阅读到,必须通过类似于socket.io的websocket发送json/字符串或二进制数据,但不能混合使用这些类型。但是我在socket.io客户端java实现的官方文档中找到了以下示例,让我感到困惑:

// 发送一个对象
JSONObject obj = new JSONObject();
obj.put("hello", "server");
obj.put("binary", new byte[42]);
socket.emit("foo", obj);

// 接收一个对象
socket.on("foo", new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONObject obj = (JSONObject)args[0];
}
});

其中json的"binary"元素明显是二进制,与名称一致。文档提到socket.io使用org.json,但我在任何地方都找不到这个库支持向json文件添加二进制数据。

这个功能现在是否被支持?如果是,socket.io在后台做了什么?它是将emit分成两个单独的消息然后重新合并吗?还是简单地将二进制数据保存在base64中?

一些背景。

我正在尝试为我的应用程序添加私人聊天功能,以便用户可以与其他多个用户进行多个私人的两方音频消息聊天对话。我在弄清楚如何告诉服务器如何转发所有消息方面遇到了问题。如果我使用json,我可以简单地在json中添加发送者和接收者,然后让服务器读取接收者的ID并相应地转发消息。但是我不确定如何处理只包含二进制数据的消息。我不知道如何为它们添加元数据(例如发送者和接收者ID),以便服务器知道它们是发给谁的。我听说过建议发送带有发送者ID、接收者ID和我正在尝试发送的文件的MD5哈希的json,然后单独发送二进制数据,并让服务器通过md5签名匹配这两条消息,但似乎会带来自己的问题。比如我不知道在服务器上计算大量音频文件的MD5会如何影响服务器性能,还有可能在到达指定目的地的json之前就收到音频字节数组的问题。

总是可以将音频文件编码为base64并将其作为json发送,就像我一直在做的那样,但有人告诉我这是一种不好的做法,应该避免,因为它会增加包大小。

我觉得已经有很多消息传递应用程序了,我敢打赌其中至少有一些是基于websockets的。我想知道在websocket上如何路由二进制数据到特定的接收连接是否有最佳实践。

对于上述问题的任何答案以及如何解决背景部分提到的问题的任何提示,我都会点赞。

英文:

this is actually two questions in one but they are closely related

I have read plenty of times that you have to send either json/string or binary data over a websocket like socket.io but that you cannot mix these types. But then I was puzzled by finding the following example in the official documentation of the socket.io client java implementation

// Sending an object
JSONObject obj = new JSONObject();
obj.put("hello", "server");
obj.put("binary", new byte[42]);
socket.emit("foo", obj);

// Receiving an object
socket.on("foo", new Emitter.Listener() {
  @Override
  public void call(Object... args) {
    JSONObject obj = (JSONObject)args[0];
  }
});

where the "binary" element of that json is clearly binary as the name suggests. The documentation talks about socket.io using org.json, but i couldnt find that this library supports adding binary data to json files anywhere.

is this functionality now supported? if so, what is socket.io doing in the background? is it splitting the emit in two separate messages and then remerging it? or is it simply saving the binary data in base64?

A bit of background.

I am trying to add a private chat functionality to my app so that a user can have multiple private two-party audio-message based chat conversations with several other users. i am having issues finding out how to tell my server how to forward all the messages. if i use a json i can simply add a sender and a receiver to the json and have the server read the id of the receiver and forward the message accordingly. but i am not sure how to handle messages containing only binary data. i have no idea how to add metadata (such a sender and a receiver id) to them so that the server knows to whom they are addressed. i have heard the suggestion of sending a json with a sender id, a receiver id and an MD5 hash of the file i am trying to send, and then send the binary data alone separately and having the server match the two messages over the md5 signature, but that seems to come with problems of its own. like i dont know how having to calculate the MD5 of a ton of audio files on the server is going to affect server performance and there is also the issue of potentially receiving the audio byte array before the json specifying its destination has arrived.

there is always the alternative of encoding my audio files in base64 and sending them as json, as i have been doing so far, but i have been told this is a bad practice and should be avoided as if inflates package sizes.

i feel like there are a bunch of messaging apps already out there, and i bet at least some are based on websockets. I would like to know if there are any best practices on how to route binary data over a websocket to a specific receiving connection.

Ill upvote any answer to the questions above as well as any hint on how to tackle the problem mentioned in the background part.

huangapple
  • 本文由 发表于 2020年4月5日 11:05:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/61037512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定