Reputation: 1249
I am learning SignalR but have hit a road block.
I have an Azure function which is successfully posting to Azure SignalR Hosted Service (configured in Serverless Mode)
I've been following this quickstart:
Quickstart: Create a chat room with Azure Functions and SignalR Service using C#
What I would like to achieve is to essentially receive messages from Servers into my client application. To prototype this I have created a Console Application.
I have added the following Nuget Packages
Microsoft.AspNetCore.SignalR.Client -Version 1.1.0 Microsoft.Azure.WebJobs.Extensions.SignalRService
All of the infrastructure seems to be working fine - I am basing that assumption on the face that I can run the demo website at the following address, point it at my local instance (or my instance hosted in Azure) https://azure-samples.github.io/signalr-service-quickstart-serverless-chat/demo/chat-v2/
Messages posted by my AzureFunction publish directly into the Chat Window.
How can I get these messages to print to a console?
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
using System;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadKey();
var connection = new HubConnectionBuilder().WithUrl("http://localhost:7071/api").Build();
connection.On<SignalRMessage>("newMessage", (message) =>
{
Console.WriteLine(message.Arguments);
});
connection.On("newMessage", (string server, string message) =>
{
Console.WriteLine($"Message from server {server}: {message}");
}
);
await connection.StartAsync();
Console.ReadKey();
}
}
}
I strongly suspect my problem is related the
connection.On<...>
statements. They never fire. The Connection.StartAsync() seems to work fine and establishes a connection to Azure SignalR instance.
Am I missing some fundamental point? I am just thrashing about at this point.
In Short - can someone please give me a pointer to RECEIVING and WRITING messages to my console window - much in the same way as messages are printed to the web browser in the web chat demo (see second link above).
The messages are simple broadcast messages that I want to go to all connected clients.
Nearly all examples are in Javascript.
Thanks in Advance.
Upvotes: 5
Views: 5331
Reputation: 48
I managed to get around it by passing in 'object' when calling 'connection.On' instead of having to create the CorrectType class and let .net figure out of how the object is going to look like.
It was indeed the resolving of the type that prevented the .On from firing on a windows client.
Upvotes: 1
Reputation: 1249
Once I discovered how to add logging to SignalR I could see that it could not resolve the type which was being sent.
It worked once I changed my connection.On to the correct type such as
connection.On<CorrectType>("newMessage", (message) =>
{
Console.WriteLine(message.stringproperty);
});
My thinking was mislead by looking at the article Azure Functions development and configuration with Azure SignalR Service
Where they "seemingly" (atleast in my mind) add a message of type "SignalRMessage" to SignalR. When in fact they were adding a message type of "CorrectType"
CorrectType message
signalRMessages.AddAsync(
new SignalRMessage
{
// the message will only be sent to these user IDs
UserId = "userId1",
Target = "newMessage",
Arguments = new [] { message }
});
Upvotes: 4