我需要一个简单的C#中的JSON-RPC 1.0客户端,最好使用.NET 2.0或更高版本。 我检出了JRock 0.9 他们有几个样本,包括雅虎阅读器,但样本演示JSON,而不是JSON-RPC。 我明白我可以使用任何可用的JSON解析器来实现RPC部分,如JRock或Microsoft的两个解析器。我更喜欢现成的样品。C#中JSON-RPC客户端的示例代码
3
A
回答
4
有两种不同的实现。阅读整个主题+检查附件
0
下面是通过Observables(Rx)公开的.net4客户端的示例。 http://jsonrpc2.codeplex.com/SourceControl/changeset/view/13061#63133
这是一个几乎完全相同的wp7客户端,它也通过Rx公开。 http://jsonrpc2.codeplex.com/SourceControl/changeset/view/13061#282775
这两个例子都是异步执行它们的工作,所以它们可能比你想要的更复杂,除非你想要异步的例子。 :)
祝你好运!
4
上面的示例使用HTTP请求。这里是与TCP工作的变种(未缴类是用于没有返回值的要求只是一个空的类):
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Newtonsoft.Json.Linq;
using AustinHarris.JsonRpc;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Concurrency;
using System.Net.Sockets;
using System.Text;
namespace JsonRpc
{
public class JsonRpcClient
{
private static object idLock = new object();
private static int id = 0;
public Encoding encoding { get; set; }
public JsonRpcClient(IPEndPoint serviceEndpoint, Encoding encoding)
{
this.serviceEndPoint = serviceEndpoint;
this.encoding = encoding;
}
private static Stream CopyAndClose(Stream inputStream)
{
const int readSize = 256;
byte[] buffer = new byte[readSize];
MemoryStream ms = new MemoryStream();
int count = inputStream.Read(buffer, 0, readSize);
while (count > 0)
{
ms.Write(buffer, 0, count);
count = inputStream.Read(buffer, 0, readSize);
}
ms.Position = 0;
inputStream.Close();
return ms;
}
public IObservable<JsonResponse<T>> InvokeWithScheduler<T>(string method, object arg, IScheduler scheduler)
{
var req = new AustinHarris.JsonRpc.JsonRequest()
{
Method = method,
Params = new object[] { arg }
};
return InvokeRequestWithScheduler<T>(req, scheduler);
}
public IObservable<JsonResponse<T>> InvokeSingleArgument<T>(string method, object arg)
{
var req = new AustinHarris.JsonRpc.JsonRequest()
{
Method = method,
Params = new object[] { arg }
};
return InvokeRequest<T>(req);
}
public IObservable<JsonResponse<T>> InvokeWithScheduler<T>(string method, object[] args, IScheduler scheduler)
{
var req = new AustinHarris.JsonRpc.JsonRequest()
{
Method = method,
Params = args
};
return InvokeRequestWithScheduler<T>(req, scheduler);
}
public IObservable<JsonResponse<T>> Invoke<T>(string method, object[] args)
{
var req = new AustinHarris.JsonRpc.JsonRequest()
{
Method = method,
Params = args
};
return InvokeRequest<T>(req);
}
public IObservable<JsonResponse<T>> InvokeRequestWithScheduler<T>(JsonRequest jsonRpc, IScheduler scheduler)
{
var res = Observable.Create<JsonResponse<T>>((obs) =>
scheduler.Schedule(()=>{
makeRequest<T>(jsonRpc, obs);
}));
return res;
}
public IObservable<JsonResponse<T>> InvokeRequest<T>(JsonRequest jsonRpc)
{
return InvokeRequestWithScheduler<T>(jsonRpc, ImmediateScheduler.Instance);
}
private string sendAndReceive(string messageToSend) {
string res = null;
// Data buffer for incoming data.
byte[] bytes = new byte[1024];
// Connect to a remote device.
try {
// Create a TCP/IP socket.
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect the socket to the remote endpoint. Catch any errors.
try {
socket.Connect(this.serviceEndPoint);
Console.Write("Socket connected to "+socket.RemoteEndPoint.ToString());
// Encode the data string into a byte array.
byte[] msg = encoding.GetBytes(messageToSend);
// Send the data through the socket.
int bytesSent = socket.Send(msg);
// Receive the response from the remote device.
int bytesRec = socket.Receive(bytes);
res = encoding.GetString(bytes,0,bytesRec);
Console.Write("Server response = "+res);
// Release the socket.
socket.Shutdown(SocketShutdown.Both);
socket.Close();
} catch (ArgumentNullException ane) {
Console.Write("ArgumentNullException : "+ane.ToString());
} catch (SocketException se) {
Console.Write("SocketException : " + se.ToString());
} catch (Exception e) {
Console.Write("Unexpected exception : " + e.ToString());
}
} catch (Exception e) {
Console.Write(e.ToString());
}
return res;
}
private void makeRequest<T>(JsonRequest jsonRpc, IObserver<JsonResponse<T>> obs)
{
JsonResponse<T> rjson = null;
string sstream = "";
try
{
int myId;
lock (idLock)
{
myId = ++id;
}
jsonRpc.Id = myId.ToString();
}
catch (Exception ex)
{
obs.OnError(ex);
}
try
{
var json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonRpc)+"\r\n";
if (typeof(T).Equals(typeof(Nil)))
{
sendAndReceive(json);
rjson = new JsonResponse<T>();
}
else
{
sstream = sendAndReceive(json);
rjson = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonResponse<T>>(sstream);
}
}
catch (Exception ex)
{
obs.OnError(ex);
}
if (rjson == null)
{
string exceptionMessage = "";
try
{
JObject jo = Newtonsoft.Json.JsonConvert.DeserializeObject(sstream) as JObject;
exceptionMessage = jo["Error"].ToString();
}
catch(Exception ex){
exceptionMessage = sstream+"\r\n"+ex.Message;
}
obs.OnError(new Exception(exceptionMessage));
}
else
{
obs.OnNext(rjson);
}
obs.OnCompleted();
}
public IPEndPoint serviceEndPoint { get; set; }
}
}
我无法缠绕我的头围绕一个名为“援引
相关问题
- 1. C#REST客户端示例?
- 2. c#休息客户端示例
- 3. 西门子OPC客户端的VB6代码示例?
- 4. 使用Bluez的代码示例HID客户端
- 5. SpnegoContextToken与Java客户端的任何代码示例?
- 6. FreeOPCUA python客户端示例
- 7. 黑莓推送集成客户端示例代码
- 8. 从Silverlight客户端示例代码调用WebSocket?
- 9. BlackBerry客户端代码
- 10. 节点js中的客户端代码
- 11. Elasticsearch客户端java.lang.ExceptionInInitializerError 5.3中的代码
- 12. c#/。net中的IBM Watson Conversation API客户端的示例网络
- 13. myBB密码c#客户端
- 14. 显示属性中的客户端代码
- 15. 客户端的JavaScript代码来的CKEditor
- 16. 如何实现一个双向jsonrpc + twisted服务器/客户端
- 17. 客户端自己的跟踪代码
- 18. C中的REST客户端
- 19. C中的DNS客户端
- 20. C#中的UDP客户端
- 21. Android resteasy-mobile示例客户端
- 22. Jersey 2.25.1客户端记录示例
- 23. 任何SourceGear Vault客户端API示例?
- 24. javax.websocket客户端简单示例
- 25. SOAP客户端,以下示例
- 26. spring ws客户端拦截器示例
- 27. Jetty HTTP/2客户端示例
- 28. Mode Mode Bom远程客户端示例
- 29. 处理Google客户端登录验证码示例
- 30. DirectX示例代码C++
。我确信我在这里错过了一些东西,你介意给我一些关于为什么将方法命名为关键字是合法的更多信息?谢谢! – Rishi 2014-09-30 21:44:10
你是指使用泛型方法吗? http://msdn.microsoft.com/en-us/library/twcad0zb.aspx – 2014-10-01 15:34:23