Skip to content

Commit

Permalink
feat(ws): removing some unused code, removing some files, and impleme…
Browse files Browse the repository at this point in the history
…nt parsing data from json on handle_packet() from ws client
  • Loading branch information
YoruAkio committed Oct 23, 2024
1 parent 07dea0c commit 7184b77
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 105 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ c.connect(con);
c.run();
```
## Contact
If you have any questions or suggestions, feel free to contact me at:
- Discord: [@yoruakio](https://discord.com/users/919841186246692886)
- Telegram: [@yoruakio](https://t.me/yoruakio) or [HTF](https://t.me/htf_public)
## LICENSE
WebSocket-Server is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information.
9 changes: 0 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "websocket/websocket.h"
// #include "ws_client_test.cpp"

int main() {
WSServer& server = WSServer::Get();
Expand All @@ -15,13 +14,5 @@ int main() {
std::cerr << "Failed to start the server." << std::endl;
}

// Client code version

// std::string uri = "ws://localhost:9002";
// std::string auth_token = "your_secret_token";

// WSClient client;
// client.connect(uri, auth_token);

return 0;
}
95 changes: 0 additions & 95 deletions websocket/websocket.cpp

This file was deleted.

13 changes: 12 additions & 1 deletion websocket/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <atomic>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <nlohmann/json.hpp>
#include <unordered_map>
#include <iostream>
#include <chrono>
Expand Down Expand Up @@ -118,7 +119,17 @@ class WSServer {

client_info.message_count++;

std::cout << "Received message: " << msg->get_payload() << std::endl;
nlohmann::json received_data;
try {
received_data = nlohmann::json::parse(msg->get_payload());
} catch (nlohmann::json::parse_error& e) {
std::cout << "Received message: " << msg->get_payload() << std::endl;
std::cerr << "Failed to parse message as JSON: " << e.what() << std::endl;
ws_server->close(hdl, websocketpp::close::status::invalid_payload, "Invalid JSON");
return;
}

std::cout << "Received JSON data: " << received_data.dump(4) << std::endl;

ws_server->send(hdl, msg->get_payload(), msg->get_opcode());
});
Expand Down

0 comments on commit 7184b77

Please sign in to comment.