Exemple #1
0
        private void ConnectAsync()
        {
            Connection           = new HubConnection(ServerURI);
            m_ChatFormDictionary = new Dictionary <string, ChatForm>();
            m_UserDictionary     = new Dictionary <string, UserPerson>();
            HubProxy             = Connection.CreateHubProxy("MyHub");

            HubProxy.On <string, char>("BroadcastOnlineStatus", (displayName, status) => Invoke((Action)(() => ChangeStatus(displayName, status))));
            HubProxy.On <Messages>("SendMessageToClient", (message) => Invoke((Action)(() => AppearMessage(message))));
            HubProxy.On <List <Messages> >("SendOfflineMessage", (offlineMessage) => Invoke((Action)(() => AppearOfflineMessage(offlineMessage))));



            try
            {
                Connection.Start().ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        //Connection error
                        Invoke((MethodInvoker) delegate
                        {
                            MessageBox.Show(task.Exception.Message);
                        });
                    }
                    else
                    {
                        //connected
                        Invoke((MethodInvoker) delegate
                        {
                            label3.Text = "Connected to server at " + ServerURI + "";
                            up          = new UserPerson(m_userName, m_passWord)
                            {
                                Status = 'O'
                            };
                            getUserList();
                            bool m_result       = HubProxy.Invoke <bool>("Login", up).Result;
                            string connectionId = HubProxy.Invoke <string>("GetConnectionId").Result;
                            if (m_result == false)
                            {
                                MessageBox.Show("Log in failed.");
                            }
                            else
                            {
                                //MessageBox.Show(connectionId);

                                showUserList();
                                buttonLogin.Enabled = false;
                            }
                        });
                    }
                });
            }
            catch (Exception)
            {
            }
        }
Exemple #2
0
        private async void ConnectAsync()
        {
            Connection         = new HubConnection(ServerURI);
            Connection.Closed += Connection_Closed;
            HubProxy           = Connection.CreateHubProxy("MyHub");
            //Handle incoming event from server: use Invoke to write to console from SignalR's thread
            HubProxy.On <string, string>("AddMessage", (name, message) =>
                                         this.Invoke((Action)(() =>
                                                              richTextBox1.AppendText(String.Format("{0}: {1}" + Environment.NewLine, name, message))
                                                              ))
                                         );
            HubProxy.On("Started", () =>
                        this.Invoke((Action)(() =>
                                             richTextBox1.AppendText(String.Format("Exam Started" + Environment.NewLine))
                                             ))
                        );

            HubProxy.On <string>("NewClient", (name) =>
                                 this.Invoke((Action)(() =>
                                                      richTextBox1.AppendText(String.Format("Client " + name + " Connected" + Environment.NewLine))
                                                      ))
                                 );
            HubProxy.On("FailedToStart", () =>
                        this.Invoke((Action)(() =>
                                             richTextBox1.AppendText(String.Format("Exam Failed To Start!" + Environment.NewLine))
                                             ))
                        );
            HubProxy.On("connected", () =>
                        this.Invoke((Action)(() => {
                richTextBox1.AppendText(String.Format("Connected as " + ChoosenName + "!" + Environment.NewLine));
                btn_send.Enabled = true;
                btn_send.Text = "Send";
            }
                                             ))
                        );
            HubProxy.On("rejected", () =>
                        this.Invoke((Action)(() =>
            {
                richTextBox1.AppendText(String.Format("Cannot connect with name of " + ChoosenName + "!" + Environment.NewLine));
                this.ChoosenName = null;
                btn_send.Enabled = true;
                btn_send.Text = "Join Chat";
            }
                                             ))
                        );
            HubProxy.On <int>("TimeLeft", (timePassed) =>

                              this.Invoke((Action)(() =>
            {
                TimeSpan sinavSüresi = TimeSpan.FromMinutes(30);
                var span = sinavSüresi.Subtract(TimeSpan.FromSeconds(timePassed));
                lbl_time_left.Text = string.Format("{0}:{1}:{2}", span.Hours.ToString("0#"), span.Minutes.ToString("0#"), span.Seconds.ToString("0#"));
            })

                                          ));

            try
            {
                await Connection.Start();
            }
            catch (HttpRequestException)
            {
                btn_send.Enabled  = false;
                btn_start.Enabled = false;
                richTextBox1.AppendText("Connected failed to " + ServerURI + Environment.NewLine);
                return;
            }

            richTextBox1.AppendText("Connected to server at " + ServerURI + Environment.NewLine);
        }