Exemplo n.º 1
0
        private async Task Connect()
        {
            if (_hubConnection != null)
            {
                if (_hubConnection.State != ConnectionState.Disconnected)
                {
                    return;
                }

                // Dispose/Stop the old connection on a bg thread, we don't care about it anymore
                var oldConnection = _hubConnection;
                Task.Run(() => oldConnection.Dispose()).Forget();
            }

            Status = "Connecting...";

            // The DispatchingHubConnection will execute all callbacks via the Dispatcher (UI thread)
            // so the need for locking and posting is drastically reduced.
            _hubConnection = new DispatchingHubConnection(_hubUrl, Dispatcher);

            // Enable tracing
            _hubConnection.TraceLevel  = TraceLevels.All;
            _hubConnection.TraceWriter = new CollectionTraceWriter(LogMessages, Dispatcher);

            // Handle the connection lifetime events
            _hubConnection.Reconnecting += () =>
            {
                if (_hubConnection.State == ConnectionState.Reconnecting)
                {
                    CanSend = false;
                    Status  = "Connection reconnecting...";
                }
            };
            _hubConnection.Reconnected += () =>
            {
                if (_hubConnection.State == ConnectionState.Connected)
                {
                    Status  = string.Format("Connected to {0} via {1}", _hubUrl, _hubConnection.Transport.Name);
                    CanSend = true;
                }
            };
            _hubConnection.Closed += async() =>
            {
                if (_hubConnection.State == ConnectionState.Disconnected)
                {
                    CanSend = false;
                    Status  = "Connection lost, reconnecting in a bit...";
                    await Task.Delay(_reconnectDelay);
                    await Connect();
                }
            };
            _hubConnection.Error += ex =>
            {
                LogMessages.Add(ex.ToString());
            };

            _hubProxy = _hubConnection.CreateHubProxy("chat");

            _hubProxy.On <string>("userJoined", userName =>
            {
                Messages.Add(new Message(string.Format("{0} joined", userName)));
            });
            _hubProxy.On <string, string>("newMessage", (userName, message) =>
            {
                Messages.Add(new Message(message, userName));
            });

            while (true)
            {
                try
                {
                    // HTTP streaming transports don't work well on the Windows Phone stack so we force long polling.
                    // We're adding support for WebSockets to Windows Store/Phone 8.1 apps in SignalR 2.2.0
                    await _hubConnection.Start(new LongPollingTransport());

                    if (_hubConnection.State == ConnectionState.Connected)
                    {
                        Status  = string.Format("Connected to {0} via {1}", _hubUrl, _hubConnection.Transport.Name);
                        CanSend = true;
                        break;
                    }
                }
                catch (Exception)
                {
                    Status = string.Format("Connecting to {0} failed, trying again in a bit", _hubUrl);
                }
                await Task.Delay(_reconnectDelay);
            }
        }
Exemplo n.º 2
0
        private async Task Connect()
        {
            if (_hubConnection != null)
            {
                if (_hubConnection.State != ConnectionState.Disconnected)
                {
                    return;
                }

                // Dispose/Stop the old connection on a bg thread, we don't care about it anymore
                var oldConnection = _hubConnection;
                Task.Run(() => oldConnection.Dispose()).Forget();
            }

            Status = "Connecting...";

            // The DispatchingHubConnection will execute all callbacks via the Dispatcher (UI thread)
            // so the need for locking and posting is drastically reduced.
            _hubConnection = new DispatchingHubConnection(_hubUrl, Dispatcher);

            // Enable tracing
            _hubConnection.TraceLevel = TraceLevels.All;
            _hubConnection.TraceWriter = new CollectionTraceWriter(LogMessages, Dispatcher);

            // Handle the connection lifetime events
            _hubConnection.Reconnecting += () =>
            {
                if (_hubConnection.State == ConnectionState.Reconnecting)
                {
                    CanSend = false;
                    Status = "Connection reconnecting...";
                }
            };
            _hubConnection.Reconnected += () =>
            {
                if (_hubConnection.State == ConnectionState.Connected)
                {
                    Status = string.Format("Connected to {0} via {1}", _hubUrl, _hubConnection.Transport.Name);
                    CanSend = true;
                }
            };
            _hubConnection.Closed += async () =>
            {
                if (_hubConnection.State == ConnectionState.Disconnected)
                {
                    CanSend = false;
                    Status = "Connection lost, reconnecting in a bit...";
                    await Task.Delay(_reconnectDelay);
                    await Connect();
                }
            };
            _hubConnection.Error += ex =>
            {
                LogMessages.Add(ex.ToString());
            };

            _hubProxy = _hubConnection.CreateHubProxy("chat");

            _hubProxy.On<string>("userJoined", userName =>
            {
                Messages.Add(new Message(string.Format("{0} joined", userName)));
            });
            _hubProxy.On<string, string>("newMessage", (userName, message) =>
            {
                Messages.Add(new Message(message, userName));
            });

            while (true)
            {
                try
                {
                    // HTTP streaming transports don't work well on the Windows Phone stack so we force long polling.
                    // We're adding support for WebSockets to Windows Store/Phone 8.1 apps in SignalR 2.2.0
                    await _hubConnection.Start(new LongPollingTransport());
                    if (_hubConnection.State == ConnectionState.Connected)
                    {
                        Status = string.Format("Connected to {0} via {1}", _hubUrl, _hubConnection.Transport.Name);
                        CanSend = true;
                        break;
                    }
                }
                catch (Exception)
                {
                    Status = string.Format("Connecting to {0} failed, trying again in a bit", _hubUrl);
                }
                await Task.Delay(_reconnectDelay);
            }
        }