Giter VIP home page Giter VIP logo

websocketex.js's Introduction

WebSocketEx.js

Extended WebSocket library for Javascript, which allows subscribing to events along with handling various events with callbacks.

Callbacks

  • WebSocketEx.prototype.onopen
  • WebSocketEx.prototype.onclose
  • WebSocketEx.prototype.onretry
  • WebSocketEx.prototype.onmessage
  • WebSocketEx.prototype.subscribe
  • WebSocketEx.prototype.send

Constructor

var socket = new WebSocketEx();
var socket = new WebSocketEx('localhost'); // default
var socket = new WebSocketEx('localhost', 8080); // default

Pub/Sub

WebSocketEx.prototype.subscribe is fired whenever a JSON-encoded string is sent to the client, in the following format:

{
	'type': 'event name',
	'data': your_object_here
}

Examples

window.socket = new WebSocketEx();

window.socket.onopen(function() {
    console.log('Connected to server!');
    window.socket.send('Sending some data!');
});

window.socket.onretry(function() {
    console.log('Lost connection to websocket server, attempting to reconnect...');
});

window.socket.subscribe('alert', function(data) {
    console.log('Do something with your ' + data.customProperty + ', or it could be just a simple string that says: ' + data);
});

window.socket.onmessage(function(e) {
	console.log('Here's a socket message! Published events will never reach this. They are handled by WebSocketEx.prototype.subscribe.');
});

websocketex.js's People

Contributors

waylaidwanderer avatar

Stargazers

David, your friend avatar

Watchers

James Cloos avatar  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.