在JSON-RPC连接上读取多个JSON对象 [英] Reading multiple JSON objects on a JSON-RPC connection

查看:254
本文介绍了在JSON-RPC连接上读取多个JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个流API,该API处理RPC样式调用以及从服务器到客户端的通知(而不是从JSON-RPC规范中的客户端到服务器的通知).不幸的是,最后一部分排除了JSON-RPC +持久HTTP.

I am making a streaming API which handles both RPC style calls as well as notifications from the server to the client (not the notifications in the JSON-RPC spec which are client to server). This last part unfortunately rules out JSON-RPC + persistent HTTP.

API基于JSON和JSON-RPC规范.

The API is based on the JSON and JSON-RPC specs.

JSON- http://www.ietf.org/rfc/rfc4627.txt

JSON-RPC- http://www.jsonrpc.org/specification

JSON-RPC - http://www.jsonrpc.org/specification

典型的会话可能是:

-> Sending to server
<- Receiving from server    

-> {'id': 0, 'method':'login','params':{'token':'secret'}}
<- {'id': 0, 'method':'login','result':0}
-> {'id': 1, 'method':'subscribe','params':{'symbol':'VOD.L'}}
<- {'id': 1, 'method':'subscribe','result':0}
...
<- {'method':'notifyPrice','params':{'symbol':'VOD.L', 'bid':10.1, 'ask':10.03}}
<- {'method':'notifyPrice','params':{'symbol':'VOD.L', 'bid':10.2, 'ask':10.03}}

以上消息,尤其是通知,可以以任何顺序出现在同一数据包中.这两个规范似乎都没有包含消息分隔符的详细信息,这使得在不使用基于SAX的JSON解析器的情况下,很难知道何时已接收到整个JSON消息,与DOM对应文件相比,这是非常罕见的.

The above messages, particularly the notifications, could come in any order and in the same packet. Neither of the specs seem to include details of a message separator which makes it difficult to know when an entire JSON message has been received without using a SAX based JSON parser, which are rather rare compared to their DOM counterparts.

我是否遗漏了明显的东西,或者真的没有标准的方法来分隔通过网络传入的多个JSON消息?

Am I missing something obvious, or is there genuinely no standard way to separate between multiple JSON messages coming in over the wire?

推荐答案

您缺少了一些:-)

每个JSON-RPC消息都是有效的JSON值. JSON值可以是以下任意一种:

Each JSON-RPC message is a valid JSON value. A JSON value can by any of:

  • 一个Array
  • 一个Object
  • 一个String
  • 一个Number
  • an Array
  • an Object
  • a String
  • a Number

JSON-RPC信封是Object.

The JSON-RPC envelope is an Object.

如果这是原始套接字(例如Telnet),则...

If this were a raw socket (like Telnet), then...

对象以{开头,以}结束. 如果您在接收方使用正确的解析器(拉式或面向事件),则ObjectArray的嵌套方式无关紧要,您仍然会知道何时点击该解析器最后一个}.

Objects start with { and end with }. If you're using the right kind of parser (pull, or event-oriented) at your receiver then it doesn't matter how nested the Objects and Arrays get, you'll still know when you hit that last }.

以上消息,尤其是通知,可以以任何顺序出现在同一数据包中.

The above messages, particularly the notifications, could come in any order and in the same packet.

只要请求之间没有错位(在下一个请求开始之前完成一个请求),那么就没有问题.是否还需要换行符终止信封(也称为面向行的协议),由您决定.

As long as the requests are not interlaved (one finishes before the next starts), then there's no problem at all. It's up to you whether you also require newline termination of the envelope (a.k.a line-oriented protocol).

但是,由于您正在处理HTTP ...

However, as you're dealing with HTTP...

为什么不按照JSON-RPC规范仅使用 batch 消息?

Why not just use batch messages per the JSON-RPC spec?

这篇关于在JSON-RPC连接上读取多个JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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