Example #1
0
        public void SendMessage(string message)
        {
            try
            {
                // Create the message to send
                StringMessage msg = new StringMessage
                {
                    Message = message
                };

                // Serialize the message to a binary array
                byte[] binaryMessage = SerializationHelper.Serialize(msg);

                // Send the message; the state is used by ClientSocket_WriteCompleted to display an output to the log
                string description = "<string message: " + msg.Message + ">";
                ClientSocket.WriteAsync(binaryMessage, description);

                Console.WriteLine("Sending message " + description);
            }
            catch (Exception ex)
            {
                ResetSocket();
                Console.WriteLine("Error sending message to socket: [" + ex.GetType().Name + "] " + ex.Message);
            }
            finally
            {
                RefreshDisplay();
            }
        }
Example #2
0
        public void SendMessage(KeyValuePair<SimpleServerChildTcpSocket, ChildSocketState> childSocket, string message)
        {
            // This function sends a simple (string) message to all connected clients
            StringMessage msg = new StringMessage();
            msg.Message = message;

            string description = "<string message: " + msg.Message + ">";

            // Serialize it to a binary array
            byte[] binaryObject = SerializationHelper.Serialize(msg);

            // Start a send on child socket

            // Ignore sockets that are disconnecting
            if (childSocket.Value == ChildSocketState.Connected)
            {
                try
                {
                    ConsoleService.Write("Sending to " + childSocket.Key.RemoteEndPoint + ": " + description);
                    childSocket.Key.WriteAsync(binaryObject, description);
                }
                catch (Exception ex)
                {
                    // Handle error
                    ConsoleService.Write("Child Socket error sending message to " + childSocket.Key.RemoteEndPoint + ": [" + ex.GetType().Name + "] " + ex.Message);
                    ResetChildSocket(childSocket.Key);
                }
            }

            // In case there were any errors, the display may need to be updated
            RefreshDisplay();
        }