Skip to content

Broadcast messages through websocket. #335

Answered by ithewei
komnotmr asked this question in Q&A
Discussion options

You must be logged in to vote
#include "WebSocketServer.h"

using namespace hv;

class ChannelMgr {
public:
    void addChannel(const WebSocketChannelPtr& channel) {
        std::lock_guard<std::mutex> locker(mutex_);
        channels[channel->id()] = channel;
    }

    void removeChannel(const WebSocketChannelPtr& channel) {
        std::lock_guard<std::mutex> locker(mutex_);
        channels.erase(channel->id());
    }

    int foreachChannel(std::function<void(const WebSocketChannelPtr& channel)> fn) {
        std::lock_guard<std::mutex> locker(mutex_);
        for (auto& pair : channels) {
            fn(pair.second);
        }
        return channels.size();
    }

    // broadcast thread-safe
    int broadcast(c…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by komnotmr
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants