Skip to content

Commit 95f2f5a

Browse files
committed
fix(WSConnector): Removed http case for changing URLs, added pass through for unhandled URLs, Added warning to use US South instance or .NET 4.6
1 parent 7bc5dbf commit 95f2f5a

File tree

1 file changed

+30
-58
lines changed

1 file changed

+30
-58
lines changed

Scripts/Connection/WSConnector.cs

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@
2222
using IBM.Watson.DeveloperCloud.Utilities;
2323
using System.Collections;
2424
using System.Collections.Generic;
25-
using System.Threading;
26-
#if UNITY_2018_2_OR_NEWER
2725
using System.Security.Authentication;
28-
#endif
29-
26+
using System.Threading;
3027
#if !NETFX_CORE
3128
using UnitySDK.WebSocketSharp;
3229
#else
@@ -205,99 +202,74 @@ public static string FixupURL(string URL)
205202
#if UNITY_2018_2_OR_NEWER
206203
#if NET_4_6
207204
// Use standard endpoints since 2018.2 supports TLS 1.2
208-
if (URL.StartsWith("http://stream."))
209-
{
210-
URL = URL.Replace("http://stream.", "ws://stream.");
211-
}
212-
else if (URL.StartsWith("https://stream."))
205+
if (URL.StartsWith("https://stream."))
213206
{
214207
URL = URL.Replace("https://stream.", "wss://stream.");
215208
}
216209

217210
// TLS 1.0 endpoint - Do not change this to TLS 1.2 endpoint since
218211
// users may need to use the TLS 1.0 endpoint because of different
219212
// platforms.
220-
else if (URL.StartsWith("http://stream-tls10."))
221-
{
222-
URL = URL.Replace("http://stream-tls10.", "ws://stream-tls10.");
223-
}
224213
else if (URL.StartsWith("https://stream-tls10."))
225214
{
226215
URL = URL.Replace("https://stream-tls10.", "wss://stream-tls10.");
227216
}
228-
229217
// Germany
230-
else if (URL.StartsWith("http://gateway-fra."))
231-
{
232-
URL = URL.Replace("http://gateway-fra.", "ws://stream-fra.");
233-
}
234218
else if (URL.StartsWith("https://gateway-fra."))
235219
{
236220
URL = URL.Replace("https://gateway-fra.", "wss://stream-fra.");
237221
}
238-
239222
// US East
240-
else if (URL.StartsWith("http://gateway-wdc."))
241-
{
242-
URL = URL.Replace("http://gateway-wdc.", "ws://gateway-wdc.");
243-
}
244223
else if (URL.StartsWith("https://gateway-wdc."))
245224
{
246225
URL = URL.Replace("https://gateway-wdc.", "wss://gateway-wdc.");
247226
}
248-
249-
250227
// Sydney
251-
else if (URL.StartsWith("http://gateway-syd."))
252-
{
253-
URL = URL.Replace("http://gateway-syd.", "ws://gateway-syd.");
254-
}
255228
else if (URL.StartsWith("https://gateway-syd."))
256229
{
257230
URL = URL.Replace("https://gateway-syd.", "wss://gateway-syd.");
258231
}
259-
260232
else
261233
{
262-
Log.Warning("WSConnector", "No case for URL for wss://. Leaving URL unchanged.");
234+
URL = URL.Replace("https://", "wss://");
235+
Log.Warning("WSConnector", "No case for URL for wss://. Replacing https:// with wss://.");
263236
}
264237
#else
265238
// Use TLS 1.0 endpoint if user is on .NET 3.5. US South is the
266239
// only region that supports this endpoint.
267-
if (URL.StartsWith("http://stream."))
268-
{
269-
URL = URL.Replace("http://stream.", "ws://stream-tls10.");
270-
}
271-
else if (URL.StartsWith("https://stream."))
240+
if (URL.StartsWith("https://stream."))
272241
{
273242
URL = URL.Replace("https://stream.", "wss://stream-tls10.");
274243
}
275-
else if (URL.StartsWith("http://stream-tls10."))
276-
{
277-
URL = URL.Replace("http://stream-tls10.", "ws://stream-tls10.");
278-
}
279244
else if (URL.StartsWith("https://stream-tls10."))
280245
{
281246
URL = URL.Replace("https://stream-tls10.", "wss://stream-tls10.");
282247
}
248+
else
249+
{
250+
URL = URL.Replace("https://", "wss://");
251+
Log.Warning("WSConnector", "No case for URL for wss://. Replacing https:// with wss://.");
252+
Log.Warning("WSConnector", "Streaming with TLS 1.0 is only available in US South. Please create your Speech to Text instance in US South. Alternatviely, use Unity 2018.2 with .NET 4.x Scripting Runtime Version enabled (File > Build Settings > Player Settings > Other Settings > Scripting Runtime Version).");
253+
}
283254
#endif
284255
#else
285-
// Redirect to TLS 1.0 endpoints.
286-
// Note frankfurt endpoint does not support TLS 1.0.
287-
if (URL.StartsWith("http://stream."))
288-
URL = URL.Replace("http://stream.", "ws://stream-tls10.");
289-
else if (URL.StartsWith("https://stream."))
256+
// Use TLS 1.0 endpoint if user is on .NET 3.5 or 4.6 if using Unity 2018.1 or older.
257+
// US South is the only region that supports this endpoint.
258+
if (URL.StartsWith("https://stream."))
259+
{
290260
URL = URL.Replace("https://stream.", "wss://stream-tls10.");
291-
else if (URL.StartsWith("http://stream-tls10."))
292-
URL = URL.Replace("http://stream-tls10.", "ws://stream-tls10.");
261+
}
293262
else if (URL.StartsWith("https://stream-tls10."))
263+
{
294264
URL = URL.Replace("https://stream-tls10.", "wss://stream-tls10.");
295-
else if (URL.StartsWith("http://stream-fra."))
296-
URL = URL.Replace("http://stream-fra.", "ws://stream-fra.");
297-
else if (URL.StartsWith("https://stream-fra."))
298-
URL = URL.Replace("https://stream-fra.", "wss://stream-fra.");
265+
}
266+
else
267+
{
268+
URL = URL.Replace("https://", "wss://");
269+
Log.Warning("WSConnector", "No case for URL for wss://. Replacing https:// with wss://.");
270+
Log.Warning("WSConnector", "Streaming with TLS 1.0 is only available in US South. Please create your Speech to Text instance in US South. Alternatviely, use Unity 2018.2 with .NET 4.x Scripting Runtime Version enabled (File > Build Settings > Player Settings > Other Settings > Scripting Runtime Version).");
271+
}
299272
#endif
300-
301273
return URL;
302274
}
303275

