Hello World program demonstrating HTML5 Websockets connecting via Ethernet

Dependencies:   WebSocketClient

Fork of Websocket_Ethernet_HelloWorld by mbed official

Revision:
6:2fae6e37c5ca
Parent:
3:9bd22e5386cd
--- a/main.cpp	Wed May 14 15:26:34 2014 +0000
+++ b/main.cpp	Thu Mar 16 21:15:06 2017 +0000
@@ -1,47 +1,51 @@
+/* Copyright C2014 ARM, MIT License
+ *
+ * Author: Doug Anson (doug.anson@arm.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files the "Software", to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
 #include "mbed.h"
 #include "EthernetInterface.h"
 #include "Websocket.h"
 
-Ticker flash;
+// Blinky
 DigitalOut led(LED1);
-void flashLED(void){led = !led;}
- 
- 
-int main() 
-{
-    flash.attach(&flashLED, 1.0f);
+
+int main() {   
+
+    // announce
+    printf("Websocket Example v1.0.0\r\n");
     
-    EthernetInterface ethernet;
-    ethernet.init();    // connect with DHCP
-    int ret_val = ethernet.connect();
-
-    if (0 == ret_val) {
-        printf("IP Address: %s\n", ethernet.getIPAddress());
-    } else {
-        error("ethernet failed to connect: %d.\n", ret_val);
-    }
+    // Create a network interface and connect
+    EthernetInterface eth;
+    eth.connect();
+    printf("IP Address is %s\n\r", eth.get_ip_address());
 
-    // view @ http://sockets.mbed.org/demo/viewer
-    Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw");
-    ws.connect();
-    char str[100];
-    
-    for(int i=0; i<0x7fffffff; ++i) {
-        // string with a message
-        sprintf(str, "%d WebSocket Hello World over Ethernet", i);
-        ws.send(str);
+    // Create a websocket instance
+    Websocket ws("ws://example.com:8080/", &eth);
+    int connect_error = ws.connect();
     
-        // clear the buffer and wait a sec...
-        memset(str, 0, 100);
-        wait(0.5f);
-    
-        // websocket server should echo whatever we sent it
-        if (ws.read(str)) {
-            printf("rcv'd: %s\n", str);
-        }
+    // begin main loop
+    while (true) {
+
+        // blink... 
+        led = !led; 
+        wait(0.5);
+        
+        int error_c = ws.send("Hello World\r\n");
     }
-    ws.close();
-    ethernet.disconnect();
-    
-    while(true);
 }
\ No newline at end of file