-
-
Save minimum-necessary-change/3ee0fed447861ecdaed2de44610af4d6 to your computer and use it in GitHub Desktop.
Revisions
-
Ismael Celis revised this gist
Jul 14, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -------------------------------*/ /* Simplified WebSocket events dispatcher (no channels, no users) var socket = new FancyWebSocket(); -
Ismael Celis revised this gist
Jul 14, 2014 . 1 changed file with 23 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) -
ismasan revised this gist
Oct 23, 2010 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 FancyWebSocket(); // bind to server events socket.bind('some_event', function(data){ alert(data.name + ' says: ' + data.message) }); // broadcast events to all connected users socket.send( 'some_event', {name: 'ismael', message : 'Hello world'} ); */ var FancyWebSocket = function(url){ var conn = new WebSocket(url); var callbacks = {}; -
ismasan revised this gist
Jul 1, 2010 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, 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 -
ismasan revised this gist
Jul 1, 2010 . 1 changed file with 2 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 json = JSON.parse(evt.data) dispatch(json.event, json.data) }; conn.onclose = function(){dispatch('close',null)} -
ismasan revised this gist
Jul 1, 2010 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(url){ var conn = new WebSocket(url); var callbacks = {}; @@ -23,7 +23,7 @@ var ServerEventsDispatcher = function(){ return this;// chainable }; this.send = function(event_name, data){ var payload = JSON.stringify([event_name, data]); conn.send( payload ); // <= send JSON data to socket server return this; -
ismasan revised this gist
Feb 10, 2010 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 ServerEventsDispatcher = function(){ var conn = new WebSocket('ws://socket.server.com'); var callbacks = {}; -
ismasan created this gist
Feb 9, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ) } } };