Example #1
0
        //
        // FUNCTION : Connect
        //
        // DESCRIPTION : This method will connect to the server for the chat client. It will generate a GUID if it needs one and
        //               will send a clientInfo class object with the information to the user. It will also create the message
        //               queue that this client will use and send the connection string in the object as well.
        //
        // PARAMETERS : NA
        // RETURNS : None
        //
        public void Connect()
        {
            string mQueueGeneral = @"FormatName:Direct=TCP:" + connectBox.Text.ToString() + "\\private$\\genMQ";//Using the IP address entered for the connection string

            try
            {
                myClient = new ClientInfo();
                genMQ    = new MessageQueue(mQueueGeneral);//connect to message

                //check to see if general queue is empty
                if (IsQueueEmpty(genMQ))
                {
                    //function will throw exception if queue doesn't exist
                }
                genMQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(ClientInfo) }); //format the messages to allow for passing classinfo

                myClient.clientID  = Guid.NewGuid().ToString();                               //creation of a unique GUID for client
                myClient.clientIP  = getIP();                                                 //creation of an IP for client
                myClient.connected = true;
                genMQ.Send(myClient);
                genMQ.Close();

                try
                {
                    clientQueue = MessageQueue.Create(@".\private$\" + myClient.clientID);
                }
                catch (Exception e)
                {
                    System.Windows.MessageBox.Show("EX: {0}" + e.Message);
                }
                finally
                {
                    clientQueue           = new MessageQueue(@".\private$\" + myClient.clientID);
                    clientQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(ClientInfo) });
                    loop.Start();
                    TextBoxIn.Focus();
                }

                connectBox.Visibility         = Visibility.Hidden;
                connectToServer.Visibility    = Visibility.Hidden;
                connectionAccepted.Text       = "Status: Connected";
                connectionAccepted.Visibility = Visibility.Visible;
                TextSendBtn.IsEnabled         = true;
            }
            catch (MessageQueueException)
            {
                System.Windows.MessageBox.Show("Sorry, could not connect to message queue. Try again!");
            }
            catch (Exception a)
            {
                System.Windows.MessageBox.Show(a.ToString());
                connectBox.IsEnabled          = true;
                connectToServer.IsEnabled     = true;
                connectionAccepted.Text       = "Could not connect!";
                connectionAccepted.Visibility = Visibility.Visible;
            }
        }