Azure-Samples / signalr-service-quickstart-serverless-chat

Serverless chat quickstart samples for Azure SignalR
MIT License
75 stars 62 forks source link

Having trouble getting the sample to work with a .NET Client #8

Closed juliusl closed 5 years ago

juliusl commented 5 years ago

I feel like I'm missing something really obvious. I have a .NET client using Microsoft.AspNetCore.SignalR.Client (1.1.0)

My client code looks like this:

using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            HubConnection connection;

            connection = new HubConnectionBuilder()
                .WithUrl("http://localhost:7071/api")
                .Build();

            connection.Closed += async (error) =>
            {
                await Task.Delay(new Random().Next(0, 5) * 1000);
                await connection.StartAsync();
            };

            connection.On<string, string>("newMessage", (sender, text) =>
            {
                Console.WriteLine($"{sender} {text}");
            });

            Task.Run(async () => {
                await connection.StartAsync();
                Console.WriteLine($"Connected {connection.State}");
            }).Wait();

            Console.ReadLine();
        }
    }
}

I'm using the C# version of the function code. They seem to be working because when I debug locally they are being hit and I'm seeing the connection is established from the portal. When I send a message from Postman I get a 204 and I can see it being processed on the function side by debugging locally.

1) I can't see any new messages hitting newMessage when I debug my client. 2) I don't see any messages sent in the portal with the graph.

Also, what's the point of the HubName for functions? I can't figure how that's used with respect to the .NET client. i.e.

[SignalRConnectionInfo(HubName = "chat")] SignalRConnectionInfo connectionInfo
[SignalR(HubName = "chat")] IAsyncCollector<SignalRMessage> signalRMessages
juliusl commented 5 years ago

Ok I figured it out, it needs to be:

            connection.On<object>("newMessage", (dynamic message) =>
            {
                Console.WriteLine($"{ message.sender} : {message.message}");
            });

RE: those attributes are the bindings, but the confusing part is that in the generated function.json it doesn't list those bindings, but things appear to just work still.