WebSocket client library
Websocket.cpp@13:3b058372cad9, 2011-08-30 (annotated)
- Committer:
- samux
- Date:
- Tue Aug 30 09:25:28 2011 +0000
- Revision:
- 13:3b058372cad9
- Parent:
- 12:1f6b9451a608
- Child:
- 14:c5ac3e26998f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:21fe3b05249f | 1 | #include "Websocket.h" |
samux | 6:01a1eb7c0145 | 2 | #include <string> |
samux | 0:21fe3b05249f | 3 | |
samux | 6:01a1eb7c0145 | 4 | Websocket::Websocket(char * url, Wifly * wifi) { |
samux | 9:9fa055ed54b4 | 5 | this->wifi = wifi; |
samux | 9:9fa055ed54b4 | 6 | wifi_use = true; |
samux | 9:9fa055ed54b4 | 7 | eth_use = false; |
samux | 9:9fa055ed54b4 | 8 | response_server_eth = false; |
samux | 9:9fa055ed54b4 | 9 | fillFields(wifi_use, url); |
samux | 6:01a1eb7c0145 | 10 | } |
samux | 6:01a1eb7c0145 | 11 | |
samux | 13:3b058372cad9 | 12 | |
samux | 13:3b058372cad9 | 13 | Websocket::Websocket(char * url) { |
samux | 13:3b058372cad9 | 14 | wifi_use = false; |
samux | 13:3b058372cad9 | 15 | server_ip = NULL; |
samux | 13:3b058372cad9 | 16 | eth_use = true; |
samux | 13:3b058372cad9 | 17 | eth_writeable = false; |
samux | 13:3b058372cad9 | 18 | eth_readable = false; |
samux | 13:3b058372cad9 | 19 | eth_connected = false; |
samux | 13:3b058372cad9 | 20 | response_server_eth = false; |
samux | 13:3b058372cad9 | 21 | fillFields(wifi_use, url); |
samux | 13:3b058372cad9 | 22 | } |
samux | 13:3b058372cad9 | 23 | |
samux | 13:3b058372cad9 | 24 | |
samux | 6:01a1eb7c0145 | 25 | void Websocket::fillFields(bool wifi, char * url) { |
samux | 0:21fe3b05249f | 26 | char *res = NULL; |
samux | 6:01a1eb7c0145 | 27 | char *res1 = NULL; |
samux | 6:01a1eb7c0145 | 28 | |
samux | 0:21fe3b05249f | 29 | char buf[30]; |
samux | 0:21fe3b05249f | 30 | strcpy(buf, url); |
samux | 6:01a1eb7c0145 | 31 | |
samux | 0:21fe3b05249f | 32 | res = strtok(buf, ":"); |
samux | 9:9fa055ed54b4 | 33 | if (strcmp(res, "ws")) { |
samux | 0:21fe3b05249f | 34 | this->ip_domain = NULL; |
samux | 0:21fe3b05249f | 35 | this->path = NULL; |
samux | 6:01a1eb7c0145 | 36 | printf("\r\nFormat error: please use: \"ws://ip-or-domain[:port]/path\"\r\n\r\n"); |
samux | 9:9fa055ed54b4 | 37 | } else { |
samux | 6:01a1eb7c0145 | 38 | //ip_domain and port |
samux | 6:01a1eb7c0145 | 39 | res = strtok(NULL, "/"); |
samux | 6:01a1eb7c0145 | 40 | |
samux | 6:01a1eb7c0145 | 41 | //path |
samux | 6:01a1eb7c0145 | 42 | res1 = strtok(NULL, " "); |
samux | 6:01a1eb7c0145 | 43 | if (res1 != NULL) { |
samux | 6:01a1eb7c0145 | 44 | path = (char *) malloc (sizeof(char) * strlen(res1)); |
samux | 6:01a1eb7c0145 | 45 | strcpy(this->path, res1); |
samux | 6:01a1eb7c0145 | 46 | } |
samux | 6:01a1eb7c0145 | 47 | |
samux | 6:01a1eb7c0145 | 48 | //ip_domain |
samux | 6:01a1eb7c0145 | 49 | res = strtok(res, ":"); |
samux | 9:9fa055ed54b4 | 50 | |
samux | 7:b15978708360 | 51 | //port |
samux | 7:b15978708360 | 52 | res1 = strtok(NULL, " "); |
samux | 7:b15978708360 | 53 | //port |
samux | 7:b15978708360 | 54 | if (res1 != NULL) { |
samux | 7:b15978708360 | 55 | port = (char *) malloc (sizeof(char) * strlen(res1)); |
samux | 7:b15978708360 | 56 | strcpy(this->port, res1); |
samux | 7:b15978708360 | 57 | } else { |
samux | 7:b15978708360 | 58 | port = (char *) malloc (sizeof(char) * 3); |
samux | 7:b15978708360 | 59 | strcpy(this->port, "80"); |
samux | 7:b15978708360 | 60 | } |
samux | 9:9fa055ed54b4 | 61 | |
samux | 6:01a1eb7c0145 | 62 | if (res != NULL) { |
samux | 0:21fe3b05249f | 63 | ip_domain = (char *) malloc (sizeof(char) * strlen(res)); |
samux | 0:21fe3b05249f | 64 | strcpy(this->ip_domain, res); |
samux | 6:01a1eb7c0145 | 65 | //if we use ethernet, we must decode ip address or use dnsresolver |
samux | 6:01a1eb7c0145 | 66 | if (!wifi) { |
samux | 6:01a1eb7c0145 | 67 | strcpy(buf, res); |
samux | 12:1f6b9451a608 | 68 | |
samux | 6:01a1eb7c0145 | 69 | //we try to decode the ip address |
samux | 6:01a1eb7c0145 | 70 | if (buf[0] >= '0' && buf[0] <= '9') { |
samux | 6:01a1eb7c0145 | 71 | res = strtok(buf, "."); |
samux | 6:01a1eb7c0145 | 72 | int i = 0; |
samux | 6:01a1eb7c0145 | 73 | int ip[4]; |
samux | 6:01a1eb7c0145 | 74 | while (res != NULL) { |
samux | 6:01a1eb7c0145 | 75 | ip[i] = atoi(res); |
samux | 6:01a1eb7c0145 | 76 | res = strtok(NULL, "."); |
samux | 6:01a1eb7c0145 | 77 | i++; |
samux | 6:01a1eb7c0145 | 78 | } |
samux | 6:01a1eb7c0145 | 79 | server_ip = IpAddr(ip[0], ip[1], ip[2], ip[3]); |
samux | 12:1f6b9451a608 | 80 | printf("server without dns=%i.%i.%i.%i\n",server_ip[0],server_ip[1],server_ip[2],server_ip[3]); |
samux | 6:01a1eb7c0145 | 81 | } |
samux | 6:01a1eb7c0145 | 82 | } |
samux | 0:21fe3b05249f | 83 | } |
samux | 0:21fe3b05249f | 84 | } |
samux | 0:21fe3b05249f | 85 | } |
samux | 0:21fe3b05249f | 86 | |
samux | 6:01a1eb7c0145 | 87 | |
samux | 6:01a1eb7c0145 | 88 | bool Websocket::connect() { |
samux | 0:21fe3b05249f | 89 | char cmd[50]; |
samux | 6:01a1eb7c0145 | 90 | if ( wifi_use ) { |
samux | 6:01a1eb7c0145 | 91 | wifi->Send("exit\r", "NO"); |
samux | 6:01a1eb7c0145 | 92 | //enter in cmd mode |
samux | 6:01a1eb7c0145 | 93 | while (!wifi->Send("$$$", "CMD")) { |
samux | 6:01a1eb7c0145 | 94 | printf("cannot enter in CMD mode\r\n"); |
samux | 6:01a1eb7c0145 | 95 | wifi->exit(); |
samux | 6:01a1eb7c0145 | 96 | } |
samux | 6:01a1eb7c0145 | 97 | |
samux | 6:01a1eb7c0145 | 98 | |
samux | 6:01a1eb7c0145 | 99 | //open the connection |
samux | 6:01a1eb7c0145 | 100 | sprintf(cmd, "open %s %s\r\n", ip_domain, port); |
samux | 6:01a1eb7c0145 | 101 | if (!wifi->Send(cmd, "OPEN")) { |
samux | 6:01a1eb7c0145 | 102 | printf("Websocket::connect cannot open\r\n"); |
samux | 6:01a1eb7c0145 | 103 | return false; |
samux | 6:01a1eb7c0145 | 104 | } |
samux | 6:01a1eb7c0145 | 105 | |
samux | 6:01a1eb7c0145 | 106 | |
samux | 6:01a1eb7c0145 | 107 | //send websocket HTTP header |
samux | 6:01a1eb7c0145 | 108 | sprintf(cmd, "GET /%s HTTP/1.1\r\n", path); |
samux | 6:01a1eb7c0145 | 109 | wifi->Send(cmd, "NO"); |
samux | 6:01a1eb7c0145 | 110 | |
samux | 6:01a1eb7c0145 | 111 | sprintf(cmd, "Host: %s:%s\r\n", ip_domain, port); |
samux | 6:01a1eb7c0145 | 112 | wifi->Send(cmd, "NO"); |
samux | 6:01a1eb7c0145 | 113 | |
samux | 6:01a1eb7c0145 | 114 | wifi->Send("Upgrade: WebSocket\r\n", "NO"); |
samux | 6:01a1eb7c0145 | 115 | |
samux | 6:01a1eb7c0145 | 116 | sprintf(cmd, "Origin: http:%s:%s\r\n", ip_domain, port); |
samux | 6:01a1eb7c0145 | 117 | wifi->Send(cmd, "NO"); |
samux | 6:01a1eb7c0145 | 118 | |
samux | 6:01a1eb7c0145 | 119 | |
samux | 6:01a1eb7c0145 | 120 | wifi->Send("Connection: Upgrade\r\n", "NO"); |
samux | 6:01a1eb7c0145 | 121 | wifi->Send("Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n", "NO"); |
samux | 6:01a1eb7c0145 | 122 | wifi->Send("Sec-WebSocket-key2: 12998 5 Y3 1 .P00\r\n\r\n", "NO"); |
samux | 6:01a1eb7c0145 | 123 | if (!wifi->Send("^n:ds[4U", "8jKS'y:G*Co,Wxa-")) |
samux | 6:01a1eb7c0145 | 124 | return false; |
samux | 6:01a1eb7c0145 | 125 | |
samux | 6:01a1eb7c0145 | 126 | printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain, this->path, this->port); |
samux | 6:01a1eb7c0145 | 127 | return true; |
samux | 6:01a1eb7c0145 | 128 | } else if ( eth_use ) { |
samux | 6:01a1eb7c0145 | 129 | int i = 0; |
samux | 6:01a1eb7c0145 | 130 | |
samux | 6:01a1eb7c0145 | 131 | EthernetErr ethErr = eth.setup(); |
samux | 6:01a1eb7c0145 | 132 | if (ethErr) { |
samux | 6:01a1eb7c0145 | 133 | printf("Error %d in setup.\r\n", ethErr); |
samux | 6:01a1eb7c0145 | 134 | return false; |
samux | 6:01a1eb7c0145 | 135 | } |
samux | 12:1f6b9451a608 | 136 | |
samux | 12:1f6b9451a608 | 137 | //we must use dnsresolver to find the ip address |
samux | 12:1f6b9451a608 | 138 | if (server_ip == NULL) { |
samux | 12:1f6b9451a608 | 139 | DNSResolver dr; |
samux | 12:1f6b9451a608 | 140 | server_ip = dr.resolveName(ip_domain); |
samux | 12:1f6b9451a608 | 141 | printf("server with dns=%i.%i.%i.%i\r\n",server_ip[0],server_ip[1],server_ip[2],server_ip[3]); |
samux | 12:1f6b9451a608 | 142 | } |
samux | 6:01a1eb7c0145 | 143 | |
samux | 6:01a1eb7c0145 | 144 | IpAddr ipt = eth.getIp(); |
samux | 6:01a1eb7c0145 | 145 | printf("mbed IP Address is %d.%d.%d.%d\r\n", ipt[0], ipt[1], ipt[2], ipt[3]); |
samux | 6:01a1eb7c0145 | 146 | |
samux | 6:01a1eb7c0145 | 147 | sock.setOnEvent(this, &Websocket::onTCPSocketEvent); |
samux | 6:01a1eb7c0145 | 148 | Host server (server_ip, atoi(port)); |
samux | 6:01a1eb7c0145 | 149 | TCPSocketErr bindErr = sock.connect(server); |
samux | 6:01a1eb7c0145 | 150 | if (bindErr) { |
samux | 12:1f6b9451a608 | 151 | printf("binderr: %d\r\n", bindErr); |
samux | 6:01a1eb7c0145 | 152 | return false; |
samux | 6:01a1eb7c0145 | 153 | } |
samux | 6:01a1eb7c0145 | 154 | |
samux | 6:01a1eb7c0145 | 155 | |
samux | 6:01a1eb7c0145 | 156 | Timer tmr; |
samux | 6:01a1eb7c0145 | 157 | tmr.start(); |
samux | 0:21fe3b05249f | 158 | |
samux | 6:01a1eb7c0145 | 159 | while (true) { |
samux | 6:01a1eb7c0145 | 160 | Net::poll(); |
samux | 12:1f6b9451a608 | 161 | if (tmr.read() > 0.01) { |
samux | 6:01a1eb7c0145 | 162 | tmr.reset(); |
samux | 6:01a1eb7c0145 | 163 | if (eth_connected) { |
samux | 6:01a1eb7c0145 | 164 | switch (i) { |
samux | 6:01a1eb7c0145 | 165 | case 0: |
samux | 6:01a1eb7c0145 | 166 | sprintf(cmd, "GET /%s HTTP/1.1\r\n", path); |
samux | 6:01a1eb7c0145 | 167 | sock.send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 168 | i++; |
samux | 6:01a1eb7c0145 | 169 | break; |
samux | 6:01a1eb7c0145 | 170 | case 1: |
samux | 6:01a1eb7c0145 | 171 | sprintf(cmd, "Host: %s:%s\r\n", ip_domain, port); |
samux | 6:01a1eb7c0145 | 172 | sock.send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 173 | i++; |
samux | 6:01a1eb7c0145 | 174 | break; |
samux | 6:01a1eb7c0145 | 175 | case 2: |
samux | 6:01a1eb7c0145 | 176 | sprintf(cmd, "Upgrade: WebSocket\r\n"); |
samux | 6:01a1eb7c0145 | 177 | sock.send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 178 | i++; |
samux | 6:01a1eb7c0145 | 179 | break; |
samux | 6:01a1eb7c0145 | 180 | case 3: |
samux | 6:01a1eb7c0145 | 181 | sprintf(cmd, "Origin: http:%s:%s\r\n", ip_domain, port); |
samux | 6:01a1eb7c0145 | 182 | sock.send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 183 | i++; |
samux | 6:01a1eb7c0145 | 184 | break; |
samux | 6:01a1eb7c0145 | 185 | case 4: |
samux | 6:01a1eb7c0145 | 186 | sprintf(cmd, "Connection: Upgrade\r\n"); |
samux | 6:01a1eb7c0145 | 187 | sock.send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 188 | i++; |
samux | 6:01a1eb7c0145 | 189 | break; |
samux | 6:01a1eb7c0145 | 190 | case 5: |
samux | 6:01a1eb7c0145 | 191 | sprintf(cmd, "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n"); |
samux | 6:01a1eb7c0145 | 192 | sock.send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 193 | i++; |
samux | 6:01a1eb7c0145 | 194 | break; |
samux | 6:01a1eb7c0145 | 195 | case 6: |
samux | 6:01a1eb7c0145 | 196 | sprintf(cmd, "Sec-WebSocket-key2: 12998 5 Y3 1 .P00\r\n\r\n"); |
samux | 6:01a1eb7c0145 | 197 | sock.send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 198 | i++; |
samux | 6:01a1eb7c0145 | 199 | break; |
samux | 6:01a1eb7c0145 | 200 | case 7: |
samux | 6:01a1eb7c0145 | 201 | sock.send("^n:ds[4U", 8); |
samux | 6:01a1eb7c0145 | 202 | i++; |
samux | 6:01a1eb7c0145 | 203 | break; |
samux | 6:01a1eb7c0145 | 204 | case 8: |
samux | 9:9fa055ed54b4 | 205 | if (response_server_eth) |
samux | 9:9fa055ed54b4 | 206 | i++; |
samux | 9:9fa055ed54b4 | 207 | else |
samux | 9:9fa055ed54b4 | 208 | break; |
samux | 9:9fa055ed54b4 | 209 | |
samux | 6:01a1eb7c0145 | 210 | default: |
samux | 6:01a1eb7c0145 | 211 | break; |
samux | 6:01a1eb7c0145 | 212 | } |
samux | 6:01a1eb7c0145 | 213 | } |
samux | 9:9fa055ed54b4 | 214 | if (i==9) { |
samux | 6:01a1eb7c0145 | 215 | printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain, this->path, this->port); |
samux | 6:01a1eb7c0145 | 216 | return true; |
samux | 6:01a1eb7c0145 | 217 | } |
samux | 6:01a1eb7c0145 | 218 | } |
samux | 6:01a1eb7c0145 | 219 | } |
samux | 6:01a1eb7c0145 | 220 | |
samux | 0:21fe3b05249f | 221 | } |
samux | 0:21fe3b05249f | 222 | return true; |
samux | 0:21fe3b05249f | 223 | } |
samux | 0:21fe3b05249f | 224 | |
samux | 6:01a1eb7c0145 | 225 | void Websocket::Send(char * str) { |
samux | 0:21fe3b05249f | 226 | char cmd[100]; |
samux | 6:01a1eb7c0145 | 227 | if (wifi_use) { |
samux | 6:01a1eb7c0145 | 228 | wifi->putc('\x00'); |
samux | 6:01a1eb7c0145 | 229 | sprintf(cmd, "%s%c", str, '\xff'); |
samux | 6:01a1eb7c0145 | 230 | wifi->Send(cmd, "NO"); |
samux | 6:01a1eb7c0145 | 231 | } else if (eth_use) { |
samux | 6:01a1eb7c0145 | 232 | sprintf(cmd, "%c%s%c", 0x00, str, 0xff); |
samux | 6:01a1eb7c0145 | 233 | Net::poll(); |
samux | 6:01a1eb7c0145 | 234 | sock.send(cmd, strlen(cmd + 1) + 1); |
samux | 6:01a1eb7c0145 | 235 | } |
samux | 0:21fe3b05249f | 236 | } |
samux | 0:21fe3b05249f | 237 | |
samux | 6:01a1eb7c0145 | 238 | bool Websocket::read(char * message) { |
samux | 0:21fe3b05249f | 239 | int i = 0; |
samux | 6:01a1eb7c0145 | 240 | |
samux | 9:9fa055ed54b4 | 241 | if (wifi_use) { |
samux | 9:9fa055ed54b4 | 242 | char char_read; |
samux | 9:9fa055ed54b4 | 243 | if (!wifi->readable()) { |
samux | 9:9fa055ed54b4 | 244 | message = NULL; |
samux | 9:9fa055ed54b4 | 245 | return false; |
samux | 9:9fa055ed54b4 | 246 | } |
samux | 9:9fa055ed54b4 | 247 | |
samux | 9:9fa055ed54b4 | 248 | if (wifi->getc() != 0x00) { |
samux | 9:9fa055ed54b4 | 249 | message = NULL; |
samux | 9:9fa055ed54b4 | 250 | return false; |
samux | 9:9fa055ed54b4 | 251 | } |
samux | 6:01a1eb7c0145 | 252 | |
samux | 9:9fa055ed54b4 | 253 | while ( (char_read = wifi->getc()) != 0xff) |
samux | 9:9fa055ed54b4 | 254 | message[i++] = char_read; |
samux | 9:9fa055ed54b4 | 255 | message[i] = 0; |
samux | 9:9fa055ed54b4 | 256 | } else if (eth_use) { |
samux | 9:9fa055ed54b4 | 257 | |
samux | 9:9fa055ed54b4 | 258 | if (eth_rx[0] != 0x00) { |
samux | 9:9fa055ed54b4 | 259 | message = NULL; |
samux | 9:9fa055ed54b4 | 260 | return false; |
samux | 9:9fa055ed54b4 | 261 | } |
samux | 9:9fa055ed54b4 | 262 | while (eth_rx[i + 1] != 0xff) { |
samux | 9:9fa055ed54b4 | 263 | message[i] = eth_rx[i + 1]; |
samux | 9:9fa055ed54b4 | 264 | i++; |
samux | 9:9fa055ed54b4 | 265 | } |
samux | 9:9fa055ed54b4 | 266 | message[i] = 0; |
samux | 0:21fe3b05249f | 267 | } |
samux | 0:21fe3b05249f | 268 | return true; |
samux | 0:21fe3b05249f | 269 | } |
samux | 0:21fe3b05249f | 270 | |
samux | 6:01a1eb7c0145 | 271 | bool Websocket::close() { |
samux | 9:9fa055ed54b4 | 272 | if (wifi_use) { |
samux | 9:9fa055ed54b4 | 273 | if (!wifi->CmdMode()) { |
samux | 9:9fa055ed54b4 | 274 | printf("Websocket::close: cannot enter in cmd mode\r\n"); |
samux | 9:9fa055ed54b4 | 275 | return false; |
samux | 9:9fa055ed54b4 | 276 | } |
samux | 6:01a1eb7c0145 | 277 | |
samux | 9:9fa055ed54b4 | 278 | wifi->Send("close\r", "NO"); |
samux | 6:01a1eb7c0145 | 279 | |
samux | 9:9fa055ed54b4 | 280 | if (!wifi->exit()) |
samux | 9:9fa055ed54b4 | 281 | return false; |
samux | 9:9fa055ed54b4 | 282 | } else if (eth_use) { |
samux | 9:9fa055ed54b4 | 283 | if (sock.close()) |
samux | 9:9fa055ed54b4 | 284 | return false; |
samux | 9:9fa055ed54b4 | 285 | } |
samux | 0:21fe3b05249f | 286 | return true; |
samux | 0:21fe3b05249f | 287 | } |
samux | 0:21fe3b05249f | 288 | |
samux | 0:21fe3b05249f | 289 | |
samux | 0:21fe3b05249f | 290 | |
samux | 6:01a1eb7c0145 | 291 | bool Websocket::connected() { |
samux | 6:01a1eb7c0145 | 292 | if (wifi_use) { |
samux | 6:01a1eb7c0145 | 293 | char str[10]; |
samux | 6:01a1eb7c0145 | 294 | |
samux | 6:01a1eb7c0145 | 295 | wait(0.25); |
samux | 6:01a1eb7c0145 | 296 | if (!wifi->CmdMode()) { |
samux | 6:01a1eb7c0145 | 297 | printf("Websocket::connected: cannot enter in cmd mode\r\n"); |
samux | 0:21fe3b05249f | 298 | return false; |
samux | 3:9b00db719afa | 299 | } |
samux | 6:01a1eb7c0145 | 300 | wait(0.25); |
samux | 6:01a1eb7c0145 | 301 | |
samux | 6:01a1eb7c0145 | 302 | wifi->Send("show c\r\n", "NO"); |
samux | 6:01a1eb7c0145 | 303 | wifi->read(str); |
samux | 6:01a1eb7c0145 | 304 | |
samux | 6:01a1eb7c0145 | 305 | if (str[3] == '1') { |
samux | 6:01a1eb7c0145 | 306 | if (!wifi->exit()) { |
samux | 6:01a1eb7c0145 | 307 | printf("Websocket::connected: cannot exit\r\n"); |
samux | 6:01a1eb7c0145 | 308 | return false; |
samux | 6:01a1eb7c0145 | 309 | } |
samux | 6:01a1eb7c0145 | 310 | return true; |
samux | 6:01a1eb7c0145 | 311 | } |
samux | 6:01a1eb7c0145 | 312 | if (!wifi->exit()) |
samux | 6:01a1eb7c0145 | 313 | printf("Websocket::connected: cannot exit\r\n"); |
samux | 6:01a1eb7c0145 | 314 | return false; |
samux | 9:9fa055ed54b4 | 315 | } else if (eth_use) |
samux | 6:01a1eb7c0145 | 316 | return eth_connected; |
samux | 6:01a1eb7c0145 | 317 | |
samux | 6:01a1eb7c0145 | 318 | return true; |
samux | 6:01a1eb7c0145 | 319 | } |
samux | 6:01a1eb7c0145 | 320 | |
samux | 6:01a1eb7c0145 | 321 | |
samux | 6:01a1eb7c0145 | 322 | |
samux | 6:01a1eb7c0145 | 323 | |
samux | 6:01a1eb7c0145 | 324 | |
samux | 6:01a1eb7c0145 | 325 | void Websocket::onTCPSocketEvent(TCPSocketEvent e) { |
samux | 9:9fa055ed54b4 | 326 | if (e == TCPSOCKET_CONNECTED) { |
samux | 9:9fa055ed54b4 | 327 | eth_connected = true; |
samux | 9:9fa055ed54b4 | 328 | printf("TCP Socket Connected\r\n"); |
samux | 12:1f6b9451a608 | 329 | } else if (e == TCPSOCKET_WRITEABLE) { |
samux | 12:1f6b9451a608 | 330 | } else if (e == TCPSOCKET_READABLE) { |
samux | 9:9fa055ed54b4 | 331 | int len = sock.recv(eth_rx, 512); |
samux | 9:9fa055ed54b4 | 332 | eth_rx[len] = 0; |
samux | 9:9fa055ed54b4 | 333 | if (!response_server_eth) { |
samux | 9:9fa055ed54b4 | 334 | string checking; |
samux | 9:9fa055ed54b4 | 335 | size_t found = string::npos; |
samux | 9:9fa055ed54b4 | 336 | checking = eth_rx; |
samux | 9:9fa055ed54b4 | 337 | found = checking.find("HTTP"); |
samux | 9:9fa055ed54b4 | 338 | if (found != string::npos) |
samux | 9:9fa055ed54b4 | 339 | response_server_eth = true; |
samux | 9:9fa055ed54b4 | 340 | } |
samux | 12:1f6b9451a608 | 341 | } else { |
samux | 9:9fa055ed54b4 | 342 | printf("TCP Socket Fail\r\n"); |
samux | 9:9fa055ed54b4 | 343 | eth_connected = false; |
samux | 0:21fe3b05249f | 344 | } |
samux | 6:01a1eb7c0145 | 345 | } |
samux | 6:01a1eb7c0145 | 346 | |
samux | 6:01a1eb7c0145 | 347 |