WebSocket client library
Websocket.cpp
- Committer:
- samux
- Date:
- 2011-09-06
- Revision:
- 15:79bfbc0ad6bc
- Parent:
- 14:c5ac3e26998f
- Child:
- 16:d4518b50f653
File content as of revision 15:79bfbc0ad6bc:
#include "Websocket.h" #include <string> Websocket::Websocket(char * url, Wifly * wifi) { this->wifi = wifi; wifi_use = true; eth_use = false; response_server_eth = false; fillFields(wifi_use, url); } Websocket::Websocket(char * url) { wifi_use = false; server_ip = NULL; eth_use = true; eth_writeable = false; eth_readable = false; eth_connected = false; response_server_eth = false; fillFields(wifi_use, url); EthernetErr ethErr = eth.setup(); if (ethErr) { printf("\r\nERROR %d in setup.\r\n", ethErr); } //we must use dnsresolver to find the ip address if (server_ip == NULL) { DNSResolver dr; server_ip = dr.resolveName(ip_domain); printf("\r\nserver with dns=%i.%i.%i.%i\r\n",server_ip[0],server_ip[1],server_ip[2],server_ip[3]); } IpAddr ipt = eth.getIp(); printf("\r\nmbed IP Address is %d.%d.%d.%d\r\n", ipt[0], ipt[1], ipt[2], ipt[3]); sock.setOnEvent(this, &Websocket::onTCPSocketEvent); } void Websocket::fillFields(bool wifi, char * url) { char *res = NULL; char *res1 = NULL; char buf[30]; strcpy(buf, url); res = strtok(buf, ":"); if (strcmp(res, "ws")) { this->ip_domain = NULL; this->path = NULL; printf("\r\nFormat error: please use: \"ws://ip-or-domain[:port]/path\"\r\n\r\n"); } else { //ip_domain and port res = strtok(NULL, "/"); //path res1 = strtok(NULL, " "); if (res1 != NULL) { path = (char *) malloc (sizeof(char) * strlen(res1)); strcpy(this->path, res1); } //ip_domain res = strtok(res, ":"); //port res1 = strtok(NULL, " "); //port if (res1 != NULL) { port = (char *) malloc (sizeof(char) * strlen(res1)); strcpy(this->port, res1); } else { port = (char *) malloc (sizeof(char) * 3); strcpy(this->port, "80"); } if (res != NULL) { ip_domain = (char *) malloc (sizeof(char) * strlen(res)); strcpy(this->ip_domain, res); //if we use ethernet, we must decode ip address or use dnsresolver if (!wifi) { strcpy(buf, res); //we try to decode the ip address if (buf[0] >= '0' && buf[0] <= '9') { res = strtok(buf, "."); int i = 0; int ip[4]; while (res != NULL) { ip[i] = atoi(res); res = strtok(NULL, "."); i++; } server_ip = IpAddr(ip[0], ip[1], ip[2], ip[3]); printf("server without dns=%i.%i.%i.%i\n",server_ip[0],server_ip[1],server_ip[2],server_ip[3]); } } } } } bool Websocket::connect() { char cmd[50]; if ( wifi_use ) { wifi->Send("exit\r", "NO"); //enter in cmd mode while (!wifi->Send("$$$", "CMD")) { printf("cannot enter in CMD mode\r\n"); wifi->exit(); } //open the connection sprintf(cmd, "open %s %s\r\n", ip_domain, port); if (!wifi->Send(cmd, "OPEN*")) { printf("Websocket::connect cannot open\r\n"); return false; } //send websocket HTTP header sprintf(cmd, "GET /%s HTTP/1.1\r\n", path); wifi->Send(cmd, "NO"); sprintf(cmd, "Host: %s:%s\r\n", ip_domain, port); wifi->Send(cmd, "NO"); wifi->Send("Upgrade: WebSocket\r\n", "NO"); sprintf(cmd, "Origin: http:%s:%s\r\n", ip_domain, port); wifi->Send(cmd, "NO"); wifi->Send("Connection: Upgrade\r\n", "NO"); wifi->Send("Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n", "NO"); wifi->Send("Sec-WebSocket-key2: 12998 5 Y3 1 .P00\r\n\r\n", "NO"); if (!wifi->Send("^n:ds[4U", "8jKS'y:G*Co,Wxa-")) return false; printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain, this->path, this->port); return true; } else if ( eth_use ) { Host server (server_ip, atoi(port)); sock.close(); TCPSocketErr bindErr = sock.connect(server); if (bindErr) { printf("\r\nERROR binderr: %d\r\n", bindErr); return false; } Timer tmr; tmr.start(); Timer stop; stop.start(); int i = 0; while (true) { Net::poll(); if (stop.read() > 3) return false; if (tmr.read() > 0.01) { tmr.reset(); if (eth_connected) { switch (i) { case 0: sprintf(cmd, "GET /%s HTTP/1.1\r\n", path); sock.send(cmd, strlen(cmd)); i++; break; case 1: sprintf(cmd, "Host: %s:%s\r\n", ip_domain, port); sock.send(cmd, strlen(cmd)); i++; break; case 2: sprintf(cmd, "Upgrade: WebSocket\r\n"); sock.send(cmd, strlen(cmd)); i++; break; case 3: sprintf(cmd, "Origin: http:%s:%s\r\n", ip_domain, port); sock.send(cmd, strlen(cmd)); i++; break; case 4: sprintf(cmd, "Connection: Upgrade\r\n"); sock.send(cmd, strlen(cmd)); i++; break; case 5: sprintf(cmd, "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n"); sock.send(cmd, strlen(cmd)); i++; break; case 6: sprintf(cmd, "Sec-WebSocket-key2: 12998 5 Y3 1 .P00\r\n\r\n"); sock.send(cmd, strlen(cmd)); i++; break; case 7: sock.send("^n:ds[4U", 8); i++; break; case 8: if (response_server_eth) i++; else break; default: break; } } if (i==9) { printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain, this->path, this->port); return true; } } } } return true; } void Websocket::Send(char * str) { char cmd[100]; if (wifi_use) { wifi->putc('\x00'); sprintf(cmd, "%s%c", str, '\xff'); wifi->Send(cmd, "NO"); } else if (eth_use) { sprintf(cmd, "%c%s%c", 0x00, str, 0xff); Net::poll(); sock.send(cmd, strlen(cmd + 1) + 1); } } bool Websocket::read(char * message) { int i = 0; if (wifi_use) { wifi->read(message); //printf("msg: %s strlen: %d\r\n", message + 1, strlen(message + 1) - 1); //we check if the first byte is 0x00 if (message == NULL || message[0] != 0x00) { message = NULL; return false; } while(message[i + 1] != 0xff && i < strlen(message + 1)) i++; if(message[i+1] == 0xff) { message[i+1] = 0; memcpy(message, message + 1, strlen(message + 1) + 1); } else { message = NULL; return false; } } else if (eth_use) { Net::poll(); if (eth_rx[0] != 0x00) { message = NULL; return false; } while (eth_rx[i + 1] != 0xff) { message[i] = eth_rx[i + 1]; i++; } message[i] = 0; } return true; } bool Websocket::close() { if (wifi_use) { if (!wifi->CmdMode()) { printf("Websocket::close: cannot enter in cmd mode\r\n"); return false; } wifi->Send("close\r", "NO"); if (!wifi->exit()) return false; } else if (eth_use) { if (sock.close()) return false; } return true; } bool Websocket::connected() { if (wifi_use) { char str[10]; wait(0.25); if (!wifi->CmdMode()) { printf("Websocket::connected: cannot enter in cmd mode\r\n"); return false; } wait(0.25); wifi->Send("show c\r\n", "NO", str); if (str[3] == '1') { if (!wifi->exit()) { printf("Websocket::connected: cannot exit\r\n"); return false; } return true; } if (!wifi->exit()) printf("Websocket::connected: cannot exit\r\n"); return false; } else if (eth_use) return eth_connected; return true; } void Websocket::onTCPSocketEvent(TCPSocketEvent e) { if (e == TCPSOCKET_CONNECTED) { eth_connected = true; printf("TCP Socket Connected\r\n"); } else if (e == TCPSOCKET_WRITEABLE) { } else if (e == TCPSOCKET_READABLE) { int len = sock.recv(eth_rx, 512); eth_rx[len] = 0; if (!response_server_eth) { string checking; size_t found = string::npos; checking = eth_rx; found = checking.find("HTTP"); if (found != string::npos) response_server_eth = true; } } else { printf("TCP Socket Fail\r\n"); eth_connected = false; } }