L3D Cube Library
 All Classes Functions Pages
SparkWebSocketServer.h
1 /*
2 Websocket-Arduino, a websocket implementation for Arduino
3 Copyright 2014 NaAl (h20@alocreative.com)
4 Based on previous implementations by
5 Copyright 2011 Per Ejeklint
6 and
7 Copyright 2010 Ben Swanson
8 and
9 Copyright 2010 Randall Brewer
10 and
11 Copyright 2010 Oliver Smith
12 
13 Some code and concept based off of Webduino library
14 Copyright 2009 Ben Combee, Ran Talbott
15 
16 Permission is hereby granted, free of charge, to any person obtaining a copy
17 of this software and associated documentation files (the "Software"), to deal
18 in the Software without restriction, including without limitation the rights
19 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 copies of the Software, and to permit persons to whom the Software is
21 furnished to do so, subject to the following conditions:
22 
23 The above copyright notice and this permission notice shall be included in
24 all copies or substantial portions of the Software.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 THE SOFTWARE.
33 
34 -------------
35 Now based off
36 http://www.whatwg.org/specs/web-socket-protocol/
37 
38 - OLD -
39 Currently based off of "The Web Socket protocol" draft (v 75):
40 http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75
41  */
42 
43 #ifndef _SPARK_WEB_SOCKET_H_
44 #define _SPARK_WEB_SOCKET_H_
45 
46 #include "application.h"
47 #include "spark_utilities.h"
48 
49 #define CRLF "\r\n"
50 
51 #define HB_INTERVAL 2500
52 #define TIMEOUT 5000
53 
54 #ifndef CALLBACK_FUNCTIONS
55 #define CALLBACK_FUNCTIONS 1
56 #endif
57 
63 typedef void (*CallBack)(String&, String&);
64 
66  public:
67  SparkWebSocketServer(TCPServer &server);
68 
69  void setCallBack(CallBack &callBack){
70  cBack = callBack;
71  }
72 
73  bool handshake(TCPClient &client);
74 
75  bool getData(String &data, TCPClient &client);
76 
77  void sendData(const char *str, TCPClient &client);
78  void sendData(String str, TCPClient &client);
79 
80  void doIt();
81 
82  CallBack cBack;
83 
84  private:
85  const int packetLen = 520;
86  const int dataLen = packetLen - 8; // length bytes and mask
87 
88  unsigned long lastBeatTime;
89  unsigned long lastContactTime;
90  TCPServer* server;
91  TCPClient* source;
92 
93  String origin;
94  String host;
95 
96  bool analyzeRequest(TCPClient &client);
97  bool handleStream(String &data, TCPClient &client);
98  int packetHealth(char* buffer);
99 
100  void disconnectClient(void);
101 
102  int checkedRead(TCPClient &client);
103 
104  void sendEncodedData(char *str, TCPClient &client);
105  void sendEncodedData(String str, TCPClient &client);
106 };
107 
108 #endif
Definition: SparkWebSocketServer.h:65
bool getData(String &data, TCPClient &client)
Read data from client.
Definition: SparkWebSocketServer.cpp:131
void sendData(const char *str, TCPClient &client)
Send a string to a client.
Definition: SparkWebSocketServer.cpp:217