0
我可以在我的服务器方法中设置断点并在集线器启动时调用它。如果我在hub.start()中放置了一个断点,我确实看到连接已经有方法绑定的客户端版本。但不知何故该方法不从服务器调用。这里是我的代码:SignalR客户端方法绑定在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);
}
});
中心启动(打字稿类方法是从另一个类调用。)
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类中。