Example #1
0
        void CreateClient()
        {
            // Create the client
            var client = new SimplMessageClient();

            // Make the client connect automatically
            client.AutoConnect();

            // Create a callback for received data of type classA. This time we use a lambda function instead
            client.AddCallBack <ClassA>((receivedMessage) =>
            {
                // get data from received message cast to ClassA
                var receivedObject = receivedMessage.GetContent <ClassA>();

                // Notify that the server received data
                Console.WriteLine($"Client received message: {receivedObject.VarDouble}, {receivedObject.VarInt}");
            });
        }
Example #2
0
        void CreateClient()
        {
            // Create the client
            _client = new SimplMessageClient(
                () => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
                keepAlive: true,
                messageBufferSize: 65536,
                communicationTimeout: 10000,
                maxMessageSize: 10485760,
                useNagleAlgorithm: false);


            // Add a callback that will be triggered when a message from the server is received
            _client.AddCallBack <ClassB>(ClientReceivedClassBCallback);

            // Indicate when connected
            _client.Connected += (s, e) => {
                Console.WriteLine($"Client: connected to {e.IPEndPoint.Address}!");
            };

            // Indicate when disconnected
            _client.Disconnected += (s, e) =>
            {
                //Disconnected not triggered, due to botched wasconnected logic
                Console.WriteLine("Client: disconnected");
            };

            // Indicate when trying to connect
            _client.ConnectionAttempt += (s, e) =>
            {
                Console.WriteLine($"Client: trying to connect to {e.IPEndPoint.Address}!");
            };

            // Make the client connect automatically
            _client.AutoConnect("ServerName");

            // Wait until the connection is actually made
            _client.WaitForConnection();

            // Create an object to send
            var objectToSend = new ClassA()
            {
                VarInt = 2, VarDouble = 2.5
            };

            // Send it
            Console.WriteLine($"Client: sending message: {objectToSend.VarDouble}, {objectToSend.VarInt}");
            _client.Send(objectToSend);

            // Wait some time for the server to respond.
            // This is needed, because if the server responds while the client is not connected, the respons is lost
            // there is no resend functionality
            Thread.Sleep(100);

            // Change mode to fixed IP auto connection
            _client.AutoConnect(new IPEndPoint(IPAddress.Loopback, 5000));

            // Now disconnect the previous connection
            _client.Disconnect();

            // Wait until the new connection is actually made based on the fixed IP
            _client.WaitForConnection();

            // Send the object again
            Console.WriteLine($"Client: sending message: {objectToSend.VarDouble}, {objectToSend.VarInt}");
            _client.Send(objectToSend);

            // Wait some time for the server to respond
            Thread.Sleep(100);

            // Change to manual connection, this will disable autoconnecting
            _client.Connect(new IPEndPoint(IPAddress.Loopback, 5000));

            // The previous function should block until the connection is made, so we will not wait, but just check
            if (_client.IsConnected())
            {
                Console.WriteLine("Client: Connected");
            }
            else
            {
                Console.WriteLine("Client: not connected, should not happen!");
            }

            // Send the object again
            Console.WriteLine($"Client: sending message: {objectToSend.VarDouble}, {objectToSend.VarInt}");
            _client.Send(objectToSend);

            // Wait some time for the server to respond
            Thread.Sleep(100);

            // Now disconnect the previous connection
            _client.Disconnect();

            // Since we have disabled the auto connect, there should be no reconnection,
            // so we'll add a timeout to prevent hanging
            bool connected = _client.WaitForConnection(TimeSpan.FromSeconds(10));

            if (connected)
            {
                Console.WriteLine("Client: Connected, even though we disabled autoconnect!");
            }
            else
            {
                Console.WriteLine("Client: did not reconnect, because we disabled autoconnect");
            }
        }