Jamoma API  0.6.0.a19
TTWebSocket.h
1 /*
2  * @file TTWebSocket.h
3  * @group network library
4  * @brief Jamoma WebSocket server
5  * @see TTWebSend, TTWebReceive
6  *
7  * @copyright © 2013, Laurent Garnier
8  * License: This code is licensed under the terms of the "New BSD License"
9  * http://creativecommons.org/licenses/BSD/
10  *
11  * @details Use cross platform civetweb server from https://github.com/sunsetbrew/civetweb
12  * @details Some parts of this code are taken from ws_server example
13  */
14 
15 #ifndef __TT_WEB_SOCKET_H__
16 #define __TT_WEB_SOCKET_H__
17 
18 #include "TTFoundationAPI.h"
19 #include "civetweb.h"
20 
21 // simple structure for keeping track of websocket connection
22 struct ws_connection {
23  struct mg_connection *conn;
24  int update;
25  int closing;
26 };
27 
28 // time base and structure periodic updates to client for demo
29 #define BASETIME 100000 /* 0.1 seconds */
30 struct progress {
31  int limit;
32  int increment;
33  int period;
34  int value;
35 };
36 
37 // up to 16 independent client connections
38 #define CONNECTIONS 16
39 static struct ws_connection ws_conn[CONNECTIONS];
40 
41 // last connection
42 static struct mg_connection *mLastConnection;
43 
44 /**
45  * Create a websocket server.
46  */
48 {
49 
50 public:
51 
52  TTObjectBasePtr mOwner; ///< The object that instantiated this socket and will receive notifications.
53 
54 private:
55 
56  TTUInt16 mPort; ///< Not used in the case of a TTWebSend object, websocket server manage the communication itself.
57  TTString mHtmlPath; ///< Path of the index.html file.
58 
59  struct mg_context *mContext;
60 
61 public:
62 
63  /** Create a socket to receive websocket messages.
64  @param owner the TTObjectBase to notify
65  @param port the port number
66  @param htmlPath the path to index.html file
67  */
68  TTWebSocket(const TTObjectBasePtr owner, const TTUInt16 port, const TTString& htmlPath);
69 
70  /** Create a socket to send websocket messages.
71  */
72  TTWebSocket();
73 
74  virtual ~TTWebSocket();
75 
76  /** Bind websocket server.
77  */
78  TTErr bind();
79 
80  /** Send message
81  @param message message to send
82  */
83  TTErr SendMessage(TTSymbol& message);
84 };
86 
87 
88 #endif // __TT_WEB_SOCKET_H__
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
Create a websocket server.
Definition: TTWebSocket.h:47
TTWebSocket()
Create a socket to send websocket messages.
Base class for all first-class Jamoma objects.
Definition: TTObjectBase.h:109
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTObjectBasePtr mOwner
The object that instantiated this socket and will receive notifications.
Definition: TTWebSocket.h:52
TTErr bind()
Bind websocket server.
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
The TTString class is used to represent a string.
Definition: TTString.h:34
TTErr SendMessage(TTSymbol &message)
Send message.