SignalR 无法 re-call 启动异步方法

SignalR Cannot re-call start of asynchronous method

我遇到了一个似乎是 async \ await 的问题,但我很难找到它。这就是问题所在,我们正在使用 Xamarin Android 和 SignalR 实现 WebRtc 视频会议系统。

在与 WebRtc 的初始谈判期间,我们收到双方之间的信号以设置 offer \ answerIceCandidates 我们收到并快速处理前三个候选人,但是第四个似乎是 HubConnection 上的错误原因。这里是例外。

System.InvalidOperationException: Cannot re-call start of asynchronous method while a previous call is still in progress.

at System.Net.HttpWebRequest.BeginGetResponse (System.AsyncCallback callback, System.Object state) [0x00000] in :0

现在这是第 4 次收到 IceCandidate(由日志输出给出)

onIceCandidate: {"candidate":{"candidate":"candidate:1532951ed4f3bc58106a9d8d563c6ab7 1 udp 2130706431 192.168.1.118 1104 typ host","sdpMid":"","sdpMLineIndex":0}}
onIceCandidate: {"candidate":{"candidate":"candidate:1532951ed4f3bc58106a9d8d563c6ab7 1 udp 2130706431 192.168.1.118 34462 typ host","sdpMid":"","sdpMLineIndex":1}}
onIceCandidate: {"candidate":{"candidate":"candidate:94378da69e185ca5371cdd7e0b75d8cf 1 udp 1694498815 59.167.62.173 1104 typ srflx raddr 192.168.1.118 rport 1104","sdpMid":"","sdpMLineIndex":0}}
onIceCandidate: {"candidate":{"candidate":"candidate:94378da69e185ca5371cdd7e0b75d8cf 1 udp 1694498815 59.167.62.173 34462 typ srflx raddr 192.168.1.118 rport 34462","sdpMid":"","sdpMLineIndex":1}}

现在调用方法如下所示:

/// <summary>
/// handles the on ice candidates event
/// </summary>
/// <param name="args"></param>
async void onIceCandidate(LinkCandidateArgs args)
{
    var rootJObject = new JObject();
    var candidateObject = new JObject();

    var cadString = args.Candidate.SdpCandidateAttribute;
    if (cadString.StartsWith("a="))
        cadString = cadString.Substring("a=".Length);

    candidateObject.Add("candidate", cadString);
    candidateObject.Add("sdpMid", "");
    candidateObject.Add("sdpMLineIndex", args.Candidate.SdpMediaIndex);

    rootJObject.Add("candidate", candidateObject);

    var payload = rootJObject.ToString(Newtonsoft.Json.Formatting.None);

    Log.Debug(App.TAG, "onIceCandidate: " + payload);
    await this.sendSignal(args.PeerId, payload);
}

/// <summary>
/// sends a signal back to the hub client
/// </summary>
/// <param name="connectionId"></param>
/// <param name="payload"></param>
/// <returns></returns>
private async Task sendSignal(string connectionId, string payload)
{
    await this.proxy.Invoke("SendSignal", payload, connectionId);
}

现在,方法 onIceCandidate(LinkCandidateArgs args) 是一个事件处理程序,每次将 IceCandidate 添加到 link 时都会触发该事件处理程序。

不久之后我确实解决了这个问题,并认为最好 post 一个答案。这里的问题来自两个领域。首先,在查看了一些 SignalR 帮助指南后,我发现了客户端 属性 的配置

ServicePointManager.DefaultConnectionLimit = 10;

http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-net-client#maxconnections

指南指出:

In WPF clients, you might have to increase the maximum number of concurrent connections from its default value of 2. The recommended value is 10.

现在我不是 100% 确定 Xamarin Android 限制是什么,但这似乎有助于解决问题。

其次,我从方法 onIceCandidatesendSignal 中删除了 async \ await 看起来好像等待者导致底层 HttpWebRequest 并抛出错误。现在我仍然不是 100% 确定为什么会发生这种情况,但是我不再收到 HubConnection.

上的错误

希望这对以后的人有所帮助。