120 lines
3.2 KiB
Plaintext
120 lines
3.2 KiB
Plaintext
@page "/chat/{room?}"
|
|
@using Microsoft.AspNetCore.SignalR.Client;
|
|
@using LiveChat.Models;
|
|
@inject NavigationManager Navigation
|
|
|
|
<PageTitle>Chat room</PageTitle>
|
|
|
|
|
|
@if(Room is null or "")
|
|
{
|
|
<p class="alert alert-info"><i class="bi bi-exclamation-circle-fill"></i> Fant ikke rom i URL. Lag et rom og putt det i URLen.</p>
|
|
}
|
|
|
|
<h1>Rom @Room</h1>
|
|
|
|
<p>Din tilkoblings ID: @connectionId. Brukernavn: @username</p>
|
|
|
|
@foreach (var msg in msgs)
|
|
{
|
|
<div class="message">@msg</div>
|
|
}
|
|
|
|
<div class="messageFormContainer">
|
|
@if (messageModel == null)
|
|
{
|
|
<p>laster inn meldingsboks</p>
|
|
}
|
|
else
|
|
{
|
|
<EditForm Model="@messageModel" OnValidSubmit="@HandleSubmit" class="MessageForm">
|
|
<DataAnnotationsValidator />
|
|
<div class="MessageFormValidation">
|
|
<ValidationSummary />
|
|
</div>
|
|
|
|
<div class="MessageFormInputs">
|
|
<input @bind-value="messageModel.Message" placeholder="Skriv inn din melding..." class="MessageFormMessage" />
|
|
<button type="submit" class="MessageFormButton"><i class="bi bi-send-fill"></i></button>
|
|
</div>
|
|
</EditForm>
|
|
}
|
|
</div>
|
|
|
|
@code {
|
|
[Parameter]
|
|
public string? Room { get; set; }
|
|
|
|
private HubConnection? hubConnection;
|
|
|
|
private List<string> msgs = new List<string>();
|
|
|
|
private bool ErViInne = false;
|
|
|
|
private string connectionId = "";
|
|
|
|
private string username = "";
|
|
|
|
private MessageModel messageModel = new MessageModel() { User = "Something", Message = "" };
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
hubConnection = new HubConnectionBuilder()
|
|
.WithUrl(Navigation.ToAbsoluteUri("/chathub"))
|
|
.Build();
|
|
|
|
await hubConnection.StartAsync();
|
|
|
|
// lag tilfeldig navn
|
|
username = "Bruker" + new Random().Next(1, 1000);
|
|
|
|
if (Room != null || Room != "")
|
|
{
|
|
// sjekk om rom finnes
|
|
// hvis ikke, lag et rom
|
|
using (var db = new ChattingContext())
|
|
{
|
|
var room = db.Rooms.FirstOrDefault(r => r.RoomJoinCode == Room);
|
|
if (room is null)
|
|
{
|
|
Console.WriteLine("Lager rom?!?");
|
|
room = new Room() { RoomJoinCode = Room };
|
|
db.Rooms.Add(room);
|
|
db.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// bli med i rom
|
|
await hubConnection.SendAsync("JoinRoom", Room, username);
|
|
ErViInne = true;
|
|
messageModel.User = username;
|
|
|
|
hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
|
|
{
|
|
var encodedMsg = $"{user}: {message}";
|
|
msgs.Add(encodedMsg);
|
|
InvokeAsync(StateHasChanged);
|
|
});
|
|
}
|
|
|
|
private async Task HandleSubmit()
|
|
{
|
|
messageModel.User = username;
|
|
await hubConnection.SendAsync("SendMessageRoom", messageModel.User, messageModel.Message, Room);
|
|
}
|
|
|
|
public bool IsConnected =>
|
|
hubConnection?.State == HubConnectionState.Connected;
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (hubConnection is not null)
|
|
{
|
|
await hubConnection.DisposeAsync();
|
|
}
|
|
}
|
|
}
|