Skip to content

Commit d89e5f9

Browse files
author
David Douglas
committed
Dispose method for WebSocket
1 parent 6527283 commit d89e5f9

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

UnityWebSocket.cs

+8-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public abstract class UnityWebSocket : MonoBehaviour {
1717
protected string WebSocketUri;
1818
protected List<UnityKeyValue> Headers;
1919

20-
private IWebSocket _ws;
20+
protected IWebSocket _ws;
2121

2222
protected bool isAttached = false;
2323

@@ -41,9 +41,6 @@ protected virtual void OnWebSocketOpen (object sender, EventArgs e) {
4141

4242
protected virtual void OnWebSocketClose (object sender, WebSocketCloseEventArgs e) {
4343
Debug.Log ("Web socket closed with reason: " + e.Reason);
44-
if (!e.WasClean) {
45-
DisconnectWebSocket ();
46-
}
4744
DettachHandlers();
4845
}
4946

@@ -75,7 +72,7 @@ public void SendUTF8Text (string text, Action<bool> callback = null) {
7572
SendBytes (data, callback);
7673
}
7774

78-
public void SendInputText (InputField inputField) {
75+
public virtual void SendInputText (InputField inputField) {
7976
SendText (inputField.text);
8077
}
8178

@@ -93,17 +90,19 @@ protected void ConnectWebSocket () {
9390
return;
9491
}
9592

96-
if (_ws == null) {
93+
if (_ws == null || !_ws.IsConfigured()) {
9794
var customHeaders = new List<KeyValuePair<string, string>>();
98-
foreach (UnityKeyValue header in Headers) {
99-
customHeaders.Add(new KeyValuePair<string, string>(header.key, header.value));
95+
if (Headers != null) {
96+
foreach (UnityKeyValue header in Headers) {
97+
customHeaders.Add(new KeyValuePair<string, string>(header.key, header.value));
98+
}
10099
}
101100

102101
Debug.Log ("Create Web Socket: " + WebSocketUri);
103102
#if ENABLE_WINMD_SUPPORT
104103
Debug.Log ("Using UWP Web Socket");
105104
_ws = new WebSocketUWP();
106-
#else
105+
#elif UNITY_EDITOR || ENABLE_MONO
107106
Debug.Log("Using Mono Web Socket");
108107
_ws = new WebSocketMono();
109108
#endif

UnityWebSocketScript.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using UnityEngine;
4-
using WebSocketSharp;
54

65
namespace Unity3dAzure.WebSockets {
76
public sealed class UnityWebSocketScript : UnityWebSocket {

WebSocket/IWebSocket.cs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public interface IWebSocket {
1515
void SendAsync(byte[] data, Action<bool> completed = null);
1616
void SendAsync(string text, Action<bool> completed = null);
1717

18+
bool IsConfigured();
1819
bool IsOpen();
1920
string Url();
2021

WebSocket/WebSocketMono.cs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if UNITY_EDITOR || ENABLE_MONO
12
using System;
23
using System.Collections.Generic;
34
using System.IO;
@@ -45,7 +46,8 @@ public void ConfigureWebSocket(string url, List<KeyValuePair<string, string>> he
4546

4647
public void ConnectAsync() {
4748
if (socket == null) {
48-
throw new Exception("WebSocket needs to be configured!");
49+
Debug.LogError("WebSocket not configured!");
50+
return;
4951
}
5052
AttachHandlers();
5153
socket.ConnectAsync();
@@ -58,6 +60,13 @@ public void CloseAsync() {
5860
socket.CloseAsync();
5961
}
6062

63+
public bool IsConfigured() {
64+
if (socket == null) {
65+
return false;
66+
}
67+
return true;
68+
}
69+
6170
public bool IsOpen() {
6271
if (socket == null || socket.ReadyState != WebSocketState.Open) {
6372
return false;
@@ -101,6 +110,12 @@ private void UnattachHandlers() {
101110
socket.OnClose -= HandleOnClose;
102111
}
103112

113+
private void Dispose() {
114+
((IDisposable)socket).Dispose();
115+
socket = null;
116+
isAttached = false;
117+
}
118+
104119
private void HandleOnError(object sender, WebSocketSharp.ErrorEventArgs e) {
105120
if (OnError != null) {
106121
OnError.Invoke(sender, new WebSocketErrorEventArgs(e.Message));
@@ -125,6 +140,7 @@ private void HandleOnClose(object sender, WebSocketSharp.CloseEventArgs e) {
125140
OnClose.Invoke(sender, new WebSocketCloseEventArgs(e.Reason, e.Code));
126141
}
127142
UnattachHandlers();
143+
Dispose();
128144
}
129145

130146
#endregion
@@ -153,3 +169,4 @@ public bool CheckValidCertificateCallback(System.Object sender, X509Certificate
153169

154170
}
155171
}
172+
#endif

WebSocket/WebSocketUWP.cs

+7
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ public void CloseAsync() {
8686
}
8787
}
8888

89+
public bool IsConfigured() {
90+
if (socket == null) {
91+
return false;
92+
}
93+
return true;
94+
}
95+
8996
public bool IsOpen() {
9097
if (socket != null && isOpened) {
9198
return true;

0 commit comments

Comments
 (0)