Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save minimum-necessary-change/3ee0fed447861ecdaed2de44610af4d6 to your computer and use it in GitHub Desktop.

Select an option

Save minimum-necessary-change/3ee0fed447861ecdaed2de44610af4d6 to your computer and use it in GitHub Desktop.

Revisions

  1. Ismael Celis revised this gist Jul 14, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ws_events_dispatcher.js
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    -------------------------------*/
    /* Ismael Celis 2010
    /*
    Simplified WebSocket events dispatcher (no channels, no users)
    var socket = new FancyWebSocket();
  2. Ismael Celis revised this gist Jul 14, 2014. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions ws_events_dispatcher.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,26 @@
    /*
    The MIT License (MIT)
    Copyright (c) 2014 Ismael Celis
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    -------------------------------*/
    /* Ismael Celis 2010
    Simplified WebSocket events dispatcher (no channels, no users)
  3. @ismasan ismasan revised this gist Oct 23, 2010. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions ws_events_dispatcher.js
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,18 @@
    /* Ismael Celis 2010
    Simplified WebSocket events dispatcher (no channels, no users)
    var socket = new ServerEventsDispatcher();
    var socket = new FancyWebSocket();
    // bind to server events
    socket.bind('some_event', function(data){
    alert(data.name + ' says: ' + data.message)
    });
    // broadcast events to all connected users
    socket.trigger( 'some_event', {name: 'ismael', message : 'Hello world'} );
    socket.send( 'some_event', {name: 'ismael', message : 'Hello world'} );
    */

    var ServerEventsDispatcher = function(url){
    var FancyWebSocket = function(url){
    var conn = new WebSocket(url);

    var callbacks = {};
  4. @ismasan ismasan revised this gist Jul 1, 2010. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions ws_events_dispatcher.js
    Original file line number Diff line number Diff line change
    @@ -14,30 +14,30 @@ socket.trigger( 'some_event', {name: 'ismael', message : 'Hello world'} );

    var ServerEventsDispatcher = function(url){
    var conn = new WebSocket(url);

    var callbacks = {};

    this.bind = function(event_name, callback){
    callbacks[event_name] = callbacks[event_name] || [];
    callbacks[event_name].push(callback);
    return this;// chainable
    };
    this.send = function(event_name, data){
    var payload = JSON.stringify([event_name, data]);

    this.send = function(event_name, event_data){
    var payload = JSON.stringify({event:event_name, data: event_data});
    conn.send( payload ); // <= send JSON data to socket server
    return this;
    };

    // dispatch to the right handlers
    conn.onmessage = function(evt){
    var json = JSON.parse(evt.data)
    dispatch(json.event, json.data)
    };

    conn.onclose = function(){dispatch('close',null)}
    conn.onopen = function(){dispatch('open',null)}

    var dispatch = function(event_name, message){
    var chain = callbacks[event_name];
    if(typeof chain == 'undefined') return; // no callbacks for this event
  5. @ismasan ismasan revised this gist Jul 1, 2010. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions ws_events_dispatcher.js
    Original file line number Diff line number Diff line change
    @@ -31,10 +31,8 @@ var ServerEventsDispatcher = function(url){

    // dispatch to the right handlers
    conn.onmessage = function(evt){
    var data = JSON.parse(evt.data),
    event_name = data[0],
    message = data[1];
    dispatch(event_name, message)
    var json = JSON.parse(evt.data)
    dispatch(json.event, json.data)
    };

    conn.onclose = function(){dispatch('close',null)}
  6. @ismasan ismasan revised this gist Jul 1, 2010. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions ws_events_dispatcher.js
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,8 @@ socket.bind('some_event', function(data){
    socket.trigger( 'some_event', {name: 'ismael', message : 'Hello world'} );
    */

    var ServerEventsDispatcher = function(){
    var conn = new WebSocket('ws://socket.server.com');
    var ServerEventsDispatcher = function(url){
    var conn = new WebSocket(url);

    var callbacks = {};

    @@ -23,7 +23,7 @@ var ServerEventsDispatcher = function(){
    return this;// chainable
    };

    this.trigger = function(event_name, data){
    this.send = function(event_name, data){
    var payload = JSON.stringify([event_name, data]);
    conn.send( payload ); // <= send JSON data to socket server
    return this;
  7. @ismasan ismasan revised this gist Feb 10, 2010. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ws_events_dispatcher.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /* Ismael Celis 2010
    Simplified WebSocket events dispatcher (no channels, no users)
    var socket = new EventsDispatcher();
    var socket = new ServerEventsDispatcher();
    // bind to server events
    socket.bind('some_event', function(data){
    @@ -12,7 +12,7 @@ socket.bind('some_event', function(data){
    socket.trigger( 'some_event', {name: 'ismael', message : 'Hello world'} );
    */

    var EventsDispatcher = function(){
    var ServerEventsDispatcher = function(){
    var conn = new WebSocket('ws://socket.server.com');

    var callbacks = {};
  8. @ismasan ismasan created this gist Feb 9, 2010.
    50 changes: 50 additions & 0 deletions ws_events_dispatcher.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    /* Ismael Celis 2010
    Simplified WebSocket events dispatcher (no channels, no users)
    var socket = new EventsDispatcher();
    // bind to server events
    socket.bind('some_event', function(data){
    alert(data.name + ' says: ' + data.message)
    });
    // broadcast events to all connected users
    socket.trigger( 'some_event', {name: 'ismael', message : 'Hello world'} );
    */

    var EventsDispatcher = function(){
    var conn = new WebSocket('ws://socket.server.com');

    var callbacks = {};

    this.bind = function(event_name, callback){
    callbacks[event_name] = callbacks[event_name] || [];
    callbacks[event_name].push(callback);
    return this;// chainable
    };

    this.trigger = function(event_name, data){
    var payload = JSON.stringify([event_name, data]);
    conn.send( payload ); // <= send JSON data to socket server
    return this;
    };

    // dispatch to the right handlers
    conn.onmessage = function(evt){
    var data = JSON.parse(evt.data),
    event_name = data[0],
    message = data[1];
    dispatch(event_name, message)
    };

    conn.onclose = function(){dispatch('close',null)}
    conn.onopen = function(){dispatch('open',null)}

    var dispatch = function(event_name, message){
    var chain = callbacks[event_name];
    if(typeof chain == 'undefined') return; // no callbacks for this event
    for(var i = 0; i < chain.length; i++){
    chain[i]( message )
    }
    }
    };