Websocket 等待客户端中的信号器消息

Websocket 等待客户端中的信号器消息,websocket,async-await,signalr,es6-promise,Websocket,Async Await,Signalr,Es6 Promise,signar客户端是否可以向服务器发送消息,然后等待来自服务器的单独消息(不是返回值) 理论 Client1将消息1发送到Server并“等待”响应 服务器处理一些逻辑 Server向Client1发送消息2,然后Client1执行等待的代码 对服务器的调用: $.connection.myhub.server.myRequest(id).done(()==>{ // myRequest is done; // the server has received the request

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

理论

  • Client1
    将消息1发送到
    Server
    并“等待”响应
  • 服务器
    处理一些逻辑
  • Server
    Client1
    发送消息2,然后
    Client1
    执行等待的代码
  • 对服务器的调用:

    $.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,或者以某种方式将其封装在承诺中吗


    如果这在SignalR中不可用,是否还有其他套接字库可以替代使用?

    您可以执行以下操作:

    假设您有一个客户机加入一个组,更新一个表,然后通知客户机它已加入

    客户端

    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将从其等待状态返回,JoinGroup将返回并警告新用户已加入组