1
votes

I was initially trying to setup my server-side signalR HUB to send messages to the client, but so far have not succeeded.

So I decided to try to send a message from the client instead; and setup a button to trigger a message from client to server.

I can launch my Core 3.1 project (image below), and setup the Hub connection just fine, but cannot verify in any way that the message is being received on the server. In fact, my server breakpoints never get hit.

l

enter image description here In my html:

<button mat-button (click)="sendClientMessage()"> Send Message </button>

TypeScript component:

 sendClientMessage(): void {
    this.notificationService.sendMessageToHub();
}

import { Injectable } from '@angular/core';
import * as signalr from '@microsoft/signalr';
import { SIGCONT } from 'constants';

@Injectable({
  providedIn: 'root',
})
export class NotificationService {

  private hubConnection: signalr.HubConnection;
  hubMessage: string;

  public startConnection = () => {
    this.hubConnection = new signalr.HubConnectionBuilder()
                            .withUrl('https://localhost:44311/hub')
                            .configureLogging(signalr.LogLevel.Debug)
                            .build();

    this.hubConnection
      .start()
      .then(() =>  {
        console.log('Hub Connection started');
        this.sendMessageToHub();
      })
      .catch((err) => console.log(`Error while starting connection: ${err}`));
    this.hubConnection.serverTimeoutInMilliseconds = 50000;
  }

  public hubListener = () => {
    this.hubConnection.on('messageReceived', (message) => {
      this.hubMessage = message;
      console.log(message);
    });
  }

  public sendMessageToHub = () => {
    if (this.hubConnection == undefined || this.hubConnection.state === signalr.HubConnectionState.Disconnected) {
      this.startConnection();
    } else {
      this.hubConnection.send('NewMessage', 'client', 'You have a notification from the front end !')
      .then(() => console.log('Message sent from client.'));
    }
  }
  constructor() { }
}

My server-side Core project - Notifications.cs

using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NotificationHub.Hubs
{
    public class Notifications: Hub
    {
        public async Task NewMessage(long username, string message)
        {
            await Clients.All.SendAsync("messageReceived", username, message);
        }

        internal Task NewMessage(string v1, string v2)
        {
            throw new NotImplementedException();
        }
    }
}

When I click the button above, it appears to send something to the server:

enter image description here

I would appreciate any help in getting my Core project to first hit those breakpoints, and to see what the NewMessage method is not receiving the client message.

From there I can try and figure out how to send messages from server to client (i.e. using some Timer example).

thank you.

1

1 Answers

0
votes

You're sending 2 strings from the client to the server but you've made your server method take a long and a string so it doesn't match. If you looked at the server logs you would see a message about the method no being found.

Another way to observe the error would be to call invoke instead of send from the client side which will expect a response from the server on completion of the hub method, or in this case an error will be sent from the server.