after invoke i receive signal once, reactjs signalR .net

Viewed 63

i wanna recieve signal after every update in driverReq

react js

i invoke SendRequest and send data model after connected to socket, receive signal after invoke in connection.on("broadcastTripList") just once.

 try {
      await connection
        .invoke("SendRequest", { locationModel: data })
        .then(console.log("done"));
    } catch (err) {
      console.log(err.message);
    }
  };
  //

  // negotiates with server and starts the connection
  const startConnection = () => {
    const newConnection = new HubConnectionBuilder()
      .withUrl("https://localhost:5001/TripList")
      .withAutomaticReconnect()
      .configureLogging(LogLevel.Information)
      .build();
    setConnection(newConnection);
  };
  //
  useEffect(() => {
    // if the socket is connected starts listening for signals
    if (connection) {
      connection
        .start()
        .then((result) => {
          connection.on("broadcastTripList", (signal) => {
            console.log(signal, "signal");
          });
        })
        .catch((e) => console.log("Connection failed: ", e));
    }
  }, [connection]);

.net

after client invoke SendRequest and send model, res for that model will send on "broadcastTripList".

namespace Application.Hubs
{
    public class TripListHub :  Hub
    {
 

       
        private readonly IMediator _mediator;

        public TripListHub(IMediator mediator)
        {
            _mediator = mediator;
        }

      
        public async Task SendRequest(DriverReqRequest driverReq)
        {

            var res =_mediator.Send(driverReq);
  
            await Clients.Client(Context.ConnectionId).SendAsync("broadcastTripList", res);

        }
    }
}
0 Answers
Related