Skip to content

Commit 56ca6c5

Browse files
committed
Fixed echo test in webrtccmdline.
1 parent 0ab58b4 commit 56ca6c5

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

examples/webrtccmdline/EchoServer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ private async static Task Offer(IHttpContext context, int pcTimeout)
156156
pc.close();
157157
}
158158
}, null, pcTimeout * 1000, Timeout.Infinite);
159-
pc.OnClosed += timeout.Dispose;
159+
160+
pc.OnClosed += () =>
161+
{
162+
timeout.Dispose();
163+
logger.LogInformation("Echo test completed successfully.");
164+
};
160165
}
161166
}
162167
}
@@ -244,6 +249,7 @@ public async Task<RTCPeerConnection> GotOffer(RTCSessionDescriptionInit offer)
244249
{
245250
logger.LogInformation($"Data channel got message: {Encoding.UTF8.GetString(data)}");
246251
rdc.send(Encoding.UTF8.GetString(data));
252+
pc.Close("test successfully completed");
247253
};
248254
};
249255

examples/webrtccmdline/Program.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,41 @@ private static async Task RunCommand(Options options, bool noOptions)
358358
{
359359
Console.WriteLine($"Set remote description failed {setAnswerResult}.");
360360
}
361+
else
362+
{
363+
Console.WriteLine("WebRTC echo server connection established.");
364+
365+
var dc = pc.DataChannels.FirstOrDefault();
366+
367+
if (dc != null)
368+
{
369+
var pseudo = Crypto.GetRandomString(5);
370+
371+
dc.onopen += () =>
372+
{
373+
logger.LogInformation($"Data channel {dc.label}, stream ID {dc.id} opened.");
374+
dc.send(pseudo);
375+
};
376+
377+
dc.onmessage += async (dc, proto, data) =>
378+
{
379+
string echoMsg = Encoding.UTF8.GetString(data);
380+
logger.LogDebug($"data channel onmessage {proto}: {echoMsg}.");
381+
382+
if (echoMsg == pseudo)
383+
{
384+
logger.LogInformation($"Data channel echo test success.");
385+
pc.Close("test success");
386+
await Task.Delay(500);
387+
exitCts.Cancel();
388+
}
389+
else
390+
{
391+
logger.LogWarning($"Data channel echo test failed, echoed message of {echoMsg} did not match original of {pseudo}.");
392+
}
393+
};
394+
}
395+
}
361396
}
362397
else
363398
{

0 commit comments

Comments
 (0)