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

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

问题描述

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

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

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