Example #1
0
        static void Main(string[] args)
        {
            encoding = Encoding.ASCII;
            SocketServer server;
            server = new SocketServer(new IPEndPoint(IPAddress.Loopback, 0));

            server.Connected += c =>
                {
                    serverClient = new FramedClient(c);

                    // When received is called, bs will contain no more and no less
                    // data than whole packet as sent from client.
                    serverClient.Received += bs =>
                        {
                            var msg = encoding.GetString(bs.Array, bs.Offset, bs.Count);
                            msg = "Hello, " + msg + "!";
                            serverClient.SendPacket(encoding.GetBytes(msg));
                        };
                };

            server.Start();
            HandleClient(server.BindEndPoint.Port);
            
            Console.WriteLine("Press any key to stop...");
            Console.ReadKey();

            server.Stop();
            client.Close();
            serverClient.Close();
        }
Example #2
0
        public void Run()
        {
            service = new TemperatureService();
            server = new SocketServer(new IPEndPoint(IPAddress.IPv6Any, 0));

            server.Connected.Subscribe(ClientConnected);

            server.Start();
            this.ServerPort = server.BindEndPoint.Port;

            service.TemperatureChanged += TempChanged;
        }
Example #3
0
        public void Run()
        {
            ReactiveMessageClient<IServerPacketHandler> client = null;

            service = new MarketService();
            server = new SocketServer(new IPEndPoint(IPAddress.Loopback, 0));

            server.Connected.Subscribe(c =>
                {
                    client = new ReactiveMessageClient<IServerPacketHandler>(
                                    new FramedClient(c),
                                    new ProtoBufStacksSerializer());
                    client.PreLoadTypesFromAssemblyOfType<Price>();

                    // Here is the subscription to all the packets.
                    // `.Packets` is an implementation of interface
                    // given when message client was defined, so
                    // every packet has a strongly typed `IObservable<TPacket>`
                    // to subscribe to.
                    client.Packets.RegisterSymbol.Subscribe(req =>
                        {
                            if (req.Register)
                                service.RegisterSymbol(req.Symbol);
                            else
                                service.UnregisterSymbol(req.Symbol);
                        });
                });

            // Service will only report registered price changes.
            // In this sample, this observer should not run 
            // for 4 seconds after application start.
            service.PricesChanged.Subscribe(prices =>
                {
                    Console.WriteLine("Services reported that prices changed");

                    // Send new prices to client.
                    foreach (var p in prices)
                    {
                        if (client != null)
                            client.Send(p);
                    }
                });

            server.Start();
            this.ServerPort = server.BindEndPoint.Port;
        }