选择 JSON over TCP 的传输方式 [英] Choice of transports for JSON over TCP

查看:33
本文介绍了选择 JSON over TCP 的传输方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的流式 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:

  1. 换行符分隔 JSON - 缺点:JSON 中的换行符需要转义或禁止
  2. websocket 启发了 0x00 0xff 框架 - 缺点:它现在是二进制的,不再是 utf-8
  3. 真正的 websockets - 缺点:缺乏(开源)websocket client
  4. http multipart http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html - 缺点:客户端支持不完整?
  5. 没有分隔符 - 缺点:分块需要 JSON 解析(不能因为字符串中的卷曲而只计算卷曲)
  1. newline separated JSON - downside: newlines within JSON require escaping, or prohibition
  2. websocket inspired 0x00 0xff framing - downside: it's now binary, not utf-8 anymore
  3. real websockets - downside: lack of (opensource) websocket client libraries
  4. http multipart http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html - downside: incomplete client support?
  5. 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.org/wiki/Line_Delimited_JSON

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

It has the advantage of being netcat/telnet compatible.

另见:http://ndjson.org/

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