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