| | |
| | | websocketpp::frame::opcode::text, ec);
|
| | | }
|
| | | FunASRFreeResult(Result);
|
| | | }else{
|
| | | if(msg["wav_format"] != "pcm" && msg["wav_format"] != "PCM"){
|
| | | websocketpp::lib::error_code ec;
|
| | | nlohmann::json jsonresult;
|
| | | jsonresult["text"] = "ERROR. Real-time transcription service ONLY SUPPORT wav_format pcm.";
|
| | | jsonresult["wav_name"] = wav_name;
|
| | | jsonresult["is_final"] = true;
|
| | | if (is_ssl) {
|
| | | wss_server_->send(hdl, jsonresult.dump(),
|
| | | websocketpp::frame::opcode::text, ec);
|
| | | } else {
|
| | | server_->send(hdl, jsonresult.dump(),
|
| | | websocketpp::frame::opcode::text, ec);
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | data_msg->msg["is_eof"]=false; // if this connection is closed
|
| | | data_msg->punc_cache =
|
| | | std::make_shared<std::vector<std::vector<std::string>>>(2);
|
| | | data_msg->strand_ = std::make_shared<asio::io_context::strand>(io_decoder_);
|
| | |
|
| | | data_map.emplace(hdl, data_msg);
|
| | | // LOG(INFO) << "on_open, active connections: " << data_map.size();
|
| | | }catch (std::exception const& e) {
|
| | | std::cerr << "Error: " << e.what() << std::endl;
|
| | | }
|
| | |
| | | // finished and avoid access freed tpass_online_handle
|
| | | unique_lock guard_decoder(*(data_msg->thread_lock));
|
| | | if (data_msg->msg["access_num"]==0 && data_msg->msg["is_eof"]==true) {
|
| | | // LOG(INFO) << "----------------FunTpassOnlineUninit----------------------";
|
| | | FunTpassOnlineUninit(data_msg->tpass_online_handle);
|
| | | data_msg->tpass_online_handle = nullptr;
|
| | | data_map.erase(hdl);
|
| | |
| | | unique_lock guard_decoder(*(data_msg->thread_lock));
|
| | | data_msg->msg["is_eof"]=true;
|
| | | guard_decoder.unlock();
|
| | | //LOG(INFO) << "on_close, active connections: " << data_map.size();
|
| | | }
|
| | |
|
| | | // remove closed connection
|
| | | void WebSocketServer::check_and_clean_connection() {
|
| | | // LOG(INFO)<<"***********begin check_and_clean_connection ****************";
|
| | | while(true){
|
| | | std::this_thread::sleep_for(std::chrono::milliseconds(5000));
|
| | | // LOG(INFO) << "run check_and_clean_connection" <<", active connections: " << data_map.size();
|
| | | std::vector<websocketpp::connection_hdl> to_remove; // remove list
|
| | | auto iter = data_map.begin();
|
| | | while (iter != data_map.end()) { // loop to find closed connection
|
| | |
| | | iter++;
|
| | | }
|
| | | for (auto hdl : to_remove) {
|
| | | remove_hdl(hdl, data_map);
|
| | | // LOG(INFO) << "remove one connection ";
|
| | | {
|
| | | unique_lock lock(m_lock);
|
| | | remove_hdl(hdl, data_map);
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
| | |
|
| | | // if it is in final message, post the sample_data to decode
|
| | | try{
|
| | | asio::post(
|
| | | io_decoder_,
|
| | | |
| | | msg_data->strand_->post(
|
| | | std::bind(&WebSocketServer::do_decoder, this,
|
| | | std::move(*(sample_data_p.get())), std::move(hdl),
|
| | | std::ref(msg_data->msg), std::ref(*(punc_cache_p.get())),
|
| | |
| | |
|
| | | try{
|
| | | // post to decode
|
| | | asio::post(io_decoder_,
|
| | | msg_data->strand_->post( |
| | | std::bind(&WebSocketServer::do_decoder, this,
|
| | | std::move(subvector), std::move(hdl),
|
| | | std::ref(msg_data->msg),
|
| | |
| | | LOG(ERROR) << "FunTpassInit init failed";
|
| | | exit(-1);
|
| | | }
|
| | | LOG(INFO) << "initAsr run check_and_clean_connection";
|
| | | std::thread clean_thread(&WebSocketServer::check_and_clean_connection,this); |
| | | clean_thread.detach();
|
| | | LOG(INFO) << "initAsr run check_and_clean_connection finished";
|
| | | LOG(INFO) << "initAsr run check_and_clean_connection";
|
| | | std::thread clean_thread(&WebSocketServer::check_and_clean_connection,this); |
| | | clean_thread.detach();
|
| | | LOG(INFO) << "initAsr run check_and_clean_connection finished";
|
| | |
|
| | | } catch (const std::exception& e) {
|
| | | LOG(INFO) << e.what();
|