4 changed files with 134 additions and 35 deletions
-
2src/configs/version.hpp
-
43src/service/extapi_service.cpp
-
72src/service/wbesocket_connect_mgr_service.cpp
-
50src/service/wbesocket_connect_mgr_service.hpp
@ -1,2 +1,2 @@ |
|||||
#pragma once
|
#pragma once
|
||||
#define VERSION "2.1"
|
|
||||
|
#define VERSION "2.2"
|
@ -0,0 +1,72 @@ |
|||||
|
#include "wbesocket_connect_mgr_service.hpp"
|
||||
|
|
||||
|
using namespace iflytop; |
||||
|
using namespace core; |
||||
|
using namespace std; |
||||
|
using namespace nlohmann; |
||||
|
using namespace ix; |
||||
|
|
||||
|
// static map<string,
|
||||
|
static map<string, list<shared_ptr<WebSocket>>> wsClients; |
||||
|
static map<void *, shared_ptr<WebSocketConnectInfo>> wsClientsInfo; |
||||
|
static std::mutex wsClientsLock; |
||||
|
|
||||
|
shared_ptr<WebSocketConnectInfo> findwsinfo(shared_ptr<WebSocket> client) { |
||||
|
if (client == nullptr) return nullptr; |
||||
|
auto it = wsClientsInfo.find((void *)client.get()); |
||||
|
if (it == wsClientsInfo.end()) return nullptr; |
||||
|
return it->second; |
||||
|
} |
||||
|
|
||||
|
void WbesocketConnectMgrService::insertNewClient(string chname, shared_ptr<WebSocket> client, shared_ptr<WebSocketConnectInfo> connectInfo) { |
||||
|
lock_guard<mutex> lock(wsClientsLock); |
||||
|
if (client == nullptr) return; |
||||
|
if (chname.empty()) return; |
||||
|
if (wsClients.find(chname) == wsClients.end()) { |
||||
|
wsClients[chname] = list<shared_ptr<WebSocket>>(); |
||||
|
} |
||||
|
wsClients[chname].push_back(client); |
||||
|
wsClientsInfo[(void *)client.get()] = connectInfo; |
||||
|
GET_LOGGER(WSConnectMgr)->info("new client: {} {}, channel: {}", client->getUrl(), (void *)client.get(), chname); |
||||
|
} |
||||
|
void WbesocketConnectMgrService::removeClient(string chname, shared_ptr<WebSocket> client) { |
||||
|
lock_guard<mutex> lock(wsClientsLock); |
||||
|
if (client == nullptr) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (wsClients.find(chname) != wsClients.end()) { |
||||
|
wsClients[chname].remove(client); |
||||
|
wsClientsInfo.erase((void *)client.get()); |
||||
|
GET_LOGGER(WSConnectMgr)->info("remove client: {} {}, channel: {}", client->getUrl(), (void *)client.get(), chname); |
||||
|
} |
||||
|
} |
||||
|
void WbesocketConnectMgrService::findClientByName(string chname, list<shared_ptr<WebSocket>> &clients) { |
||||
|
lock_guard<mutex> lock(wsClientsLock); |
||||
|
auto it = wsClients.find(chname); |
||||
|
if (it == wsClients.end()) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
for (auto ch : it->second) { |
||||
|
clients.push_back(ch); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
json WbesocketConnectMgrService::getConnectionList() { |
||||
|
lock_guard<mutex> lock(wsClientsLock); |
||||
|
json j; |
||||
|
for (auto it : wsClients) { |
||||
|
json jlist; |
||||
|
jlist["channelName"] = it.first; |
||||
|
for (auto ch : it.second) { |
||||
|
json chj; |
||||
|
chj["uri"] = ch->getUrl(); |
||||
|
chj["ptr"] = (int64_t)(void *)ch.get(); |
||||
|
chj["remoteIp"] = findwsinfo(ch) ? findwsinfo(ch)->getRemoteIp() : "null"; |
||||
|
jlist["connections"].push_back(chj); |
||||
|
} |
||||
|
j.push_back(jlist); |
||||
|
} |
||||
|
return j; |
||||
|
} |
@ -0,0 +1,50 @@ |
|||||
|
//
|
||||
|
// Created by zwsd
|
||||
|
//
|
||||
|
|
||||
|
#pragma once
|
||||
|
#include <fstream>
|
||||
|
#include <iostream>
|
||||
|
#include <list>
|
||||
|
#include <map>
|
||||
|
#include <memory>
|
||||
|
#include <set>
|
||||
|
#include <sstream>
|
||||
|
#include <string>
|
||||
|
#include <vector>
|
||||
|
|
||||
|
//
|
||||
|
#include "components/zcanreceiver/zcanreceiverhost.hpp"
|
||||
|
#include "components/zservice_container/zservice_container.hpp"
|
||||
|
//
|
||||
|
#include "ixwebsocket/IXHttp.h"
|
||||
|
#include "ixwebsocket/IXHttpServer.h"
|
||||
|
#include "ixwebsocket/IXWebSocketServer.h"
|
||||
|
#include "spdlogfactory/logger.hpp"
|
||||
|
//
|
||||
|
//
|
||||
|
#include <mutex>
|
||||
|
|
||||
|
namespace iflytop { |
||||
|
using namespace std; |
||||
|
using namespace nlohmann; |
||||
|
using namespace ix; |
||||
|
|
||||
|
class WebSocketConnectInfo { |
||||
|
string remoteIp; |
||||
|
|
||||
|
public: |
||||
|
WebSocketConnectInfo() {} |
||||
|
WebSocketConnectInfo(string ip) : remoteIp(ip) {} |
||||
|
string getRemoteIp() { return remoteIp; } |
||||
|
}; |
||||
|
|
||||
|
class WbesocketConnectMgrService { |
||||
|
public: |
||||
|
static void insertNewClient(string chname, shared_ptr<WebSocket> client, shared_ptr<WebSocketConnectInfo> connectInfo); |
||||
|
static void removeClient(string chname, shared_ptr<WebSocket> client); |
||||
|
static void findClientByName(string chname, list<shared_ptr<WebSocket>> &clients); |
||||
|
static json getConnectionList(); |
||||
|
}; |
||||
|
|
||||
|
} // namespace iflytop
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue