forked from ejeklint/ArduinoWebsocketServer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
WebSocket.h
executable file
·134 lines (98 loc) · 3.65 KB
/
WebSocket.h
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Websocket-Arduino, a simple websocket implementation for Arduino
Copyright 2012 Per Ejeklint
Based on previous implementations by
Copyright 2010 Ben Swanson
and
Copyright 2010 Randall Brewer
and
Copyright 2010 Oliver Smith
Some code and concept based off of Webduino library
Copyright 2009 Ben Combee, Ran Talbott
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-------------
Now based off version 13
http://datatracker.ietf.org/doc/rfc6455/?include_text=1
Modified by Alexandros Baltas, 2013
www.codebender.cc
*/
#include <Arduino.h> // Arduino 1.0 or greater is required
#include <stdlib.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#ifndef WEBSOCKET_H_
#define WEBSOCKET_H_
// CRLF characters to terminate lines/handshakes in headers.
#define CRLF "\r\n"
class WebSocket;
class WebSocketServer {
public:
// Constructor.
WebSocketServer(const char *urlPrefix = "/", int inPort = 80, byte maxConnections = 4);
// Callback functions definition.
typedef void DataCallback(WebSocket &socket, char *socketString, byte frameLength);
typedef void Callback(WebSocket &socket);
// Callbacks
void registerDataCallback(DataCallback *callback);
void registerConnectCallback(Callback *callback);
void registerDisconnectCallback(Callback *callback);
// Start tlistening for connections.
void begin();
// Main listener for incoming data. Should be called from the loop.
void listen();
// Connection count
byte connectionCount();
// Broadcast to all connected clients.
void send(char *str, byte length);
private:
WiFiServer m_server;
const char *m_socket_urlPrefix;
int m_maxConnections;
byte m_connectionCount;
// Pointer array of clients:
WebSocket **m_connections;
protected:
friend class WebSocket;
// Pointer to the callback function the user should provide
DataCallback *onData;
Callback *onConnect;
Callback *onDisconnect;
};
class WebSocket {
WebSocketServer *m_server;
public:
// Constructor.
WebSocket(WebSocketServer *server, WiFiClient cli);
// Are we connected?
bool isConnected();
// Embeds data in frame and sends to client.
bool send(char *str, byte length);
// Handle incoming data.
void listen();
private:
WiFiClient client;
enum State {DISCONNECTED, CONNECTED} state;
// Discovers if the client's header is requesting an upgrade to a
// websocket connection.
bool doHandshake();
// Reads a frame from client. Returns false if user disconnects,
// or unhandled frame is received. Server must then disconnect, or an error occurs.
bool getFrame();
// Disconnect user gracefully.
void disconnectStream();
};
#endif