WebSocket client library

Revision:
17:1e339933c97a
Parent:
16:d4518b50f653
Child:
18:ef44ea603938
--- a/Websocket.cpp	Wed Sep 21 09:31:36 2011 +0000
+++ b/Websocket.cpp	Thu Sep 22 10:09:12 2011 +0000
@@ -32,7 +32,7 @@
     if (server_ip == NULL) {
         DNSResolver dr;
         server_ip = new IpAddr();
-        *server_ip = dr.resolveName(ip_domain);
+        *server_ip = dr.resolveName(ip_domain.c_str());
 #ifdef DEBUG
         printf("\r\nserver with dns=%i.%i.%i.%i\r\n",server_ip[0],server_ip[1],server_ip[2],server_ip[3]);
 #endif
@@ -57,8 +57,6 @@
 
     res = strtok(buf, ":");
     if (strcmp(res, "ws")) {
-        strcpy(this->ip_domain, "");
-        strcpy(this->path, "");
 #ifdef DEBUG
         printf("\r\nFormat error: please use: \"ws://ip-or-domain[:port]/path\"\r\n\r\n");
 #endif
@@ -69,7 +67,7 @@
         //path
         res1 = strtok(NULL, " ");
         if (res1 != NULL) {
-            strcpy(this->path, res1);
+            path = res1;
         }
 
         //ip_domain
@@ -79,13 +77,13 @@
         res1 = strtok(NULL, " ");
         //port
         if (res1 != NULL) {
-            strcpy(this->port, res1);
+            port = res1;
         } else {
-            strcpy(this->port, "80");
+            port = "80";
         }
 
         if (res != NULL) {
-            strcpy(this->ip_domain, res);
+            ip_domain = res;
 
             //if we use ethernet, we must decode ip address or use dnsresolver
             if (netif == ETH) {
@@ -126,7 +124,7 @@
 
 
         //open the connection
-        sprintf(cmd, "open %s %s\r\n", ip_domain, port);
+        sprintf(cmd, "open %s %s\r\n", ip_domain.c_str(), port.c_str());
         if (!wifi->Send(cmd, "OPEN*")) {
 #ifdef DEBUG
             printf("Websocket::connect cannot open\r\n");
@@ -136,15 +134,15 @@
 
 
         //send websocket HTTP header
-        sprintf(cmd, "GET /%s HTTP/1.1\r\n", path);
+        sprintf(cmd, "GET /%s HTTP/1.1\r\n", path.c_str());
         wifi->Send(cmd, "NO");
 
-        sprintf(cmd, "Host: %s:%s\r\n", ip_domain, port);
+        sprintf(cmd, "Host: %s:%s\r\n", ip_domain.c_str(), port.c_str());
         wifi->Send(cmd, "NO");
 
         wifi->Send("Upgrade: WebSocket\r\n", "NO");
 
-        sprintf(cmd, "Origin: http:%s:%s\r\n", ip_domain, port);
+        sprintf(cmd, "Origin: http:%s:%s\r\n", ip_domain.c_str(), port.c_str());
         wifi->Send(cmd, "NO");
 
 
@@ -154,11 +152,11 @@
         if (!wifi->Send("^n:ds[4U", "8jKS'y:G*Co,Wxa-"))
             return false;
 #ifdef DEBUG
-        printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain, this->path, this->port);
+        printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain.c_str(), this->path.c_str(), this->port.c_str());
 #endif
         return true;
     } else if (netif == ETH) {
-        Host server (*server_ip, atoi(port));
+        Host server (*server_ip, atoi(port.c_str()));
         sock->close();
         TCPSocketErr bindErr = sock->connect(server);
         if (bindErr) {
@@ -184,12 +182,12 @@
                 if (eth_connected) {
                     switch (i) {
                         case 0:
-                            sprintf(cmd, "GET /%s HTTP/1.1\r\n", path);
+                            sprintf(cmd, "GET /%s HTTP/1.1\r\n", path.c_str());
                             sock->send(cmd, strlen(cmd));
                             i++;
                             break;
                         case 1:
-                            sprintf(cmd, "Host: %s:%s\r\n", ip_domain, port);
+                            sprintf(cmd, "Host: %s:%s\r\n", ip_domain.c_str(), port.c_str());
                             sock->send(cmd, strlen(cmd));
                             i++;
                             break;
@@ -199,7 +197,7 @@
                             i++;
                             break;
                         case 3:
-                            sprintf(cmd, "Origin: http:%s:%s\r\n", ip_domain, port);
+                            sprintf(cmd, "Origin: http:%s:%s\r\n", ip_domain.c_str(), port.c_str());
                             sock->send(cmd, strlen(cmd));
                             i++;
                             break;
@@ -234,7 +232,7 @@
                 }
                 if (i==9) {
 #ifdef DEBUG
-                    printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain, this->path, this->port);
+                    printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain.c_str(), this->path.c_str(), this->port.c_str());
 #endif
                     return true;
                 }
@@ -369,6 +367,11 @@
     return false;
 }
 
+std::string Websocket::getPath()
+{
+    return path;
+}
+