From 4e95ad3c4985dce64009ceeee70e30f9742418cd Mon Sep 17 00:00:00 2001
From: zhaomingwork <61895407+zhaomingwork@users.noreply.github.com>
Date: 星期四, 15 六月 2023 16:41:36 +0800
Subject: [PATCH] For python ws client long wav not recv asr result bug (#633)

---
 funasr/runtime/websocket/funasr-ws-client.cpp |   36 +++++++++++++++++++++++++++---------
 1 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/funasr/runtime/websocket/funasr-ws-client.cpp b/funasr/runtime/websocket/funasr-ws-client.cpp
index 2691b26..4a3c751 100644
--- a/funasr/runtime/websocket/funasr-ws-client.cpp
+++ b/funasr/runtime/websocket/funasr-ws-client.cpp
@@ -5,7 +5,14 @@
 /* 2022-2023 by zhaomingwork */
 
 // client for websocket, support multiple threads
-// Usage: websocketclient server_ip port wav_path threads_num
+// ./funasr-ws-client  --server-ip <string>
+//                     --port <string>
+//                     --wav-path <string>
+//                     [--thread-num <int>] 
+//                     [--is-ssl <int>]  [--]
+//                     [--version] [-h]
+// example:
+// ./funasr-ws-client --server-ip 127.0.0.1 --port 8889 --wav-path test.wav --thread-num 1 --is-ssl 0
 
 #define ASIO_STANDALONE 1
 #include <websocketpp/client.hpp>
@@ -55,7 +62,7 @@
             asio::ssl::context::no_sslv3 | asio::ssl::context::single_dh_use);
 
     } catch (std::exception& e) {
-        std::cout << e.what() << std::endl;
+        LOG(ERROR) << e.what();
     }
     return ctx;
 }
@@ -99,7 +106,16 @@
         const std::string& payload = msg->get_payload();
         switch (msg->get_opcode()) {
             case websocketpp::frame::opcode::text:
-                std::cout << "on_message = " << payload << std::endl;
+				total_num=total_num+1;
+                LOG(INFO)<<total_num<<",on_message = " << payload;
+				if((total_num+1)==wav_index)
+				{
+					websocketpp::lib::error_code ec;
+					m_client.close(m_hdl, websocketpp::close::status::going_away, "", ec);
+					if (ec){
+                        LOG(ERROR)<< "Error closing connection " << ec.message();
+					}
+				}
         }
     }
 
@@ -132,8 +148,10 @@
             }
             send_wav_data(wav_list[i], wav_ids[i]);
         }
-        //send_wav_data();
+        WaitABit(); 
+
         asio_thread.join();
+
     }
 
     // The open handler will signal that we are ready to start sending data
@@ -200,7 +218,7 @@
                 }
             }
             if (wait) {
-                std::cout << "wait.." << m_open << std::endl;
+                LOG(INFO) << "wait.." << m_open;
                 WaitABit();
                 continue;
             }
@@ -230,7 +248,7 @@
             // send data to server
             m_client.send(m_hdl, iArray, len * sizeof(short),
                           websocketpp::frame::opcode::binary, ec);
-            std::cout << "sended data len=" << len * sizeof(short) << std::endl;
+            LOG(INFO) << "sended data len=" << len * sizeof(short);
             // The most likely error that we will get is that the connection is
             // not in the right state. Usually this means we tried to send a
             // message to a connection that was closed or in the process of
@@ -241,14 +259,13 @@
                                         "Send Error: " + ec.message());
               break;
             }
-
-            WaitABit();
+            // WaitABit();
         }
         nlohmann::json jsonresult;
         jsonresult["is_speaking"] = false;
         m_client.send(m_hdl, jsonresult.dump(), websocketpp::frame::opcode::text,
                       ec);
-        WaitABit();
+        // WaitABit();
     }
     websocketpp::client<T> m_client;
 
@@ -257,6 +274,7 @@
     websocketpp::lib::mutex m_lock;
     bool m_open;
     bool m_done;
+	int total_num=0;
 };
 
 int main(int argc, char* argv[]) {

--
Gitblit v1.9.1