5MP Motion Camera  1.1.1.1
A waterproof, low power, battery operated, motion activated, 5 mega-pixel, WiFi camera.
WebSocketClient.h
Go to the documentation of this file.
1 /*
2 WebSocketClient, a websocket implementation for Arduino
3 Copyright 2020 Patrick Moffitt
4 
5 Based on previous implementations by
6 Copyright 2015 William Blommaert
7 and
8 Copyright 2010 Ben Swanson
9 and
10 Copyright 2010 Randall Brewer
11 and
12 Copyright 2010 Oliver Smith
13 and
14 Copyright 2011 Branden Hall
15 and
16 Copyright 2011 Per Ejeklint
17 
18 Some code and concept based off of Webduino library
19 Copyright 2009 Ben Combee, Ran Talbott
20 
21 Permission is hereby granted, free of charge, to any person obtaining a copy
22 of this software and associated documentation files (the "Software"), to deal
23 in the Software without restriction, including without limitation the rights
24 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 copies of the Software, and to permit persons to whom the Software is
26 furnished to do so, subject to the following conditions:
27 
28 The above copyright notice and this permission notice shall be included in
29 all copies or substantial portions of the Software.
30 
31 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37 THE SOFTWARE.
38 */
39 
40 #ifndef WEBSOCKETCLIENT_H_
41 #define WEBSOCKETCLIENT_H_
42 
43 #include <Arduino.h>
44 #include <Stream.h>
45 #include "Client.h"
46 
47 // CRLF characters to terminate lines/handshakes in headers.
48 #define CRLF "\r\n"
49 
50 // Amount of time in ms to wait for server to have characters available after making a request.
51 #define AVAILABLE_TIMEOUT_MS 500
52 
53 // Amount of time (in ms) a user may be connected before getting disconnected
54 // for timing out (i.e. not sending any data to the server).
55 #define TIMEOUT_IN_MS 10000
56 
57 // ACTION_SPACE is how many actions are allowed in a program. Defaults to
58 // 5 unless overwritten by user.
59 #ifndef CALLBACK_FUNCTIONS
60 #define CALLBACK_FUNCTIONS 1
61 #endif
62 
63 // Don't allow the client to send big frames of data. This will flood the Arduinos
64 // memory and might even crash it.
65 #ifndef MAX_FRAME_LENGTH
66 #define MAX_FRAME_LENGTH 256
67 #endif
68 
69 #define SIZE(array) (sizeof(array) / sizeof(*array))
70 
71 // WebSocket protocol constants
72 // First byte
73 #define WS_FIN 0x80
74 #define WS_OPCODE_TEXT 0x1
75 #define WS_OPCODE_BINARY 0x2
76 #define WS_OPCODE_CLOSE 0x8
77 #define WS_OPCODE_PING 0x9
78 #define WS_OPCODE_PONG 0xa
79 // Second byte
80 #define WS_MASK 0x80
81 #define WS_SIZE16 126
82 #define WS_SIZE64 127
83 
85 public:
86 
87  // Handle connection requests to validate and process/refuse
88  // connections.
89  bool handshake(Client &client);
90 
91  // Get data off of the stream
92  String getData();
93 
94  // Write data to the stream
95  void sendData(const char *str);
96  void sendData(String str);
97  // Write data to the stream with frame masking, this is required!
98  void sendData(const char *str, uint8_t opcode);
99  void sendData(String str, uint8_t opcode);
100 
101  char *path;
102  char *host;
103  char *origin;
104 
105 private:
106  Client *socket_client;
107  unsigned long _startMillis;
108 
109  const char *socket_urlPrefix;
110 
111  // Discovers if the client's header is requesting an upgrade to a
112  // websocket connection.
113  bool analyzeRequest();
114 
115  String handleStream();
116 
117  // Disconnect user gracefully.
118  void disconnectStream();
119 
120  int timedRead();
121 
122  void sendEncodedData(char *str);
123  void sendEncodedData(String str);
124  // Write data to the stream with frame masking, this is required!
125  void sendEncodedData(char *str, uint8_t opcode);
126  void sendEncodedData(String str, uint8_t opcode);
127 };
128 #endif
Definition: WebSocketClient.h:84
char * path
Definition: WebSocketClient.h:101
void disconnectStream()
Definition: WebSocketClient.cpp:241
void sendEncodedData(char *str)
Definition: WebSocketClient.cpp:312
void sendData(const char *str)
Definition: WebSocketClient.cpp:264
char * origin
Definition: WebSocketClient.h:103
unsigned long _startMillis
Definition: WebSocketClient.h:107
bool handshake(Client &client)
Definition: WebSocketClient.cpp:7
int timedRead()
Definition: WebSocketClient.cpp:304
bool analyzeRequest()
Definition: WebSocketClient.cpp:38
String getData()
Definition: WebSocketClient.cpp:254
char * host
Definition: WebSocketClient.h:102
Client * socket_client
Definition: WebSocketClient.h:106
String handleStream()
Definition: WebSocketClient.cpp:137
const char * socket_urlPrefix
Definition: WebSocketClient.h:109