Simple websocket client example

Dependencies:   WIFI_API_32kRAM WebSocketClient mbed

This sample code work with following Websocket Echo Server implementation using Python (tornado package needed). The echo server will wait for a message to be received and then echo back the message reversed. Details about the connection will be printed to the terminal.

import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import socket
'''
This is a simple Websocket Echo server that uses the Tornado websocket handler.
Please run `pip install tornado` with python of version 2.7.9 or greater to install tornado.
This program will echo back the reverse of whatever it recieves.
Messages are output to the terminal for debuggin purposes. 
''' 
 
class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
      
    def on_message(self, message):
        print 'message received:  %s' % message
        # Reverse Message and send it back
        print 'sending back message: %s' % message[::-1]
        self.write_message(message[::-1])
 
    def on_close(self):
        print 'connection closed'
 
    def check_origin(self, origin):
        return True
 
application = tornado.web.Application([
    (r'/ws', WSHandler),
])
 
 
if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8888)
    myIP = socket.gethostbyname(socket.gethostname())
    print '*** Websocket Server Started at %s***' % myIP
    tornado.ioloop.IOLoop.instance().start()
 
Committer:
wgd8700
Date:
Wed Oct 07 05:16:10 2015 +0000
Revision:
0:47ec083185d0
Child:
1:610f3dc5679e
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wgd8700 0:47ec083185d0 1 #include "mbed.h"
wgd8700 0:47ec083185d0 2 #include "EthernetInterface.h"
wgd8700 0:47ec083185d0 3 #include "Websocket.h"
wgd8700 0:47ec083185d0 4
wgd8700 0:47ec083185d0 5 #include "WIFIDevice.h"
wgd8700 0:47ec083185d0 6
wgd8700 0:47ec083185d0 7 WIFIDevice wifi;
wgd8700 0:47ec083185d0 8 Serial pc(USBTX, USBRX);
wgd8700 0:47ec083185d0 9
wgd8700 0:47ec083185d0 10 int main() {
wgd8700 0:47ec083185d0 11 char recv[30];
wgd8700 0:47ec083185d0 12
wgd8700 0:47ec083185d0 13 pc.baud(115200);
wgd8700 0:47ec083185d0 14 pc.printf("Init...\n");
wgd8700 0:47ec083185d0 15
wgd8700 0:47ec083185d0 16 EthernetInterface eth;
wgd8700 0:47ec083185d0 17 eth.init(); //Use DHCP
wgd8700 0:47ec083185d0 18
wgd8700 0:47ec083185d0 19 // set given SSID and PW as the highest priority
wgd8700 0:47ec083185d0 20 wifi.setNetwork("Tsungta_iPhone", "icq87001", 0);
wgd8700 0:47ec083185d0 21
wgd8700 0:47ec083185d0 22 eth.connect(40000);
wgd8700 0:47ec083185d0 23 pc.printf("IP Address:%s\n",eth.getIPAddress());
wgd8700 0:47ec083185d0 24 pc.printf("MAC Address:%s\n",eth.getMACAddress());
wgd8700 0:47ec083185d0 25
wgd8700 0:47ec083185d0 26 Websocket ws("ws://172.20.10.11:8888/ws");
wgd8700 0:47ec083185d0 27 ws.connect();
wgd8700 0:47ec083185d0 28
wgd8700 0:47ec083185d0 29 while (1) {
wgd8700 0:47ec083185d0 30 ws.send("WebSocket Hello World over Ethernet");
wgd8700 0:47ec083185d0 31 wait(1.0);
wgd8700 0:47ec083185d0 32 }
wgd8700 0:47ec083185d0 33 }