fixed port conflicts

This commit is contained in:
tom.hempel
2025-09-21 23:54:42 +02:00
parent 4fbc109b89
commit e5580ac741
6 changed files with 54 additions and 10 deletions

View File

@ -66,7 +66,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
var cfg = networkConfigAsset != null ? networkConfigAsset : NetworkConfig.Instance;
if (cfg != null)
{
listenPort = cfg.port;
listenPort = cfg.multiplayerAudioPort;
}
}
InitializeNetwork();
@ -79,6 +79,16 @@ namespace Convai.Scripts.Runtime.Multiplayer
}
}
private void OnEnable()
{
// When re-enabled, ensure listener is running
if (_cancellationTokenSource == null)
{
_cancellationTokenSource = new CancellationTokenSource();
}
StartListening();
}
private void OnDestroy()
{
// Unsubscribe from events
@ -92,6 +102,12 @@ namespace Convai.Scripts.Runtime.Multiplayer
_cancellationTokenSource?.Dispose();
}
private void OnDisable()
{
// Free the UDP port when this NPC gets disabled
StopListening();
}
private void Update()
{
// Auto-stop listening if no packets received for a while
@ -115,8 +131,13 @@ namespace Convai.Scripts.Runtime.Multiplayer
private void InitializeConvai()
{
// Get target NPC
if (useActiveNPC)
// Prefer local ConvaiNPC on the same GameObject, then fall back to active NPC
var localNPC = GetComponent<ConvaiNPC>();
if (localNPC != null)
{
targetNPC = localNPC;
}
else if (useActiveNPC)
{
targetNPC = ConvaiNPCManager.Instance?.GetActiveConvaiNPC();
}

View File

@ -56,7 +56,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
if (cfg != null)
{
targetIP = cfg.ipAddress;
targetPort = cfg.port;
targetPort = cfg.multiplayerAudioPort;
}
}
InitializeNetwork();

View File

@ -113,13 +113,22 @@ namespace Convai.Scripts.Runtime.Multiplayer
var cfg = networkConfigAsset != null ? networkConfigAsset : NetworkConfig.Instance;
if (cfg != null)
{
listenPort = cfg.port;
listenPort = cfg.multiplayerSpeechPort;
}
}
InitializeAudio();
InitializeNetwork();
}
private void OnEnable()
{
if (_cancellationTokenSource == null)
{
_cancellationTokenSource = new CancellationTokenSource();
}
StartListening();
}
private void OnDestroy()
{
StopListening();
@ -127,6 +136,11 @@ namespace Convai.Scripts.Runtime.Multiplayer
_cancellationTokenSource?.Dispose();
}
private void OnDisable()
{
StopListening();
}
private void Update()
{
// Process playback queue

View File

@ -62,7 +62,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
if (cfg != null)
{
targetIP = cfg.ipAddress;
targetPort = cfg.port;
targetPort = cfg.multiplayerSpeechPort;
}
}
InitializeNetwork();
@ -93,8 +93,13 @@ namespace Convai.Scripts.Runtime.Multiplayer
private void InitializeConvai()
{
// Get target NPC
if (useActiveNPC)
// Prefer local ConvaiNPC on the same GameObject, then fall back to active NPC
var localNPC = GetComponent<ConvaiNPC>();
if (localNPC != null)
{
sourceNPC = localNPC;
}
else if (useActiveNPC)
{
sourceNPC = ConvaiNPCManager.Instance?.GetActiveConvaiNPC();
}

Binary file not shown.

View File

@ -7,6 +7,10 @@ public class NetworkConfig : ScriptableObject
public string ipAddress = "127.0.0.1";
public int port = 8080;
[Header("Multiplayer Ports")]
public int multiplayerAudioPort = 12345; // For ConvaiSimpleUDPAudio (send/receive)
public int multiplayerSpeechPort = 12346; // For ConvaiUDPSpeech (send/receive)
private static NetworkConfig _instance;
public static NetworkConfig Instance
{