2019-05-17 07:41:39 +02:00
|
|
|
/*
|
|
|
|
* IXWebSocketMessageQueue.h
|
|
|
|
* Author: Korchynskyi Dmytro
|
|
|
|
* Copyright (c) 2017-2019 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "IXWebSocket.h"
|
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
2019-05-30 17:46:50 +02:00
|
|
|
#include <thread>
|
2019-05-17 07:41:39 +02:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// A helper class to dispatch websocket message callbacks in your thread.
|
|
|
|
//
|
|
|
|
class WebSocketMessageQueue
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebSocketMessageQueue(WebSocket* websocket = nullptr);
|
|
|
|
~WebSocketMessageQueue();
|
|
|
|
|
|
|
|
void bindWebsocket(WebSocket* websocket);
|
|
|
|
|
|
|
|
void setOnMessageCallback(const OnMessageCallback& callback);
|
|
|
|
void setOnMessageCallback(OnMessageCallback&& callback);
|
|
|
|
|
|
|
|
void poll(int count = 512);
|
|
|
|
|
|
|
|
protected:
|
2019-06-09 19:10:33 +02:00
|
|
|
WebSocketMessagePtr popMessage();
|
2019-05-17 07:41:39 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
WebSocket* _websocket = nullptr;
|
|
|
|
OnMessageCallback _onMessageUserCallback;
|
|
|
|
std::mutex _messagesMutex;
|
2019-06-09 19:10:33 +02:00
|
|
|
std::list<WebSocketMessagePtr> _messages;
|
2019-05-17 07:41:39 +02:00
|
|
|
};
|
2019-05-30 17:46:50 +02:00
|
|
|
} // namespace ix
|