选择 JSON over TCP 的传输方式

编程入门 行业动态 更新时间:2024-10-07 18:25:47
本文介绍了选择 JSON over TCP 的传输方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在编写一个简单的流式 JSON 服务.它由 JSON 消息组成,间歇性地发送很长一段时间(数周或数月).

I'm writing a simple streaming JSON service. It consists of JSON messages, sent intermittently, for a long period of time (weeks or months).

通过普通 TCP 套接字发送多个 JSON 消息的最佳实践是什么?

What is the best practise with regard to sending multiple JSON messages over a plain TCP socket?

我看过的一些替代方案(以及它们的缺点)是:

Some alternatives I have looked at (and their downsides) are:

换行符分隔 JSON - 缺点:JSON 中的换行符需要转义或禁止websocket 启发了 0x00 0xff 框架 - 缺点:它现在是二进制的,不再是 utf-8真正的 websockets - 缺点:缺乏(开源)websocket client 库 http multipart http://www.w3/Protocols/rfc1341/7_2_Multipart.html - 缺点:客户端支持不完整?没有分隔符 - 缺点:分块需要 JSON 解析(不能因为字符串中的卷曲而只计算卷曲) newline separated JSON - downside: newlines within JSON require escaping, or prohibition websocket inspired 0x00 0xff framing - downside: it's now binary, not utf-8 anymore real websockets - downside: lack of (opensource) websocket client libraries http multipart http://www.w3/Protocols/rfc1341/7_2_Multipart.html - downside: incomplete client support? no delimiters - downside: chunking requires JSON parsing (can't just count curlies because of curlies in strings)

有没有好的,或者至少是行之有效的方法?

Is there a good, or at least well-established way of doing this?

推荐答案

我已经整理了我和其他一些开发人员正在做的事情:

I've codified what I and some other developers are doing:

http://en.wikipedia/wiki/Line_Delimited_JSON

它的优点是与 netcat/telnet 兼容.

It has the advantage of being netcat/telnet compatible.

另见:http://ndjson/

这篇关于选择 JSON over TCP 的传输方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 23:01:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/815318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   JSON   TCP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!