PNews jun 2015 | Page 19

Tutorials Point, Simply Easy Learning // Web Socket is connected, send data using send() ws.send("Message to send"); alert("Message is sent..."); }; ws.onmessage = function (evt) { var received_msg = evt.data; alert("Message is received..."); }; ws.onclose = function() { // websocket is closed. alert("Connection is closed..."); }; } else { // The browser doesn't support WebSocket alert("WebSocket NOT supported by your Browser!"); } }
Run WebSocket
Install pywebsocket: Before you test above client program, you need a server which supports WebSocket. Download mod_pywebsocket-x.x.x.tar.gz from pywebsocket which aims to provide a Web Socket extension for Apache HTTP Server ans install it following these steps. 1. 2. 3. 4. 5. Unzip and untar the downloaded file. Go inside pywebsocket-x.x.x/src/ directory. $python setup.py build $sudo python setup.py install Then read document by: o $pydoc mod_pywebsocket This will install it into your python environment. Start the Server Go to the command: pywebsocket-x.x.x/src/mod_pywebsocket folder and run the following $sudo python standalone.py -p 9998 -w ../example/ This will start the server listening at port 9998 and use the handlers directory specified by the w option where our echo_wsh.py resides. Now using Chrome browser open the html file your created in the beginning. If your browser supports WebSocket(), then you would get alert indicating that your browser supports 19 | P a g e