Skip to content

Commit 0834da6

Browse files
committed
update websocket library, add customdata to StartListening for request headers
1 parent 40922b0 commit 0834da6

File tree

3 files changed

+65
-53
lines changed

3 files changed

+65
-53
lines changed

Plugins/websocket-sharp.dll

0 Bytes
Binary file not shown.

Scripts/Connection/WSConnector.cs

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -300,58 +300,58 @@ private IEnumerator ProcessReceiveQueue()
300300
#region Threaded Functions
301301
// NOTE: ALl functions in this region are operating in a background thread, do NOT call any Unity functions!
302302
#if !NETFX_CORE
303-
private void SendMessages()
304-
{
305-
try
306-
{
307-
WebSocket ws = null;
308-
309-
ws = new WebSocket(URL);
310-
//if (Headers != null)
311-
// ws.Headers = Headers;
312-
if (Authentication != null)
313-
ws.SetCredentials(Authentication.Username, Authentication.Password, true);
314-
ws.OnOpen += OnWSOpen;
315-
ws.OnClose += OnWSClose;
316-
ws.OnError += OnWSError;
317-
ws.OnMessage += OnWSMessage;
318-
ws.Connect();
319-
320-
while (_connectionState == ConnectionState.CONNECTED)
321-
{
322-
_sendEvent.WaitOne(50);
323-
324-
Message msg = null;
325-
lock (_sendQueue)
326-
{
327-
if (_sendQueue.Count > 0)
328-
msg = _sendQueue.Dequeue();
329-
}
330-
331-
while (msg != null)
332-
{
333-
if (msg is TextMessage)
334-
ws.Send(((TextMessage)msg).Text);
335-
else if (msg is BinaryMessage)
336-
ws.Send(((BinaryMessage)msg).Data);
337-
338-
msg = null;
339-
lock (_sendQueue)
340-
{
341-
if (_sendQueue.Count > 0)
342-
msg = _sendQueue.Dequeue();
343-
}
344-
}
345-
}
346-
347-
ws.Close();
348-
}
349-
catch (System.Exception e)
350-
{
351-
_connectionState = ConnectionState.DISCONNECTED;
352-
Log.Error("WSConnector", "Caught WebSocket exception: {0}", e.ToString());
353-
}
354-
}
303+
private void SendMessages()
304+
{
305+
try
306+
{
307+
WebSocket ws = null;
308+
309+
ws = new WebSocket(URL);
310+
if (Headers != null)
311+
ws.CustomHeaders = Headers;
312+
if (Authentication != null)
313+
ws.SetCredentials(Authentication.Username, Authentication.Password, true);
314+
ws.OnOpen += OnWSOpen;
315+
ws.OnClose += OnWSClose;
316+
ws.OnError += OnWSError;
317+
ws.OnMessage += OnWSMessage;
318+
ws.Connect();
319+
320+
while (_connectionState == ConnectionState.CONNECTED)
321+
{
322+
_sendEvent.WaitOne(50);
323+
324+
Message msg = null;
325+
lock (_sendQueue)
326+
{
327+
if (_sendQueue.Count > 0)
328+
msg = _sendQueue.Dequeue();
329+
}
330+
331+
while (msg != null)
332+
{
333+
if (msg is TextMessage)
334+
ws.Send(((TextMessage)msg).Text);
335+
else if (msg is BinaryMessage)
336+
ws.Send(((BinaryMessage)msg).Data);
337+
338+
msg = null;
339+
lock (_sendQueue)
340+
{
341+
if (_sendQueue.Count > 0)
342+
msg = _sendQueue.Dequeue();
343+
}
344+
}
345+
}
346+
347+
ws.Close();
348+
}
349+
catch (System.Exception e)
350+
{
351+
_connectionState = ConnectionState.DISCONNECTED;
352+
Log.Error("WSConnector", "Caught WebSocket exception: {0}", e.ToString());
353+
}
354+
}
355355

356356
private void OnWSOpen(object sender, System.EventArgs e)
357357
{

Scripts/Services/SpeechToText/v1/SpeechToText.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ private void OnGetModelResponse(RESTConnector.Request req, RESTConnector.Respons
501501
/// <param name="callback">All recognize results are passed to this callback.</param>
502502
/// <param name="speakerLabelCallback">Speaker label goes through this callback if it arrives separately from recognize result.</param>
503503
/// <returns>Returns true on success, false on failure.</returns>
504-
public bool StartListening(OnRecognize callback, OnRecognizeSpeaker speakerLabelCallback = null)
504+
public bool StartListening(OnRecognize callback, OnRecognizeSpeaker speakerLabelCallback = null, Dictionary<string, object> customData = null)
505505
{
506506
if (callback == null)
507507
throw new ArgumentNullException("callback");
@@ -510,6 +510,18 @@ public bool StartListening(OnRecognize callback, OnRecognizeSpeaker speakerLabel
510510
if (!CreateListenConnector())
511511
return false;
512512

513+
Dictionary<string, string> customHeaders = null;
514+
if (customData.ContainsKey(Constants.String.CUSTOM_REQUEST_HEADERS))
515+
{
516+
foreach (KeyValuePair<string, string> kvp in customData[Constants.String.CUSTOM_REQUEST_HEADERS] as Dictionary<string, string>)
517+
{
518+
customHeaders.Add(kvp.Key, kvp.Value);
519+
}
520+
}
521+
522+
if (customHeaders != null && _listenSocket != null)
523+
_listenSocket.Headers = customHeaders;
524+
513525
_isListening = true;
514526
_listenCallback = callback;
515527
if (speakerLabelCallback != null)

0 commit comments

Comments
 (0)