IXWebSocket/ixwebsocket/IXWebSocketMessageQueue.h

53 lines
1.2 KiB
C
Raw Normal View History

2019-05-08 21:02:56 +02:00
/*
2019-05-08 23:16:37 +02:00
* IXWebSocketMessageQueue.h
2019-05-08 21:02:56 +02:00
* Author: Korchynskyi Dmytro
2019-05-08 23:16:37 +02:00
* Copyright (c) 2017-2019 Machine Zone, Inc. All rights reserved.
2019-05-08 21:02:56 +02:00
*/
#pragma once
#include "IXWebSocket.h"
#include <thread>
#include <list>
#include <memory>
namespace ix
{
2019-05-08 23:16:37 +02:00
//
// A helper class to dispatch websocket message callbacks in your thread.
//
2019-05-08 23:09:51 +02:00
class WebSocketMessageQueue
2019-05-08 21:02:56 +02:00
{
public:
2019-05-08 23:09:51 +02:00
WebSocketMessageQueue(WebSocket* websocket = nullptr);
~WebSocketMessageQueue();
2019-05-08 21:02:56 +02:00
void bindWebsocket(WebSocket* websocket);
void setOnMessageCallback(const OnMessageCallback& callback);
void poll(int count = 512);
protected:
struct Message
{
WebSocketMessageType type;
std::string str;
size_t wireSize;
WebSocketErrorInfo errorInfo;
WebSocketOpenInfo openInfo;
WebSocketCloseInfo closeInfo;
};
using MessageDataPtr = std::shared_ptr<Message>;
MessageDataPtr popMessage();
private:
WebSocket* _websocket = nullptr;
OnMessageCallback _onMessageUserCallback;
std::mutex _messagesMutex;
std::list<MessageDataPtr> _messages;
};
}