Skip to content

Commit fce85a4

Browse files
author
litongmacos
committed
finish single thread version
1 parent 09bfaf9 commit fce85a4

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

whisper_server_base_on_uwebsockets.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ int main(int argc, char **argv) {
5858
};
5959

6060
// WebSocket /paddlespeech/asr/streaming handler
61-
auto ws_streaming_handler = [&whisperService](auto *ws, std::string_view message, uWS::OpCode opCode) {
61+
auto item = [&whisperService](auto *ws, std::string_view message, uWS::OpCode opCode) {
6262
thread_local std::vector<float> audioBuffer; //thread-localized variable
6363
thread_local wav_writer wavWriter;
64-
std::string filename;
64+
thread_local std::string filename;
6565
//std::unique_ptr<nlohmann::json> results(new nlohmann::json(nlohmann::json::array()));
66-
nlohmann::json final_results = nlohmann::json(nlohmann::json::array());
67-
66+
thread_local nlohmann::json final_results;
67+
auto thread_id = std::this_thread::get_id();
68+
std::cout << get_current_time().c_str() << ": Handling a message in thread: " << thread_id << std::endl;
69+
nlohmann::json response;
6870
if (opCode == uWS::OpCode::TEXT) {
6971
printf("%s: Received message on /paddlespeech/asr/streaming: %s\n", get_current_time().c_str(),
7072
std::string(message).c_str());
@@ -78,27 +80,26 @@ int main(int argc, char **argv) {
7880
}
7981
std::string signal = jsonMsg["signal"];
8082
if (signal == "start") {
83+
final_results = nlohmann::json(nlohmann::json::array());
8184
// 发送服务器准备好的消息
82-
nlohmann::json response = {{"status", "ok"},
83-
{"signal", "server_ready"}};
85+
response = {{"status", "ok"},
86+
{"signal", "server_ready"}};
8487
ws->send(response.dump(), uWS::OpCode::TEXT);
8588
wavWriter.open(filename, WHISPER_SAMPLE_RATE, 16, 1);
8689
}
8790
if (signal == "end") {
8891
wavWriter.close();
8992
// nlohmann::json response = {{"name",filename},{"signal", signal}};
90-
nlohmann::json response = {{"name", filename},
91-
{"signal", signal},
92-
{"result", final_results}};
93+
response = {{"name", filename},
94+
{"signal", signal}};
95+
response["result"] = final_results;
9396
ws->send(response.dump(), uWS::OpCode::TEXT);
9497
}
9598
// other process logic...
9699
} catch (const std::exception &e) {
97100
std::cerr << "JSON parse error: " << e.what() << std::endl;
98101
}
99102
} else if (opCode == uWS::OpCode::BINARY) {
100-
nlohmann::json response;
101-
102103
// process binary message(PCM16 data)
103104
auto size = message.size();
104105

@@ -132,13 +133,14 @@ int main(int argc, char **argv) {
132133
segment["sentence"] = sentence;
133134
results.push_back(segment);
134135
}
135-
final_results=results;
136-
response["result"] = results;
136+
final_results = results;
137+
response["result"] = final_results;
137138
}
138139

139140
ws->send(response.dump(), uWS::OpCode::TEXT);
140141
}
141142
};
143+
auto ws_streaming_handler = item;
142144

143145
// config uWebSockets app
144146
uWS::App()

0 commit comments

Comments
 (0)