Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions Assets/Plugins/socket.io/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace socket.io {
/// </summary>
public class Socket : MonoBehaviour {

public bool AllowReconnection { get; private set; }

/// <summary>
/// Establishes a connection to a given url
/// </summary>
Expand All @@ -18,7 +20,7 @@ public static Socket Connect(string url) {
var socket = new GameObject(string.Format("socket.io - {0}", url)).AddComponent<Socket>();
socket.transform.SetParent(SocketManager.Instance.transform, false);
socket.Url = new Uri(url);

socket.AllowReconnection = true;
SocketManager.Instance.Connect(socket);
return socket;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -324,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;

Expand Down
3 changes: 2 additions & 1 deletion Assets/Plugins/socket.io/WebSocketTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ void CheckAndHandleWebSocketDisconnect() {
if (SocketManager.Instance.Reconnection) {
var sockets = gameObject.GetComponentsInChildren<Socket>();
foreach (var s in sockets)
SocketManager.Instance.Reconnect(s, 1);
if (s.AllowReconnection)
SocketManager.Instance.Reconnect(s, 1);
}
}

Expand Down