-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathWebSocketServer.h
More file actions
87 lines (73 loc) · 2.78 KB
/
WebSocketServer.h
File metadata and controls
87 lines (73 loc) · 2.78 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
82
83
84
85
86
87
/** --------------------------------------------------------------------------
* WebSocketServer.h
*
* Base class that WebSocket implementations must inherit from. Handles the
* client connections and calls the child class callbacks for connection
* events like onConnect, onMessage, and onDisconnect.
*
* Author : Jason Kruse <jason@jasonkruse.com> or @mnisjk
* Copyright : 2014
* License : BSD (see LICENSE)
* --------------------------------------------------------------------------
**/
#ifndef _WEBSOCKETSERVER_H
#define _WEBSOCKETSERVER_H
#include <stdint.h>
#include <map>
#include <string>
#include <list>
#include <stdio.h>
#include <ctime>
#include <sys/time.h>
#include <iostream>
#include <sstream>
#include "libwebsockets.h"
using namespace std;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/// WebSocketServer
/// ---------
class WebSocketServer
{
public:
// Represents a client connection
struct Connection
{
list<const char*> buffer; // Ordered list of pending messages to flush out when socket is writable
map<string,string> keyValueMap;
time_t createTime;
};
// Manages connections. Unfortunately this is public because static callback for
// libwebsockets is defined outside the instance and needs access to it.
map<int,Connection*> connections;
// Constructor / Destructor
WebSocketServer( int port, const string certPath = "", const string& keyPath = "" );
~WebSocketServer( );
void run( uint64_t timeout = 50 );
void wait( uint64_t timeout = 50 );
void send( int socketID, string data );
void broadcast( string data );
// Key => value storage for each connection
string getValue( int socketID, const string& name );
void setValue( int socketID, const string& name, const string& value );
int getNumberOfConnections( );
// Overridden by children
virtual void onConnect( int socketID ) = 0;
virtual void onMessage( int socketID, const string& data ) = 0;
virtual void onDisconnect( int socketID ) = 0;
virtual void onError( int socketID, const string& message ) = 0;
// Wrappers, so we can take care of some maintenance
void onConnectWrapper( int socketID );
void onDisconnectWrapper( int socketID );
void onErrorWrapper( int socketID, const string& message );
protected:
// Nothing, yet.
private:
int _port;
string _keyPath;
string _certPath;
struct lws_context *_context;
void _removeConnection( int socketID );
};
// WebSocketServer.h
#endif