【问题标题】:SignalR Client method bound before hub.start(), but not being called after Server MethodSignalR 客户端方法在 hub.start() 之前绑定,但在服务器方法之后没有被调用
【发布时间】:2016-04-30 07:25:01
【问题描述】:

我可以在我的服务器方法中设置一个断点,并在集线器启动时调用它。如果我在 hub.start() 处设置断点,我确实看到连接已经绑定了客户端版本的方法。但不知何故,该方法没有从服务器调用。这是我的代码:

服务器方法

 [HubName("MovementHub")]
public class MovementHub : Hub
{
    public void UpdatePlayerPosServer(PlayerPosition playerPosition)
    {
        playerPosition.LastUpdatedBy = Context.ConnectionId;
        Clients.AllExcept(playerPosition.LastUpdatedBy).updatePlayerPosClient(playerPosition); //debugging here shows the playerposition all filled out nicely. this hub method is HIT.
    }
}

客户端方法

$(() => {

var connection = (<any>$.connection).MovementHub; 

    //this method is never called
   connection.client.updatePlayerPosClient = (playerPosModel) => {
        alert("updatingremotePlayers: " + playerPosModel);            
    }

});

Hub Start(打字稿类。方法从另一个类调用)

public updateServerPos = (x: number, y: number) => {
        var connection = (<any>$.connection).MovementHub;         
        this.LoginID = $("#displayname").val();

        $.connection.hub.start().done(() => {
            var playerposModel = { Id: this.LoginID, X: x, Y: y };
            connection.server.updatePlayerPosServer(playerposModel); //debugging here shows me that "connection" has the client method bound at this point
        }).fail(function(error) {
            console.log(error);
        });
    }

我已经阅读了一些关于此的帖子,其中指定您必须在集线器启动之前绑定客户端方法,但它是。并且服务器方法被正确调用。所以不确定这里发生了什么。

编辑:我意识到,我是个白痴,可能会被客户的“AllExcept”调用跳过而成为受害者。我是个例外!哈哈

剩下的唯一问题是为什么我必须在 IFFE 中“实例化”客户端方法?我想把它放在调用服务器方法的同一个 Typescript 类中。

【问题讨论】:

    标签: signalr


    【解决方案1】:

    事实证明,将 javascript IIFE 调用与 typescript 调用混合使用可能是危险的。在绑定此客户端方法之前,我有一个完全不相关的(我认为)集线器开始发生。我意识到,即便如此,我有两个 Hub,实际上只有一个 hub.start();傻我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      相关资源
      最近更新 更多