SignalR:连接断开,出现错误“ ReferenceError:未定义connectionId”

时间:2019-01-10 17:34:13

标签: signalr.client asp.net-core-signalr

我正在使用SignalR库创建聊天应用程序,在与服务器/集线器连接后,我发送用户名通知所有用户,然后发送另一条消息告诉服务器向我发送谁在线。与服务器建立连接后,我就执行此操作。但是在同时发送两个请求/消息时,连接断开并显示以下错误。

Connection disconnected with error 'ReferenceError: connectionId is not defined'

但是,如果我发送一个请求,那么它可以正常工作。为什么会这样呢? 在客户端,我正在使用Vuejs。

客户

ConnectWithServer(){
  // create connection withe hub
}

this.ConnectWithServer((ok) => {
     if (ok === true) {
      var state = this.Connection.connectionState;

      if (state === 1) {

          // First request
          this.SendJoinMessage((status) => {
               if (status === true) {
                  // do stuff
               }
          });

          //Second request
          this.SendMessageToGetConnectedCliets();

      }
    }
});

SendJoinMessage(callBack) {

  this.Connection.invoke("JoinMessage", this.ConnectionId, this.Name).then(() => {
            callBack(true);
        }).catch(err => {
            callBack(false);
        });
    },

SendMessageToGetConnectedCliets() {

        this.Connection.invoke("GetConnectedClients").then(() => {
            // do stuff
        }).catch(err => {
            console.error(err.toString());
        });
 },

服务器:

 public class Messenger : Hub {

     // First message will be appear here 
     public dynamic JoinMessage(string connectionId, string name) {
        Task<dynamic> result = Task.Run(() => new ClientJoin(this.Clients, this.Context).SendJoinMessageToAll(name));
        result.Wait();

        return result.Result;
     }

    //Second message will be appear here 
    public dynamic GetConnectedClients()  {
        Task<dynamic> result = Task.Run(() => new ClientJoin(this.Clients, this.Context).GetConnectedClients());
        return result.Result;
    }
 }

 public class ClientJoin {
     public dynamic GetConnectedClients() {
        try {
            string __ClientConnectionId = this.Context.ConnectionId;
            IEnumerable<dynamic> clients = (from x in ConnectedDisconnected.Instance.OnlineClientList.ToList()
                                            select new
                                            {
                                                connectionId = x.Value.ConnectionId,
                                                name = x.Value.Name
                                            });

            Task.Run(() => Clients.Client(__ClientConnectionId).SendAsync("OnConnectedClients", clients));
            return new { success = true };
        }
        catch (Exception) { return new { success = false }; }
    }
 }

0 个答案:

没有答案