GithubHelp home page GithubHelp logo

sahilalipuria / simple_websocket_example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cankav/simple_websocket_example

0.0 2.0 0.0 128 KB

Simple websocket example with javascript client and python Tornado server files

HTML 11.30% JavaScript 51.13% Python 37.57%

simple_websocket_example's Introduction

simple websocket example

Simple websocket example with javascript client and python Tornado server files

Websockets provide convenient server to client messaging. To work with websockets you will need a websocket enabled client/server pair. Check page below for client websocket support:

http://caniuse.com/websockets

Sections below explain files in this example.

client.js

Contains javascript client code, which opens a websocket to the server and attempts to re-open it on websocket close event.

client.js should run on client side, which may be embeded to your html file. On page load the code tries to establish websocket connection with address: "ws://localhost:8080/websocket". If for some reason connection is closed, javascript code will try to re-open the connection after waiting for one second.

  • Insert your custom processing into the ws.onmessage function.
  • Use the ws_send function to send a message to the server.

Please note that you will need jquery for this example to work.

client.html

Bare-bones html file which includes jquery from Google's CDN and client.js.

tornado_server.py

Python code in tornado_server.py uses Tornado web server to open a websocket connection as well as an http connection. Once Tornado is installed (pip install tornado) you can use the command below to initialize the web server:

python tornado_server.py

To test if http server is running you can visit http://localhost:8080/hello-tornado page. Please note that websockets are not accessible through http. So do not try to access /websocket via your browser.

If it all goes according to the plan you should see the following on your browser's console when you access your html document:

ws open
ws_send sent

Python code will also provide some output:

WebSocket opened
msg recevied {"event":"register"}

Server code stores connected clients in its MyWebSocket.clients data structure. Once a message is received from a client all other recorded clients are forwarded the sent message. If a client disconnects for some reason the MyWebSocket.clients list is updated.

Contact

Please let me know if you spot any errors or for any other reason: [email protected]

simple_websocket_example's People

Contributors

cankav avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.