@@ -58,13 +58,15 @@ int main(int argc, char **argv) {
58
58
};
59
59
60
60
// 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) {
62
62
thread_local std::vector<float > audioBuffer; // thread-localized variable
63
63
thread_local wav_writer wavWriter;
64
- std::string filename;
64
+ thread_local std::string filename;
65
65
// 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;
68
70
if (opCode == uWS::OpCode::TEXT) {
69
71
printf (" %s: Received message on /paddlespeech/asr/streaming: %s\n " , get_current_time ().c_str (),
70
72
std::string (message).c_str ());
@@ -78,27 +80,26 @@ int main(int argc, char **argv) {
78
80
}
79
81
std::string signal = jsonMsg[" signal" ];
80
82
if (signal == " start" ) {
83
+ final_results = nlohmann::json (nlohmann::json::array ());
81
84
// 发送服务器准备好的消息
82
- nlohmann::json response = {{" status" , " ok" },
83
- {" signal" , " server_ready" }};
85
+ response = {{" status" , " ok" },
86
+ {" signal" , " server_ready" }};
84
87
ws->send (response.dump (), uWS::OpCode::TEXT);
85
88
wavWriter.open (filename, WHISPER_SAMPLE_RATE, 16 , 1 );
86
89
}
87
90
if (signal == " end" ) {
88
91
wavWriter.close ();
89
92
// 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;
93
96
ws->send (response.dump (), uWS::OpCode::TEXT);
94
97
}
95
98
// other process logic...
96
99
} catch (const std::exception &e) {
97
100
std::cerr << " JSON parse error: " << e.what () << std::endl;
98
101
}
99
102
} else if (opCode == uWS::OpCode::BINARY) {
100
- nlohmann::json response;
101
-
102
103
// process binary message(PCM16 data)
103
104
auto size = message.size ();
104
105
@@ -132,13 +133,14 @@ int main(int argc, char **argv) {
132
133
segment[" sentence" ] = sentence;
133
134
results.push_back (segment);
134
135
}
135
- final_results= results;
136
- response[" result" ] = results ;
136
+ final_results = results;
137
+ response[" result" ] = final_results ;
137
138
}
138
139
139
140
ws->send (response.dump (), uWS::OpCode::TEXT);
140
141
}
141
142
};
143
+ auto ws_streaming_handler = item;
142
144
143
145
// config uWebSockets app
144
146
uWS::App ()
0 commit comments