protected async override void OnNavigatedFrom(NavigationEventArgs e) { base.OnNavigatedFrom(e); // Tell Chatrrom you are leaving. await SignalRChatHub.Invoke("LeaveChat", "DeviceID123", "ChatUserSam"); // Unwire event handler. SignalRServerNotification -= new SignalRServerHandler(SignalRHub_SignalRServerNotification); }
protected async override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); HubConnection chatConnection = new HubConnection("http://localhost:1141/"); SignalRChatHub = chatConnection.CreateHubProxy("ChatHub"); // Wire-up to listen to custom event from SignalR Hub. SignalRServerNotification += new SignalRServerHandler(SignalRHub_SignalRServerNotification); // Start connection back to Hub. try { await chatConnection.Start(); if (chatConnection.State == Microsoft.AspNet.SignalR.Client.ConnectionState.Connected) { // Join the ChatRoom. await SignalRChatHub.Invoke("JoinChat", "DeviceID123", "ChatUserSam"); // Listen to chat events on SignalR Server & wire them up appropriately. SignalRChatHub.On <string>("addChatMessage", message => { SignalREventArgs chatArgs = new SignalREventArgs(); chatArgs.ChatMessageFromServer = message; // Raise custom event & let it bubble up. SignalRServerNotification(this, chatArgs); }); } } catch (Exception) { // Do some error handling. } }