在客户端等待信号器消息

问题描述 投票:0回答:1

SignalR客户端是否有可能向服务器发送消息,然后等待来自服务器的单独消息(而不是返回值)?

理论;

  1. Client1发送消息1给Server并“等待”回复。
  2. Server处理一些逻辑
  3. Server将message2发送到Client1Client1执行等待代码。

致电服务器:

$.connection.myhub.server.myRequest(id).done(()==>{
  // myRequest is done;
  // the server has received the request but hasn't processed it yet.
  // I want put some *async* code here to do something when the server has triggered $.connection.myhub.client.myResponse(id, someParam);
});

回调给客户:

$.connection.myhub.client.myResponse(originalId, somePassedBackValue);

我可以使用Async / Await,或以某种方式将其包装在Promise中吗?

如果在SignalR中无法实现这一点,是否还有其他套接字库可以使用?

websocket async-await signalr es6-promise
1个回答
1
投票

您可以执行以下操作:

想象一下,您有一个加入组的客户端,更新表,然后通知客户端它已加入。

客户

msgHub.server.joinGroup(id).done(function () {
    console.log("Joined Group");
    serverCallFinished = true;
})

msgHub.client.displayTable = function (table) {
    display(table);
}

public async Task JoinGroup(string practiceId)
{
    try
    {
        await Groups.Add(Context.ConnectionId, practiceId);
        //Add new table
        var table = new Table(practiceId)
        await UpdateTable("SomeGroup", table);
    }
    catch (Exception e)
    {
        throw;
    }
}

public async Task UpdateInfo(string groupName, string table)
    {
        //await some logic
        Clients.Group(groupName).updateTable(table);
    }

更新信息将使用message2调用客户端,在这种情况下是一个要显示给客户端的表。当它完成时,它将从其等待的状态返回JoinGroup,它将返回并警告新用户已加入组。

© www.soinside.com 2019 - 2024. All rights reserved.