Exemple #1
0
        protected override void OnWebSocketMessage(object sender, WebSocketMessageEventArgs e)
        {
            Debug.LogFormat("Web socket {1} message:\n{0}", e.Data, e.IsBinary ? "binary" : "string");

            // Renew activity timer
            ResetIdleTimer();

            // Only process text messages
            if (string.IsNullOrEmpty(e.Data))
            {
                return;
            }

            // Handle "turn.end" message as a new request id will be need to be generated!
            var match = Regex.Match(e.Data, "^Path:([A-z\\.]+)", RegexOptions.Multiline);

            if (match.Groups.Count == 2 && match.Groups[1].Value.Equals("turn.end"))
            {
                NewTurn();
            }
            else if (match.Groups.Count == 2 && match.Groups[1].Value.Equals("speech.phrase"))
            {
                // If a LUIS app script is attached then hand-off "speech.phrase" message as LUIS query request
                if (luisApp != null)
                {
                    string       jsonBody = e.Data.Substring(e.Data.IndexOf("{"));
                    SpeechPhrase phrase   = SpeechMessageHandler.ParseSpeechPhrase(jsonBody);
                    if (!string.IsNullOrEmpty(phrase.DisplayText))
                    {
                        Debug.Log("Send phrase to LUIS app: " + phrase.DisplayText);
                        luisApp.AddQueryToQueue(phrase.DisplayText);
                    }
                }
            }

            // Raise web socket data handler event
            if (OnData != null)
            {
                OnData(e.RawData, e.Data, e.IsBinary);
            }
        }
Exemple #2
0
        // Web Socket JSON data handler
        public override void OnData(byte[] rawData, string text, Boolean isBinary)
        {
            //string text = Encoding.UTF8.GetString(rawData);

            string path      = "";
            string requestId = "";
            string body      = "";

            // detect message path type
            var match = Regex.Match(text, "^Path:([A-z\\.]+)", RegexOptions.Multiline);

            if (match.Groups.Count == 2 && match.Groups[1].Value.Length > 0)
            {
                path = match.Groups[1].Value;
            }

            // detect message request id
            match = Regex.Match(text, "^X-RequestId:([A-z0-9]+)", RegexOptions.Multiline);
            if (match.Groups.Count == 2 && match.Groups[1].Value.Length > 0)
            {
                requestId = match.Groups[1].Value;
            }

            // detect message json body
            // match = Regex.Match (text, "^\\n?(\\{.*\\})", RegexOptions.Multiline | RegexOptions.Singleline);
            // if (match.Groups.Count == 2 && match.Groups[1].Value.Length > 0) {
            //   body = match.Groups[1].Value.Trim ();
            // }
            body = text.Substring(text.IndexOf("{"));

            if (String.IsNullOrEmpty(path) || String.IsNullOrEmpty(requestId) || String.IsNullOrEmpty(body))
            {
                Debug.LogError("Failed to parse speech message header and body:\n" + text);
                return;
            }

            // decide what to do depending on message path type
            if (path.Equals("speech.hypothesis"))
            {
                SpeechHypothesis hypothesis = ParseSpeechHypothesis(body);
                RaiseOnReceivedData(this, new SpeechMessageEventArgs(requestId, hypothesis.Text, false));
            }
            else if (path.Equals("speech.phrase"))
            {
                SpeechPhrase phrase = ParseSpeechPhrase(body);
                RaiseOnReceivedData(this, new SpeechMessageEventArgs(requestId, phrase.DisplayText, phrase.RecognitionStatus.Equals("Success")));
            }
            else if (path.Equals("speech.startDetected"))
            {
                SpeechDetected startDetected = ParseSpeechDetected(body);
                RaiseOnReceivedData(this, new SpeechStartEventArgs(requestId, startDetected.Offset));
            }
            else if (path.Equals("speech.endDetected"))
            {
                SpeechDetected endDetected = ParseSpeechDetected(body);
                RaiseOnReceivedData(this, new SpeechEndEventArgs(requestId, endDetected.Offset));
            }
            else if (path.Equals("turn.start"))
            {
                RaiseOnReceivedData(this, new TurnStartEventArgs(requestId, ""));
            }
            else if (path.Equals("turn.end"))
            {
                RaiseOnReceivedData(this, new TurnEndEventArgs(requestId));
            }
            else
            {
                Debug.LogWarning("Unhandled message path type: " + path);
            }
        }