Skip to content

Commit

Permalink
Change to use Unity.WebRTC for multiplayer transport
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyohome committed Jul 9, 2023
1 parent 9ff5cd4 commit e587e9f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
16 changes: 8 additions & 8 deletions Assets/Extreal/NGO/WebRTC/NativeWebRtcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ private void HandleDc(string id, bool isOffer, RTCDataChannel dc)
}
events.Enqueue(new WebRtcEvent(NetworkEvent.Connect, clientId));
};
dc.OnClose = () =>
{
if (Logger.IsDebug())
{
Logger.LogDebug($"{nameof(dc.OnClose)}: clientId={clientId}");
}
events.Enqueue(new WebRtcEvent(NetworkEvent.Disconnect, clientId));
};
}

// Both Host and Client
Expand All @@ -89,6 +81,14 @@ private void HandleDc(string id, bool isOffer, RTCDataChannel dc)
}
events.Enqueue(new WebRtcEvent(NetworkEvent.Data, clientId, ToByte(message)));
};
dc.OnClose = () =>
{
if (Logger.IsDebug())
{
Logger.LogDebug($"{nameof(dc.OnClose)}: clientId={clientId}");
}
events.Enqueue(new WebRtcEvent(NetworkEvent.Disconnect, clientId));
};
}

private static byte[] ToByte(byte[] message)
Expand Down
25 changes: 23 additions & 2 deletions Assets/Extreal/NGO/WebRTC/WebRtcTransport.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using Extreal.Core.Logging;
using Unity.Netcode;
using UnityEngine;

namespace Extreal.NGO.WebRTC.Dev
{
public class WebRtcTransport : NetworkTransport
{
private static readonly ELogger Logger = LoggingManager.GetLogger(nameof(WebRtcTransport));

private WebRtcClient webRtcClient;

public void SetWebRtcClient(WebRtcClient webRtcClient) => this.webRtcClient = webRtcClient;
Expand Down Expand Up @@ -34,13 +37,31 @@ private bool StartAs(WebRtcRole role)

public override void DisconnectRemoteClient(ulong clientId)
{
if (Logger.IsDebug())
{
Logger.LogDebug($"{nameof(DisconnectRemoteClient)}: clientId={clientId}");
}
}

public override void DisconnectLocalClient() => webRtcClient.Disconnect();
public override void DisconnectLocalClient()
{
if (Logger.IsDebug())
{
Logger.LogDebug($"{nameof(DisconnectLocalClient)}");
}
webRtcClient.Disconnect();
}

public override ulong GetCurrentRtt(ulong clientId) => 100;

public override void Shutdown() => webRtcClient.Shutdown();
public override void Shutdown()
{
if (Logger.IsDebug())
{
Logger.LogDebug($"{nameof(Shutdown)}");
}
webRtcClient.Shutdown();
}

public override void Initialize(NetworkManager networkManager = null)
{
Expand Down

0 comments on commit e587e9f

Please sign in to comment.