From 312b59117951f1c04fce5c53d3d4a2b9cddb5a5e Mon Sep 17 00:00:00 2001 From: Lital Natan Date: Tue, 7 Jan 2020 23:53:53 +0200 Subject: [PATCH 1/2] Allow disconnect functionality Signed-off-by: Lital Natan --- Assets/Plugins/socket.io/Socket.cs | 13 ++++++++++++- Assets/Plugins/socket.io/WebSocketTrigger.cs | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Assets/Plugins/socket.io/Socket.cs b/Assets/Plugins/socket.io/Socket.cs index 4c4b370..840c223 100644 --- a/Assets/Plugins/socket.io/Socket.cs +++ b/Assets/Plugins/socket.io/Socket.cs @@ -9,6 +9,8 @@ namespace socket.io { /// public class Socket : MonoBehaviour { + public bool AllowReconnection { get; private set; } + /// /// Establishes a connection to a given url /// @@ -18,7 +20,7 @@ public static Socket Connect(string url) { var socket = new GameObject(string.Format("socket.io - {0}", url)).AddComponent(); socket.transform.SetParent(SocketManager.Instance.transform, false); socket.Url = new Uri(url); - + socket.AllowReconnection = true; SocketManager.Instance.Connect(socket); return socket; } @@ -274,6 +276,15 @@ public bool IsConnected { } } + public void Disconnect() + { + AllowReconnection = false; + if (IsConnected) + { + WebSocketTrigger.WebSocket.Close(); + } + } + protected WebSocketTrigger WebSocketTrigger { get { if (_webSocketTrigger == null && transform.parent != null) diff --git a/Assets/Plugins/socket.io/WebSocketTrigger.cs b/Assets/Plugins/socket.io/WebSocketTrigger.cs index 5a44f58..ac4f4b9 100644 --- a/Assets/Plugins/socket.io/WebSocketTrigger.cs +++ b/Assets/Plugins/socket.io/WebSocketTrigger.cs @@ -123,7 +123,8 @@ void CheckAndHandleWebSocketDisconnect() { if (SocketManager.Instance.Reconnection) { var sockets = gameObject.GetComponentsInChildren(); foreach (var s in sockets) - SocketManager.Instance.Reconnect(s, 1); + if (s.AllowReconnection) + SocketManager.Instance.Reconnect(s, 1); } } From addf52edd99b49ef9bf9ca1ed0dcb5cb3b4db2a2 Mon Sep 17 00:00:00 2001 From: Lital Natan Date: Fri, 6 Dec 2024 13:04:06 +0200 Subject: [PATCH 2/2] Support receiving events with not data --- Assets/Plugins/socket.io/Socket.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Assets/Plugins/socket.io/Socket.cs b/Assets/Plugins/socket.io/Socket.cs index 840c223..0092e47 100644 --- a/Assets/Plugins/socket.io/Socket.cs +++ b/Assets/Plugins/socket.io/Socket.cs @@ -335,13 +335,23 @@ void DispatchPacket(Packet pkt) { seperatorLen = 1; } + var hasEventData = seperateIndex > -1; + + if (!hasEventData) // Event without a payload? + { + seperateIndex = pkt.body.Length - 1; + } var eventName = pkt.body.Substring(2, seperateIndex - 3); if (!_handlers.ContainsKey(eventName)) { Debug.LogWarningFormat("{0} event doesn't have a handler", eventName); break; } - var data = pkt.body.Substring(seperateIndex + seperatorLen, pkt.body.Length - seperateIndex - seperatorLen - 1); + var data = hasEventData + ? pkt.body.Substring(seperateIndex + seperatorLen, + pkt.body.Length - seperateIndex - seperatorLen - 1) + : null; + _handlers[eventName](data); break;