简体   繁体   中英

SignalR does not invoke server hub method when calling function outside of .done

I have been trying to implement signalR into my game to handle updating player position real time, and broadcasting that to all connected clients.

However, my client function which is invoking the server hub method does not ever reach the server, unless I call it directly after $.connection.hub.start().done(). Let me give you an example...

This calls the server hub method UpdatePlayerPosition correctly, but I don't want to call this method directly on .done:

$.connection.hub.start().done(function () {
    $.connection.playerHub.server.updatePlayerPosition(playerObj)
});

However, if I create a variable on my main gameEngine object and call it later, IE each time a player moves, nothing happens.

$.connection.hub.start().done(function () {
        gameEngine.updatePlayerPosition = function () {
            $.connection.playerHub.server.updatePlayerPosition(playerObj) 
            //playerObj is a global variable
        }
});

if (player.isMoving) {
   gameEngine.updatePlayerPosition();
}

Calling gameEngine.updatePlayerPosition() does hit the correct function, and goes through all of the signalr.js, but the breakpoint on the server is not hit.

Here is the hub code (I have not added the functionality for updating the player on the hub yet, I just want to get the communication going first)

using Microsoft.AspNet.SignalR;
using RPGGameCS.Models.Player;

namespace RPGGameCS
{
    public class PlayerHub : Hub
    {
        public void UpdatePlayerPosition(IPlayer player)
        {
            Clients.All.updatePlayerPosition(player);
        }
    }

}

All the examples of signalR I've seen bind these server calls to click events, which I don't want to do. It seems like this should be possible, but nothing I've tried thus far has worked. Is there something I'm doing wrong? Or maybe it's not possible to delegate singalR functions in this way?

EDIT 1***: So it appears it has something to do with the player object I'm sending to the hub method. When I make the player object null, the communication appears to work fine. But when I send the actual player object, the break point is never hit.

Method signature must match.

In your hub you have a parameter IPlayer and on client you do not set this parameter. gameEngine.updatePlayerPosition(); does not matcht with public void UpdatePlayerPosition(IPlayer player)

If you call for example gameEngine.updatePlayerPosition(); signalr searchs for a method on the connected hub according name and parameters. In the case it will not find a method I think you should find this in the log.

Enable log for testing:

$.connection.hub.logging = true;
$.connection.hub.start();

( https://docs.microsoft.com/en-us/aspnet/signalr/overview/testing-and-debugging/enabling-signalr-tracing )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM