Skip to content

Commit

Permalink
Refactor WebSocket server initialization and communication in stenogr…
Browse files Browse the repository at this point in the history
…apher.cpp
  • Loading branch information
royshil committed Oct 10, 2024
1 parent 283bf34 commit f5dc4c8
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/stenographer/stenographer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ class TranscriptionHandler::Impl {
messageCallback(callback),
running(false)
{
server.init_asio();
wsServer.init_asio();

server.set_open_handler([this](websocketpp::connection_hdl hdl) {
wsServer.set_open_handler([this](websocketpp::connection_hdl hdl) {
std::lock_guard<std::mutex> lock(mutex);
connection = hdl;
});

server.set_message_handler(
wsServer.set_message_handler(
[this](websocketpp::connection_hdl hdl, server::message_ptr msg) {
UNUSED_PARAMETER(hdl);
handleIncomingMessage(msg->get_payload());
Expand All @@ -69,9 +69,9 @@ class TranscriptionHandler::Impl {
if (!running) {
running = true;
serverThread = std::async(std::launch::async, [this]() {
server.listen(9002);
server.start_accept();
server.run();
wsServer.listen(9002);
wsServer.start_accept();
wsServer.run();
});

processingThread =
Expand All @@ -83,7 +83,7 @@ class TranscriptionHandler::Impl {
{
if (running) {
running = false;
server.stop();
wsServer.stop();
if (serverThread.valid())
serverThread.wait();
if (processingThread.valid())
Expand All @@ -93,7 +93,7 @@ class TranscriptionHandler::Impl {

private:
transcription_filter_data *gf;
server server;
server wsServer;
websocketpp::connection_hdl connection;
MessageCallback messageCallback;
std::queue<std::vector<int16_t>> audioQueue;
Expand Down Expand Up @@ -149,8 +149,8 @@ class TranscriptionHandler::Impl {
start_timestamp_offset_ns},
{"end_timestamp", end_timestamp_offset_ns}};
if (connection.lock()) {
server.send(connection, timestampInfo.dump(),
websocketpp::frame::opcode::text);
wsServer.send(connection, timestampInfo.dump(),
websocketpp::frame::opcode::text);
}
sendAudioData(pcmData);
} else {
Expand Down Expand Up @@ -192,8 +192,8 @@ class TranscriptionHandler::Impl {
std::memcpy(wavData.data() + sizeof(WAVHeader), audioBuffer.data(),
wavHeader.data_size);

server.send(connection, wavData.data(), wavData.size(),
websocketpp::frame::opcode::binary);
wsServer.send(connection, wavData.data(), wavData.size(),
websocketpp::frame::opcode::binary);

audioBuffer.clear();
}
Expand Down

0 comments on commit f5dc4c8

Please sign in to comment.