How to use SignalR in a 2 player game via WPF clients C#

Viewed 45

First time poster, so please go easy :)

I'm new to web applications. Disclaimer out of the way.

To give me something practical to aim at while learning, I've decided to write a 2 player game. The clients will be in WPF (another new thing to learn) and I plan on having the game logic and data handling handled in a "web application".

I was originally going to use a REST service, but thought that SignalR with it's 2 way communication would be a better fit for a game that has constant player interaction.

I've watched 2 or 3 videos using Blazor server with a WPF client and it all makes sense but the videos I've watched rely on the server app having web pages displayed for them to respond to the broadcast.

As an example, the standard chat type intro project has to have the corresponding chat page on the server application visible (the Index.razor file shown below) in the browser for the chat response to be actioned. This probably could be changed, but I have no idea at this stage how to do that.

For example, here is the code in the Hub on the server side

namespace BlazorServer.Hubs

{
    public class ChatHub :Hub
    {
        public Task SendMessage(string user, string message)
        {
 
            return Clients.All.SendAsync(method:"ReceiveMessage", user, message);
        }
    }
}

..and this is the code that sits in the Index.razor page


@code {
    private HubConnection? hubConnection;
    private List<string> messages = new();
    private string? userInput;
    private string? messageInput;

    protected override async Task OnInitializedAsync()
    {
        hubConnection = new HubConnectionBuilder()
            .WithUrl(Navmanager.ToAbsoluteUri("/chathub"))
            .WithAutomaticReconnect()
            .Build();

        hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
        {
            var formattedMessage = $"{userInput}: {message}";
            messages.Add(formattedMessage);
            InvokeAsync(StateHasChanged);      

        });

        await hubConnection.StartAsync();

    }

    private async Task Send()
    {
        if (hubConnection is not null)
            await hubConnection.SendAsync("SendMessage", userInput, messageInput);
    }

    public bool IsConnected => hubConnection?.State == HubConnectionState.Connected;

    public async ValueTask DisposeAsync()
    {
        if (hubConnection is not null)
        {
            await hubConnection.DisposeAsync();
        }
    }
}

The design I envisage will have no web page elements (it will just listen for messages, execute logic and data routines and broadcast responses.

My question is (remember, I'm new to web applications), is "Is a Blazor server project the appropriate project type for what I'm looking at, or should I be looking at a totally different type of architecture?"

I have looked for similar types of existing questions, but nothing really addresses this basic/fundamental type question.

Any thoughts or pointers gratefully received.

0 Answers
Related