Signalr 用Typescript实现signar客户端回调函数
我想让信号器和打字脚本一起工作 我有以下文件:Signalr 用Typescript实现signar客户端回调函数,signalr,typescript,Signalr,Typescript,我想让信号器和打字脚本一起工作 我有以下文件: /// <reference path="../../vendor/jquery/jquery.d.ts" /> interface SignalR { roleHub: IRoleHub; } interface IRoleHub { server: IRoleHubServer; client: IRoleHubClient; } interface IRoleHubServer { getU
/// <reference path="../../vendor/jquery/jquery.d.ts" />
interface SignalR {
roleHub: IRoleHub;
}
interface IRoleHub {
server: IRoleHubServer;
client: IRoleHubClient;
}
interface IRoleHubServer {
getUserRoles();
}
interface IRoleHubClient {
populateUserRoles(names: string[]) : IRoleHubClient;
}
我在另一个文件中调用getUserRoles,并且正在调用我的服务器端函数,但是我正在努力实现populateUserRoles客户端回调
在Javascript中,我会执行以下操作:
connection.client.populateUserRoles = function (roles) {
[...]
}
但是,上面的代码生成了以下输出,这是不正确的:
Controller.prototype.populateUserRoles = function (roles) {
console.log('populated');
};
我将感谢您对实现此功能的任何帮助。以下是一种可行的方法:
constructor(role: Portal.App.Model.IRole) {
this.role = role;
this.server = $.connection.roleHub.server;
this.client = $.connection.roleHub.client;
this.client.populateUserRoles = (roles)=> this.roles(roles);
}
非常感谢,它起作用了。然而,我想知道是否有更好的方法。我只是在想,在我必须在构造函数中实现很多客户端函数之后,它可能会变得有点混乱。您可以使用u.extend将类的成员添加到中心…但这会导致类型不安全。
constructor(role: Portal.App.Model.IRole) {
this.role = role;
this.server = $.connection.roleHub.server;
this.client = $.connection.roleHub.client;
this.client.populateUserRoles = (roles)=> this.roles(roles);
}