Esempio n. 1
0
        protected override async Task OnInitializedAsync()
        {
            try
            {
                string baseUrl = NavigationManager.BaseUri;

                _hubUrl = baseUrl.TrimEnd('/') + "/chatHub";


                hubConnection = new HubConnectionBuilder()
                                .WithUrl(_hubUrl, options =>
                {
                    options.AccessTokenProvider = async() =>
                    {
                        var accessTokenResult = await AccessTokenProvider.GetAuthenticationStateAsync();

                        return(await storage.GetTokenAsync());
                    };
                })
                                .Build();

                hubConnection.On <MessageViewModel>("newMessage", async(message) =>
                {
                    messages.Insert(0, message);
                    StateHasChanged();
                });
                hubConnection.On <string>("onError", async(message) =>
                {
                    MessageViewModel msg = new MessageViewModel();
                    msg.Content          = message;

                    messages.Insert(0, msg);
                    StateHasChanged();
                });
                hubConnection.On <UserViewModel>("joinedRoom", async(message) =>
                {
                    inputModel.CurrentRoom = message.CurrentRoom;
                    messages.Clear();
                    IList <MessageViewModel> mess = await hubConnection.InvokeAsync <IList <MessageViewModel> >("GetMessageHistory", inputModel.Room);
                    foreach (var m in mess)
                    {
                        messages.Insert(0, m);
                    }
                    StateHasChanged();
                });


                hubConnection.On <UserViewModel>("removeUser", async(message) =>
                {
                    users = await hubConnection.InvokeAsync <IList <UserViewModel> >("GetUsers", message.CurrentRoom);
                    StateHasChanged();
                });
                hubConnection.On <UserViewModel>("addUser", async(message) =>
                {
                    users = await hubConnection.InvokeAsync <IList <UserViewModel> >("GetUsers", message.CurrentRoom);
                    StateHasChanged();
                });
                hubConnection.On <RoomViewModel>("removeChatRoom", async(message) =>
                {
                    rooms = await hubConnection.InvokeAsync <IList <RoomViewModel> >("GetRooms");
                    StateHasChanged();
                });
                hubConnection.On <RoomViewModel>("addChatRoom", async(message) =>
                {
                    rooms = await hubConnection.InvokeAsync <IList <RoomViewModel> >("GetRooms");
                    StateHasChanged();
                });
                hubConnection.On <string>("onRoomDeleted", async(message) =>
                {
                    inputModel.Room      = "Lobby";
                    MessageViewModel msg = new MessageViewModel();
                    msg.Content          = $"Room was deleted, you are now moved back to {inputModel.Room}";

                    messages.Insert(0, msg);
                    await JoinRoom();
                    StateHasChanged();
                });


                await hubConnection.StartAsync();

                username = await hubConnection.InvokeAsync <string>("WhoAmI");

                inputModel.Room     = "Lobby";
                inputModel.Recipent = "Room";
                rooms = await hubConnection.InvokeAsync <IList <RoomViewModel> >("GetRooms");

                await JoinRoom();
            }
            catch (Exception ex)
            {
                MessageViewModel msg = new MessageViewModel();
                msg.Content = ex.Message;
                messages.Insert(0, msg);
            }
            finally
            {
                //  this.StateHasChanged();
            }
        }