Example #1
0
        //if access denied execute: "netsh http delete urlacl url=http://+:8001/" (delete for 'localhost', add for public address)
        //open Index.html to run the client
        static void Main(string[] args)
        {
            //generate js code
            File.WriteAllText($"./Site/{nameof(NumericService)}.js", RPCJs.GenerateCallerWithDoc <NumericService>());
            File.WriteAllText($"./Site/{nameof(TextService)}.js", RPCJs.GenerateCallerWithDoc <TextService>());

            //start server and bind its local and remote APIs
            var cts = new CancellationTokenSource();
            var t   = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, wc) =>
            {
                var path = wc.RequestUri.AbsolutePath;
                if (path == "/numericService")
                {
                    c.Bind(new NumericService());
                }
                else if (path == "/textService")
                {
                    c.Bind(new TextService());
                }
            });

            Console.Write("{0} ", nameof(MultiService));
            Process.Start(new ProcessStartInfo(Path.GetFullPath("./Site/Index.html"))
            {
                UseShellExecute = true
            });
            AppExit.WaitFor(cts, t);
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Server\n");

            //start server and bind to its local and remote API
            var cts = new CancellationTokenSource();
            var t   = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, wc) =>
            {
                //Connect action
                c.OnOpen += () => Task.Run((Action)connected);

                //Receive action
                c.OnReceive += async msg => {
                    await c.SendAsync("Hi!");
                    //Stampo su console
                    Console.WriteLine("Client message: \n" + msg);

                    //Rispondo con ack
                    await c.SendAsync("I've received the following message: " + msg);

                    //Chiudo connessione
                    //await c.CloseAsync();
                };

                //Disconnect action
                c.OnClose += (s, d) => Task.Run((Action)disconnected);
            });

            //Waiting for connection closing
            AppExit.WaitFor(cts, t);
        }
Example #3
0
        //if access denied execute: "netsh http delete urlacl url=http://+:8001/" (delete for 'localhost', add for public address)
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Server\n");

            //start server and bind to its local and remote API
            var cts = new CancellationTokenSource();
            var t   = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, wc) =>
            {
                c.Bind <TaskAPI, IProgressAPI>(new TaskAPI());

                c.OnOpen  += () => Task.Run((Action)writeClientCount);
                c.OnClose += (s, d) => Task.Run((Action)writeClientCount);
            });

            Console.Write("{0} ", nameof(TestServer));
            AppExit.WaitFor(cts, t);
        }
Example #4
0
        //if access denied execute: "netsh http delete urlacl url=http://+:8001/" (delete for 'localhost', add for public address)
        //open Index.html to run the client
        static void Main(string[] args)
        {
            //generate js code
            File.WriteAllText($"./Site/{nameof(TaskAPI)}.js", RPCJs.GenerateCallerWithDoc <TaskAPI>());

            //start server and bind its local and remote API
            var cts = new CancellationTokenSource();
            var t   = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, ws) =>
            {
                c.Bind <TaskAPI, IProgressAPI>(new TaskAPI());
                c.BindTimeout(TimeSpan.FromSeconds(1)); //close connection if there is no incommming message after X seconds
            });

            Console.Write("{0} ", nameof(ClientJs));
            Process.Start(new ProcessStartInfo(Path.GetFullPath("./Site/Index.html"))
            {
                UseShellExecute = true
            });
            AppExit.WaitFor(cts, t);
        }
Example #5
0
        //if access denied execute: "netsh http delete urlacl url=http://+:8001/" (delete for 'localhost', add for public address)
        //open Index.html to run the client
        static void Main(string[] args)
        {
            //generate js code (the API is empty)
            File.WriteAllText($"./Site/{nameof(PlatformInfo)}.js", RPCJs.GenerateCaller <PlatformInfo>());

            //start server and bind its local and remote API
            var cts = new CancellationTokenSource();
            var t   = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, ws) =>
            {
                var pInfo = new PlatformInfo();
                c.Bind <PlatformInfo, IBrowserInfo>(pInfo);
                c.OnOpen += pInfo.InitializeAsync;
            });

            Console.Write("{0} ", nameof(ClientInfoJs));
            Process.Start(new ProcessStartInfo(Path.GetFullPath("./Site/Index.html"))
            {
                UseShellExecute = true
            });
            AppExit.WaitFor(cts, t);
        }
Example #6
0
        //if access denied execute: "netsh http delete urlacl url=http://+:8001/" (delete for 'localhost', add for public address)
        //open Index.html to run the client
        static void Main(string[] args)
        {
            Directory.SetCurrentDirectory(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));

            Connection.MaxMessageSize = 1 * 1024 * 1024; //1MiB
            RPC.AddConverter(new JpgBase64Converter());

            //generate js code
            File.WriteAllText($"../../Site/{nameof(ImageProcessingAPI)}.js", RPCJs.GenerateCallerWithDoc <ImageProcessingAPI>());

            //start server and bind its local and remote API
            var cts = new CancellationTokenSource();
            var t   = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, ws) => c.Bind(new ImageProcessingAPI()));

            Console.Write("{0} ", nameof(Serialization));
            Process.Start(new ProcessStartInfo(Path.GetFullPath("../../Site/Index.html"))
            {
                UseShellExecute = true
            });
            AppExit.WaitFor(cts, t);
        }
Example #7
0
        //if access denied execute: "netsh http delete urlacl url=http://+:8001/" (delete for 'localhost', add for public address)
        //open Index.html to run the client
        static void Main(string[] args)
        {
            //set message limit
            Connection.MaxMessageSize = Connection.Encoding.GetMaxByteCount(40);

            //generate js code
            File.WriteAllText($"./Site/{nameof(MessagingAPI)}.js", RPCJs.GenerateCaller <MessagingAPI>());

            //start server
            var cts = new CancellationTokenSource();
            var t   = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, ws) =>
            {
                //set idle timeout
                c.BindTimeout(TimeSpan.FromSeconds(30));

                c.OnOpen  += async() => await c.SendAsync("Hello from server using WebSocketRPC");
                c.OnClose += (s, d) => Task.Run(() => Console.WriteLine("Connection closed: " + d));
                c.OnError += e => Task.Run(() => Console.WriteLine("Error: " + e.Message));

                c.OnReceive += async msg =>
                {
                    Console.WriteLine("Received: " + msg);

                    await c.SendAsync("Server received: " + msg);

                    if (msg.ToLower() == "close")
                    {
                        await c.CloseAsync(statusDescription: "Close requested by user.");
                    }
                };
            });

            Console.Write("{0} ", nameof(RawMsgJs));
            Process.Start(new ProcessStartInfo(Path.GetFullPath("./Site/Index.html"))
            {
                UseShellExecute = true
            });
            AppExit.WaitFor(cts, t);
        }
Example #8
0
        //if access denied execute: "netsh http delete urlacl url=http://+:8001/" (delete for 'localhost', add for public address)
        static void Main(string[] args)
        {
            //Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Client\n");

            //start client and bind to its local API
            var cts = new CancellationTokenSource();
            var t   = Client.ConnectAsync("ws://localhost:8001/", cts.Token, c =>
            {
                c.Bind <ProgressAPI, ITaskAPI>(new ProgressAPI());
                c.OnOpen += async() =>
                {
                    var r = await RPC.For <ITaskAPI>().CallAsync(x => x.LongRunningTask(5, 3));
                    Console.WriteLine("\nResult: " + r.First());
                };
            },
                                          reconnectOnError: true);

            Console.Write("{0} ", nameof(TestClient));
            AppExit.WaitFor(cts, t);
        }