【发布时间】:2019-09-02 15:41:00
【问题描述】:
我正在打开从我的 Typescript 客户端到我的 C# SignalR Hub 的连接。似乎没有办法添加上下文项。
客户:
public startConnection(hubRoute: string): void {
const fixedRoute = `${this._baseUrl}${hubRoute}`;
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl(fixedRoute)
.build();
this.hubConnection
.start()
.then(() => console.log('Connection started'))
.catch(err => console.log('Error while starting connection: ' + err));
/// where and how to add context items here?
}
服务器:
public class EditorialHub: Hub<IEditorialHub>
{
public override async Task OnConnectedAsync()
{
if (Context.Items["IssueNumber"] is string issueNumber)
{
await Groups.AddToGroupAsync(Context.ConnectionId, "IssueNumber_" + issueNumber);
}
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
if (Context.Items["IssueNumber"] is string issueNumber)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "IssueNumber_" + issueNumber);
}
await base.OnDisconnectedAsync(exception);
}
}
我可以从客户端添加“IssueNumber”Context.Item 吗?
【问题讨论】:
-
这里的上下文是什么?你从哪里得到的?
-
上下文是基类集线器的属性。
-
所以你想让客户端发送上下文对象给信号器?
-
正确。我找不到从客户端填充 Context.Items 的方法。
-
我认为 Signalr 客户端不能这样做,因为那是服务器端对象
标签: c# typescript asp.net-core signalr asp.net-core-signalr