Kodi Community Forum
JSONRPC over TCP/IP - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Development (https://forum.kodi.tv/forumdisplay.php?fid=32)
+--- Forum: Kodi Application (https://forum.kodi.tv/forumdisplay.php?fid=93)
+---- Forum: JSON-RPC (https://forum.kodi.tv/forumdisplay.php?fid=174)
+---- Thread: JSONRPC over TCP/IP (/showthread.php?tid=130201)



JSONRPC over TCP/IP - mouser58907 - 2012-04-28

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.



RE: JSONRPC over TCP/IP - Montellese - 2012-04-28

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.


RE: JSONRPC over TCP/IP - mouser58907 - 2012-04-28

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
}



RE: JSONRPC over TCP/IP - mouser58907 - 2012-05-03

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.


RE: JSONRPC over TCP/IP - Montellese - 2012-05-03

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.


RE: JSONRPC over TCP/IP - mouser58907 - 2012-05-04

Thanks, Montellese. That is very helpful. I hadn't considered using HTTP for function calls and TCP for Notifications.


RE: JSONRPC over TCP/IP - willsmannar - 2012-09-09

It is better go through TCP/IP basic socket communication http://vb.net-informations.com/communications/vb.net_socket_programming.htm hope this will help you in basic level.

wills.