【发布时间】: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