1

Closed

Standalone host

description

Hi there, I was able to host json-rpoc.net over raw sockets using the following code piece; can be usefull to others;
            var server = new TcpListener(IPAddress.Parse("127.0.0.1"), 3333);
            server.Start();

            while (true)
            {
                try
                {
                    using (var client = server.AcceptTcpClient())
                    {
                        var rpcResultHandler = new AsyncCallback(
                            callback =>
                            {
                                var async = ((JsonRpcStateAsync)callback);
                                var result = async.Result;
                                var writer = ((StreamWriter)async.AsyncState);

                                writer.WriteLine(result);
                                writer.FlushAsync();
                                Log.Debug("RESPONSE: {Response}", result);
                            });

                        using (var stream = client.GetStream())
                        {
                            var reader = new StreamReader(stream, Encoding.UTF8);
                            var writer = new StreamWriter(stream, new UTF8Encoding(false));

                            while (!reader.EndOfStream)
                            {
                                var line = reader.ReadLine();
                                
                                var async = new JsonRpcStateAsync(rpcResultHandler, writer) { JsonRpc = line };
                                JsonRpcProcessor.Process(async, writer);

                                Log.Debug("REQUEST: {Request}", line);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e, "RPCServer exception");
                }
            }
Closed Dec 9, 2013 at 6:21 PM by AustinHarris
An example has been added to the documentation page based on this code.

comments

AustinHarris wrote Dec 7, 2013 at 4:54 PM

Nice one. Maybe I can use this to make another getting started example for the documentation.

raistlinthewiz wrote Dec 7, 2013 at 8:16 PM

I'm sure it will be great for new-comers, I had to struggle a bit to get it working over raw sockets.

AustinHarris wrote Dec 9, 2013 at 6:18 PM

I have added an example based on your code here.

TEXT

If you have any comments, or think the code could be presented better, just say. Thanks for you help on this!

wrote Dec 9, 2013 at 6:21 PM