雾聪
2023-06-14 385a421cedbd4d358816f1faab017717a77c0312
update funasr-ws-client funasr-ws-server
3个文件已修改
56 ■■■■ 已修改文件
funasr/runtime/websocket/funasr-ws-client.cpp 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/websocket/funasr-ws-server.cpp 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/websocket/websocket-server.cpp 26 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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,7 @@
        const std::string& payload = msg->get_payload();
        switch (msg->get_opcode()) {
            case websocketpp::frame::opcode::text:
                std::cout << "on_message = " << payload << std::endl;
                LOG(INFO)<<"on_message = " << payload;
        }
    }
@@ -135,11 +142,9 @@
        WaitABit();
        m_client.close(m_hdl,websocketpp::close::status::going_away, "", ec);
        if (ec) {
                std::cout << "> Error closing connection " << ec.message() << std::endl;
                LOG(ERROR)<< "Error closing connection " << ec.message();
            }
        //send_wav_data();
        asio_thread.join();
    }
    // The open handler will signal that we are ready to start sending data
@@ -206,7 +211,7 @@
                }
            }
            if (wait) {
                std::cout << "wait.." << m_open << std::endl;
                LOG(INFO) << "wait.." << m_open;
                WaitABit();
                continue;
            }
@@ -236,7 +241,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
@@ -247,14 +252,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;
funasr/runtime/websocket/funasr-ws-server.cpp
@@ -5,7 +5,7 @@
/* 2022-2023 by zhaomingwork */
// io server
// Usage:websocketmain  [--model_thread_num <int>] [--decoder_thread_num <int>]
// Usage:funasr-ws-server  [--model_thread_num <int>] [--decoder_thread_num <int>]
//                    [--io_thread_num <int>] [--port <int>] [--listen_ip
//                    <string>] [--punc-quant <string>] [--punc-dir <string>]
//                    [--vad-quant <string>] [--vad-dir <string>] [--quantize
@@ -25,7 +25,7 @@
    google::InitGoogleLogging(argv[0]);
    FLAGS_logtostderr = true;
    TCLAP::CmdLine cmd("websocketmain", ' ', "1.0");
    TCLAP::CmdLine cmd("funasr-ws-server", ' ', "1.0");
    TCLAP::ValueArg<std::string> model_dir(
        "", MODEL_DIR,
        "the asr model path, which contains model.onnx, config.yaml, am.mvn",
funasr/runtime/websocket/websocket-server.cpp
@@ -22,11 +22,10 @@
                                         std::string& s_keyfile) {
  namespace asio = websocketpp::lib::asio;
  std::cout << "on_tls_init called with hdl: " << hdl.lock().get() << std::endl;
  std::cout << "using TLS mode: "
  LOG(INFO) << "on_tls_init called with hdl: " << hdl.lock().get();
  LOG(INFO) << "using TLS mode: "
            << (mode == MOZILLA_MODERN ? "Mozilla Modern"
                                       : "Mozilla Intermediate")
            << std::endl;
                                       : "Mozilla Intermediate");
  context_ptr ctx = websocketpp::lib::make_shared<asio::ssl::context>(
      asio::ssl::context::sslv23);
@@ -49,7 +48,7 @@
    ctx->use_private_key_file(s_keyfile, asio::ssl::context::pem);
  } catch (std::exception& e) {
    std::cout << "Exception: " << e.what() << std::endl;
    LOG(INFO) << "Exception: " << e.what();
  }
  return ctx;
}
@@ -86,8 +85,7 @@
                      ec);
      }
      std::cout << "buffer.size=" << buffer.size()
                << ",result json=" << jsonresult.dump() << std::endl;
      LOG(INFO) << "buffer.size=" << buffer.size() << ",result json=" << jsonresult.dump();
      if (!isonline) {
        //  close the client if it is not online asr
        // server_->close(hdl, websocketpp::close::status::normal, "DONE", ec);
@@ -110,14 +108,14 @@
  data_msg->samples = std::make_shared<std::vector<char>>();
  data_msg->msg = nlohmann::json::parse("{}");
  data_map.emplace(hdl, data_msg);
  std::cout << "on_open, active connections: " << data_map.size() << std::endl;
  LOG(INFO) << "on_open, active connections: " << data_map.size();
}
void WebSocketServer::on_close(websocketpp::connection_hdl hdl) {
  scoped_lock guard(m_lock);
  data_map.erase(hdl);  // remove data vector when  connection is closed
  std::cout << "on_close, active connections: " << data_map.size() << std::endl;
  LOG(INFO) << "on_close, active connections: " << data_map.size();
}
// remove closed connection
@@ -143,7 +141,7 @@
  }
  for (auto hdl : to_remove) {
    data_map.erase(hdl);
    std::cout << "remove one connection " << std::endl;
    LOG(INFO)<< "remove one connection ";
  }
}
void WebSocketServer::on_message(websocketpp::connection_hdl hdl,
@@ -161,7 +159,7 @@
  lock.unlock();
  if (sample_data_p == nullptr) {
    std::cout << "error when fetch sample data vector" << std::endl;
    LOG(INFO) << "error when fetch sample data vector";
    return;
  }
@@ -176,7 +174,7 @@
      if (jsonresult["is_speaking"] == false ||
          jsonresult["is_finished"] == true) {
        std::cout << "client done" << std::endl;
        LOG(INFO) << "client done";
        if (isonline) {
          // do_close(ws);
@@ -225,9 +223,9 @@
    // init model with api
    asr_hanlde = FunOfflineInit(model_path, thread_num);
    std::cout << "model ready" << std::endl;
    LOG(INFO) << "model successfully inited";
  } catch (const std::exception& e) {
    std::cout << e.what() << std::endl;
    LOG(INFO) << e.what();
  }
}