From a3c6c2d8b422379db632e951921454cc47897ba6 Mon Sep 17 00:00:00 2001 From: Yan Jin <601746725@qq.com> Date: Fri, 15 Dec 2023 15:22:32 -0800 Subject: [PATCH] Fixed a bug not able to reconnect --- Assets/Inworld/Inworld.AI/Scripts/InworldWebSocketClient.cs | 6 ++++-- .../Runtime/Implementation/NoWebGL/WebSocketManager.cs | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Assets/Inworld/Inworld.AI/Scripts/InworldWebSocketClient.cs b/Assets/Inworld/Inworld.AI/Scripts/InworldWebSocketClient.cs index 34654576d..7c4bebde1 100644 --- a/Assets/Inworld/Inworld.AI/Scripts/InworldWebSocketClient.cs +++ b/Assets/Inworld/Inworld.AI/Scripts/InworldWebSocketClient.cs @@ -264,11 +264,13 @@ string _GetSessionFullName(string sceneFullName) protected IEnumerator _StartSession() { string url = m_ServerConfig.SessionURL(m_Token.sessionId); - if (!IsTokenValid || WebSocketManager.Instance.Contains(url)) + if (!IsTokenValid) yield break; yield return new WaitForEndOfFrame(); string[] param = {m_Token.type, m_Token.token}; - m_Socket = new WebSocket(url, param); + m_Socket = WebSocketManager.Instance.GetWebSocket(url); + if (m_Socket == null) + m_Socket = new WebSocket(url, param); m_Socket.OnOpen += OnSocketOpen; m_Socket.OnMessage += OnMessageReceived; m_Socket.OnClose += OnSocketClosed; diff --git a/Assets/Inworld/Inworld.AI/UnityWebSocket/Scripts/Runtime/Implementation/NoWebGL/WebSocketManager.cs b/Assets/Inworld/Inworld.AI/UnityWebSocket/Scripts/Runtime/Implementation/NoWebGL/WebSocketManager.cs index 8da1a5821..636fbae2a 100644 --- a/Assets/Inworld/Inworld.AI/UnityWebSocket/Scripts/Runtime/Implementation/NoWebGL/WebSocketManager.cs +++ b/Assets/Inworld/Inworld.AI/UnityWebSocket/Scripts/Runtime/Implementation/NoWebGL/WebSocketManager.cs @@ -35,6 +35,7 @@ public static void CreateInstance() readonly List sockets = new List(); public bool Contains(string sessionURL) => sockets.Any(s => s.Address == sessionURL); + public WebSocket GetWebSocket(string sessionURL) => sockets.FirstOrDefault(s => s.Address == sessionURL); public void Add(WebSocket socket) { if (!Contains(socket.Address))