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類中。