2 changed files with 136 additions and 0 deletions
-
73src/iflytop/components/iflytop_front_end_service/iflytop_front_end_service.cpp
-
63src/iflytop/components/iflytop_front_end_service/iflytop_front_end_service.hpp
@ -0,0 +1,73 @@ |
|||||
|
#include "iflytop_front_end_service.hpp"
|
||||
|
using namespace nlohmann; |
||||
|
using namespace iflytop; |
||||
|
using namespace std; |
||||
|
using namespace core; |
||||
|
using namespace ix; |
||||
|
|
||||
|
void IflytopFrontEndService::initialize() { |
||||
|
logger->info("IflytopFrontEndService initialize {}", 19001); |
||||
|
|
||||
|
m_server.reset(new WebSocketServer(19001, "0.0.0.0")); |
||||
|
m_server->setOnConnectionCallback([this](weak_ptr<WebSocket> webSocket, shared_ptr<ConnectionState> connectionState) { |
||||
|
logger->info("Remote ip: {}", connectionState->getRemoteIp()); |
||||
|
auto ws = webSocket.lock(); |
||||
|
if (!ws) return; |
||||
|
ws->setOnMessageCallback([this, webSocket, connectionState](const ix::WebSocketMessagePtr& msg) { |
||||
|
try { |
||||
|
onMessageCallback(webSocket, connectionState, msg); |
||||
|
} catch (const std::exception& e) { |
||||
|
logger->error("catch exception,onMessageCallback error: {}", e.what()); |
||||
|
} |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
m_initialized = true; |
||||
|
} |
||||
|
void IflytopFrontEndService::startListen() { |
||||
|
auto res = m_server->listen(); |
||||
|
if (!res.first) { |
||||
|
logger->error("listen error!!!!"); |
||||
|
return; |
||||
|
} |
||||
|
m_server->disablePerMessageDeflate(); |
||||
|
m_server->start(); |
||||
|
// m_server->wait();
|
||||
|
logger->info("IflytopFrontEndService initialize done"); |
||||
|
} |
||||
|
|
||||
|
void IflytopFrontEndService::sendMessage(const string& message) { |
||||
|
if (!m_initialized) { |
||||
|
logger->error("IflytopFrontEndService not initialized"); |
||||
|
return; |
||||
|
} |
||||
|
/**
|
||||
|
* @brief 广播消息给所有连接的客户端 |
||||
|
*/ |
||||
|
logger->debug("uplink msg: {}", message); |
||||
|
auto clients = m_server->getClients(); |
||||
|
for (auto& each : clients) { |
||||
|
if (each) { |
||||
|
each->sendText(message); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void IflytopFrontEndService::onMessageCallback(weak_ptr<WebSocket> webSocket, shared_ptr<ConnectionState> connectionState, const ix::WebSocketMessagePtr& msg) { |
||||
|
if (msg->type == ix::WebSocketMessageType::Open) { |
||||
|
logger->info("New connection"); |
||||
|
logger->info("id : {}", connectionState->getId()); |
||||
|
logger->info("Uri : {}", msg->openInfo.uri); |
||||
|
logger->info("Headers:"); |
||||
|
for (auto it : msg->openInfo.headers) { |
||||
|
logger->info(" {}: {}", it.first, it.second); |
||||
|
} |
||||
|
onConnect(webSocket, msg); |
||||
|
} else if (msg->type == ix::WebSocketMessageType::Message) { |
||||
|
logger->debug("downlink msg: {}", msg->str); |
||||
|
onMessage(webSocket, msg); |
||||
|
} else if (msg->type == ix::WebSocketMessageType::Close) { |
||||
|
logger->info("{} Closed connection", msg->closeInfo.remote); |
||||
|
onDisconnect(webSocket, msg); |
||||
|
} |
||||
|
} |
@ -0,0 +1,63 @@ |
|||||
|
//
|
||||
|
// Created by iflytop
|
||||
|
//
|
||||
|
|
||||
|
#pragma once
|
||||
|
#include <ixwebsocket/IXWebSocketServer.h>
|
||||
|
|
||||
|
#include <fstream>
|
||||
|
#include <iostream>
|
||||
|
#include <list>
|
||||
|
#include <map>
|
||||
|
#include <memory>
|
||||
|
#include <set>
|
||||
|
#include <sstream>
|
||||
|
#include <string>
|
||||
|
#include <vector>
|
||||
|
|
||||
|
#include "iflytop/core/basic/nod/nod.hpp"
|
||||
|
#include "iflytop/core/spdlogfactory/logger.hpp"
|
||||
|
/**
|
||||
|
* @brief |
||||
|
* |
||||
|
* service: IflytopHardwareWSService |
||||
|
* |
||||
|
* 协议文档 |
||||
|
* https://iflytop1.feishu.cn/docx/XCeWddMyzoktUdx6EeMcOV5HnAb
|
||||
|
* 端口号 |
||||
|
* 19001 |
||||
|
* 参考https://iflytop1.feishu.cn/wiki/wikcnukbr3vOdBfrNdnVXX8YkLg
|
||||
|
*/ |
||||
|
|
||||
|
namespace iflytop { |
||||
|
using namespace std; |
||||
|
using namespace iflytop; |
||||
|
using namespace core; |
||||
|
using namespace ix; |
||||
|
|
||||
|
|
||||
|
class IflytopFrontEndService : public enable_shared_from_this<IflytopFrontEndService> { |
||||
|
ENABLE_LOGGER(IflytopFrontEndService); |
||||
|
|
||||
|
public: |
||||
|
nod::signal<void(weak_ptr<WebSocket> webSocket, const ix::WebSocketMessagePtr& msg)> onMessage; |
||||
|
nod::signal<void(weak_ptr<WebSocket> webSocket, const ix::WebSocketMessagePtr& msg)> onConnect; |
||||
|
nod::signal<void(weak_ptr<WebSocket> webSocket, const ix::WebSocketMessagePtr& msg)> onDisconnect; |
||||
|
|
||||
|
private: |
||||
|
//
|
||||
|
// WebSocketServer的使用参考:http://192.168.1.3:3000/z3rd_lib/IXWebSocket/src/branch/master/docs/usage.md
|
||||
|
//
|
||||
|
shared_ptr<ix::WebSocketServer> m_server; |
||||
|
bool m_initialized = false; |
||||
|
|
||||
|
public: |
||||
|
void initialize(); |
||||
|
void startListen(); |
||||
|
|
||||
|
void sendMessage(const string& message); |
||||
|
|
||||
|
private: |
||||
|
void onMessageCallback(weak_ptr<WebSocket> webSocket, shared_ptr<ConnectionState> connectionState, const ix::WebSocketMessagePtr& msg); |
||||
|
}; |
||||
|
} // namespace iflytop
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue