问题描述
限时送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:关键词]
发布评论