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:
Tue Oct 27 10:03:33 2015 +0000
Revision:
1:610f3dc5679e
Parent:
0:47ec083185d0
Add ws.read function

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 1:610f3dc5679e 11 char recv[50];
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 1:610f3dc5679e 20 //wifi.setNetwork("Tsungta_iPhone", "icq87001", 0);
wgd8700 1:610f3dc5679e 21 wifi.setNetwork("GainSpan_JS", "Delta9999", 0);
wgd8700 0:47ec083185d0 22
wgd8700 0:47ec083185d0 23 eth.connect(40000);
wgd8700 0:47ec083185d0 24 pc.printf("IP Address:%s\n",eth.getIPAddress());
wgd8700 0:47ec083185d0 25 pc.printf("MAC Address:%s\n",eth.getMACAddress());
wgd8700 0:47ec083185d0 26
wgd8700 1:610f3dc5679e 27 Websocket ws("ws://192.168.15.105:8888/ws");
wgd8700 0:47ec083185d0 28 ws.connect();
wgd8700 0:47ec083185d0 29
wgd8700 0:47ec083185d0 30 while (1) {
wgd8700 0:47ec083185d0 31 ws.send("WebSocket Hello World over Ethernet");
wgd8700 1:610f3dc5679e 32 ws.read(recv);
wgd8700 1:610f3dc5679e 33 pc.printf("read: %s\n", recv);
wgd8700 1:610f3dc5679e 34 wait(1.0);
wgd8700 0:47ec083185d0 35 }
wgd8700 0:47ec083185d0 36 }