Skip to content

Commit 0ccb67c

Browse files
committed
Fixed exception closing UWP message web socket
1 parent d856a2b commit 0ccb67c

File tree

3 files changed

+17
-27
lines changed

3 files changed

+17
-27
lines changed

Assets/BingSpeech/BingSpeech.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ MonoBehaviour:
898898
m_EditorClassIdentifier:
899899
key:
900900
languageMode: 6
901-
AutoConnect: 0
901+
AutoConnect: 1
902902
--- !u!4 &1124061416
903903
Transform:
904904
m_ObjectHideFlags: 0

Assets/UnityWebSocket/WebSocket/WebSocketUWP.cs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
using System;
44
using System.Collections.Generic;
5-
using System.Diagnostics;
65
using System.IO;
76
using System.Linq;
87
using System.Text;
@@ -12,6 +11,7 @@
1211
using Windows.Security.Cryptography.Certificates;
1312
using Windows.Storage.Streams;
1413
using Windows.Web;
14+
using UnityEngine;
1515

1616
namespace Unity3dAzure.WebSockets {
1717
public class WebSocketUWP : IWebSocket {
@@ -25,19 +25,16 @@ public class WebSocketUWP : IWebSocket {
2525
public event OnClose OnClose;
2626

2727
private bool isAttached = false;
28+
private bool isOpened = false;
2829

2930
private string url;
3031
private List<KeyValuePair<string, string>> headers;
3132

3233
public void ConfigureWebSocket(string url) {
33-
ConfigureWebSocket(url, null, null, 0);
34+
ConfigureWebSocket(url, null);
3435
}
3536

3637
public void ConfigureWebSocket(string url, List<KeyValuePair<string, string>> headers) {
37-
ConfigureWebSocket(url, headers, null, 0);
38-
}
39-
40-
public void ConfigureWebSocket(string url, List<KeyValuePair<string, string>> headers, string origin, uint waitTime) {
4138
if (socket != null) {
4239
throw new Exception("WebSocket is already configured!");
4340
}
@@ -57,18 +54,19 @@ public void ConfigureWebSocket(string url, List<KeyValuePair<string, string>> he
5754

5855
public async void ConnectAsync() {
5956
if (socket == null) {
60-
Debug.WriteLine("Configure MessageWebSocket");
57+
Debug.Log("Configure MessageWebSocket");
6158
ConfigureWebSocket(url, headers);
6259
}
6360
AttachHandlers();
6461
try {
6562
await socket.ConnectAsync(uri);
6663
dataWriter = new DataWriter(socket.OutputStream);
64+
isOpened = true;
6765
RaiseOpen();
6866
} catch (Exception ex) {
6967
WebErrorStatus status = WebSocketError.GetStatus(ex.GetBaseException().HResult);
7068
if (status.Equals(WebErrorStatus.Unknown)) {
71-
Debug.WriteLine("An unknown WebErrorStatus exception occurred.");
69+
Debug.LogError("An unknown WebErrorStatus exception occurred.");
7270
} else {
7371
RaiseError("Error: MessageWebSocket failed to connect: " + status.ToString());
7472
}
@@ -81,14 +79,15 @@ public void CloseAsync() {
8179
return;
8280
}
8381
try {
82+
isOpened = false;
8483
socket.Close(1000, "User closed");
8584
} catch (Exception ex) {
8685
RaiseError(ex.Message);
8786
}
8887
}
8988

9089
public bool IsOpen() {
91-
if (socket != null && isAttached) {
90+
if (socket != null && isOpened) {
9291
return true;
9392
}
9493
return false;
@@ -106,15 +105,6 @@ public async void SendAsync(byte[] data, Action<bool> completed = null) {
106105
await SendAsyncData(data, completed);
107106
}
108107

109-
public void SendAsync(FileInfo fileInfo, Action<bool> completed = null) {
110-
using (FileStream fs = fileInfo.OpenRead()) {
111-
byte[] buffer = new byte[1024];
112-
while (fs.Read(buffer, 0, buffer.Length) > 0) {
113-
SendAsync(buffer, completed);
114-
}
115-
}
116-
}
117-
118108
private async Task SendAsyncText(string message, Action<bool> completed = null) {
119109
try {
120110
await dataWriter.FlushAsync();
@@ -153,7 +143,7 @@ private async Task SendAsyncData(byte[] Data, Action<bool> completed = null) {
153143
}
154144
}
155145

156-
#region WebSocket Handlers
146+
#region WebSocket Handlers
157147

158148
private void AttachHandlers() {
159149
if (isAttached) {
@@ -169,11 +159,9 @@ private void UnattachHandlers() {
169159
return;
170160
}
171161
isAttached = false;
172-
try {
162+
if (isOpened) {
173163
socket.MessageReceived -= HandleOnMessage;
174164
socket.Closed -= HandleOnClose;
175-
} catch (Exception ex) {
176-
Debug.WriteLine("Socket closed: " + ex.Message);
177165
}
178166
}
179167

@@ -182,6 +170,8 @@ private void Dispose() {
182170
dataWriter = null;
183171
socket.Dispose();
184172
socket = null;
173+
isAttached = false;
174+
isOpened = false;
185175
}
186176

187177
private void RaiseError(string message) {
@@ -226,7 +216,7 @@ private void HandleOnClose(object sender, WebSocketClosedEventArgs args) {
226216
Dispose();
227217
}
228218

229-
#endregion
219+
#endregion
230220

231221
private Uri WebSocketUri(string url) {
232222
Uri webSocketUri;

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
Web Socket samples for Unity:
44

5-
* Bing Speech web socket demo (requires Key)
5+
* [Bing Speech API](https://portal.azure.com) web socket demo (requires Key)
66
* Echo message demo
77
* Transform JSON demo
88

99
## :octocat: Download instructions
1010

11-
This project contains git submodule dependencies so use:
11+
This project contains git submodule dependencies so use:
1212
`git clone --recursive https://github.com/Unity3dAzure/UnityWebSocketDemo.git`
1313

14-
Or if you've already done a git clone then use:
14+
Or if you've already done a git clone then use:
1515
`git submodule update --init --recursive`
1616

1717
## Demos

0 commit comments

Comments
 (0)