@@ -330,7 +302,7 @@ public static WSConnector CreateConnector(Credentials credentials, string functi
330302
return connector;
331303
}
332304

333-
#region Public Functions
305+
#region Public Functions
334306
/// <summary>
335307
/// This function sends the given message object.
336308
/// </summary>
@@ -382,9 +354,9 @@ public void Close()
382354
// setting the state to closed will make the SendThread automatically exit.
383355
_connectionState = ConnectionState.CLOSED;
384356
}
385-
#endregion
357+
#endregion
386358

387-
#region Private Functions
359+
#region Private Functions
388360
private IEnumerator ProcessReceiveQueue()
389361
{
390362
while (_connectionState == ConnectionState.CONNECTED
@@ -415,9 +387,9 @@ private IEnumerator ProcessReceiveQueue()
415387
if (OnClose != null)
416388
OnClose(this);
417389
}
418-
#endregion
390+
#endregion
419391

420-
#region Threaded Functions
392+
#region Threaded Functions
421393
// NOTE: All functions in this region are operating in a background thread, do NOT call any Unity functions!
422394
#if !NETFX_CORE
423395
private void SendMessages()
@@ -616,6 +588,6 @@ private void WebSocket_MessageReceived(MessageWebSocket sender, MessageWebSocket
616588
}
617589
}
618590
#endif
619-
#endregion
591+
#endregion
620592
}
621593
}

0 commit comments

Comments
 (0)