-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSockets.h
More file actions
81 lines (69 loc) · 2.15 KB
/
WebSockets.h
File metadata and controls
81 lines (69 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef _MONGOOSE_WEBSOCKETS_H
#define _MONGOOSE_WEBSOCKETS_H
#include <map>
#include <iostream>
#include <mongoose.h>
#include "WebSocket.h"
#include "Mutex.h"
using namespace std;
/**
* WebSockets is an array that contains WebSocket connections, this
* can be used for instance to broadcast informations to them.
*
* The function clean() allow to remove closed connections from the
* array.
*/
namespace Mongoose
{
class WebSockets
{
public:
/**
* Creates a websockets array, the responsible false specify whether the
* container will be responsible for cleaning the websocket
*/
WebSockets(bool responsible = false);
virtual ~WebSockets();
/**
* Adds the given websocket to the poll. This container will not
* become responsible for cleaning this object
*
* @param WebSocket* the websocket object
*/
void add(WebSocket *websocket);
/**
* Send data to all sockets in this container
*/
void sendAll(string data);
/**
* Gets the websocket corresponding to the given connection
*
* @param strut mg_connection* the mongoose connection
*/
WebSocket *getWebSocket(struct mg_connection *connection);
/**
* Cleans all the connections that are hold in this object and that are
* closed
*/
void clean();
/**
* Removes the websocket from the container
*
* @param WebSocket* the websocket object
*/
void remove(WebSocket *websocket, bool lock = true);
/**
* Gets the websockets having the id
*
* @param int id
*/
WebSocket *getWebSocket(int id);
protected:
Mutex mutex;
map<struct mg_connection*, WebSocket*> websockets;
map<int, WebSocket*> websocketsById;
bool responsible;
int id;
};
}
#endif