JSONRPC over TCP/IP
#1
I'm working on implementing a JSON RPC connection over TCP/IP in C# and I have one fundamental issue. Currently I'm using a synchronous approach, which works well.

I can send

Code:
{"id":1,"jsonrpc":"2.0","method":"Input.Home"}

and receive

Code:
{"id":1,"jsonrpc":"2.0","result":true}

This works with no problems. The issue arises when I receive notifications. These can arrive unpredictably and at any time. If a notification has been sent by XBMC, I receive multiple JSON requests at once. E.g.

{"jsonrpc":"2.0","method":"GUI.OnScreensaverActivated","params":{"data":null,"sender":"xbmc"}}{"jsonrpc":"2.0","method":"GUI.OnScreensaverDeactivated","params":{"data":null,"sender":"xbmc"}}

This causes a crash in JSON.NET, and understandably so. My first instinct is I need to asynchronously receive these notifications so that I don't have to wait until the next method is called to receive them. However this complicates the simple example I showed above because I can no longer utilize the synchronous calls. i.e.
Code:
SendJson(json);
result = ReceiveJson();

Is there a clean way to implement this without over complicating it? Any/All advice is appreciated.
Reply
#2
I wrote a C# based JSON-RPC library a year or so ago and it was completely asynchronous over TCP and I didn't find it really complicating at all. If you don't want notifications you could use HTTP as a protocol. There you send a request and receive a response for that request.
Always read the online manual (wiki), FAQ (wiki) and search the forum before posting.
Do not e-mail Team Kodi members directly asking for support. Read/follow the forum rules (wiki).
Please read the pages on troubleshooting (wiki) and bug reporting (wiki) before reporting issues.
Reply
#3
Thanks Montellese, I saw your open source code based on HTTP. Would I need something along the lines of this for asynchronous?
Code:
public void Play()
{
    SendJson(PLAY_ID, Player.PlayPause(), activePlayer.id)
}

private void PlayResponse(JObject JSON)
{
    //Handle response here
}
Reply
#4
Well, I switched to HTTP for now. I'd still like to implement a TCP based connection. If you'd be able to show me some of your code it would help out a lot. I think I have a pretty good idea of how to implement it, but I'd like to do it right the first time.My code will be open-source.
Reply
#5
See https://github.com/Montellese/xbmc-jsonrpc-sharp but the API itself is very outdated but as that's not what you are interested in, feel free to take a look at the implementation of XbmcJsonRpcConnection.
Always read the online manual (wiki), FAQ (wiki) and search the forum before posting.
Do not e-mail Team Kodi members directly asking for support. Read/follow the forum rules (wiki).
Please read the pages on troubleshooting (wiki) and bug reporting (wiki) before reporting issues.
Reply
#6
Thanks, Montellese. That is very helpful. I hadn't considered using HTTP for function calls and TCP for Notifications.
Reply
#7
It is better go through TCP/IP basic socket communication http://vb.net-informations.com/communica...amming.htm hope this will help you in basic level.

wills.
Reply

Logout Mark Read Team Forum Stats Members Help
JSONRPC over TCP/IP0