Comparing version 1.1.3 to 1.1.4
YUI().add('EventHub', function(Y) { | ||
Y.EventHub = function(io, url) { | ||
var _this = this; | ||
var _this = this, | ||
socket = io.connect(url) | ||
; | ||
// Bookkeeping | ||
this.events = {}; | ||
this._fire = this.fire; | ||
// this._on = this.on; | ||
var socket = io.connect(url); | ||
// cookie-ize the session | ||
// Get the session & cookie-ize the session | ||
socket.on('ready', function(session) { | ||
Y.Cookie.set("eventHub", session, { expires: new Date("August 11, 2069"), path: '/' }); | ||
socket.$emit = function() { _this._fire.apply(_this, arguments); }; | ||
socket.$emit = function() { /* fire events locally */ _this._fire.apply(_this, arguments); }; | ||
_this._fire('eventHubReady'); | ||
}); | ||
// Set our session if we have one | ||
socket.on('connect', function() { | ||
@@ -23,2 +24,3 @@ socket.emit('eventHub:session', Y.Cookie.get('eventHub')); | ||
// Fire all events off to the hub | ||
this.fire = function() { | ||
@@ -28,15 +30,2 @@ socket.emit.apply(socket, arguments); | ||
/** | ||
* Can there be such a thing as a browser event unicast listener???? | ||
**/ | ||
/* Tell event switch we're listening for a unicast event... | ||
this.on = function(eventName, func, args) { | ||
this._on.apply(this, arguments); | ||
if (typeof(args) !== 'undefined') { | ||
args['eventHub:session'] = this.session; | ||
this.fire('eventHub:on', eventName, args); | ||
} | ||
}; | ||
*/ | ||
/* | ||
@@ -50,5 +39,8 @@ * An optional helper function to set up compiler-checkable event names | ||
var _this = this; | ||
// Dummy up an object that drop in the event name for listening & firing | ||
this.events[eventName] = { | ||
on: function(callback, args) { _this.on.call(_this, eventName, callback, args); } | ||
on: function(callback) { _this.on.call(_this, eventName, callback); } | ||
, fire: function() { | ||
// Shove event name back in argument list & fire it | ||
Array.prototype.unshift.call(arguments, eventName); | ||
@@ -61,4 +53,7 @@ _this.fire.apply(_this, arguments); | ||
}; | ||
// We're an event target | ||
Y.augment(Y.EventHub, Y.EventTarget); | ||
}, '1.0', { requires: [ 'event-custom', 'cookie' ] }); | ||
var eventHub = require('../../clients/server/eventClient.js').getClientHub('http://localhost:5883?token=ehrox'); | ||
eventHub.on('eventHubReady', function() { | ||
console.log('EHR'); | ||
eventHub.on('click', function(data, callback) { | ||
console.log('GOT A CLICK Event'); | ||
console.log(data); | ||
callback('howdy from server', { mark: 'trostler', pid: process.pid }); | ||
eventHub.emit('zot', process.pid); | ||
}, { type: 'unicast' }); | ||
// this goes to everyone BUT me... | ||
// eventHub.emit('eventClient:done', 'click'); | ||
eventHub.on('eventClient:done', function(event) { | ||
@@ -16,0 +9,0 @@ console.log('DONE LISTENING FOR ' + event); |
@@ -78,33 +78,59 @@ var port = process.env['npm_package_config_' + port] || 5883 | ||
socket.$emit = function() { | ||
var ss, event, i; | ||
var ss, event, i | ||
, args = toArray(arguments); | ||
; | ||
if (arguments[0] === 'disconnect') { | ||
delete sockets[socket.id]; | ||
for (event in events) { | ||
if (events[event] === socket.id) { | ||
events[event] = null; | ||
// some sanity | ||
if (args[0].match(/^eventClient/)) { | ||
// Hey only _I_ can send these messages!! | ||
return; | ||
} | ||
// If an non-authenticated socket just sent us a session key make sure it's the right one | ||
if (typeof args[1] === 'object' && args[1] !== null) { | ||
if (typeof args[1]['eventHub:session'] !== 'undefined' && !hs.authenticated) { | ||
if (args[1]['eventHub:session'] !== hs.session) { | ||
// something funny going on..... not passing on this event | ||
return; | ||
} | ||
} | ||
} | ||
} else if (arguments[0] === 'eventHub:session') { | ||
if (arguments[1]) { | ||
hs.session = arguments[1]; | ||
} | ||
//socket.set('session', hs.session, function() { socket.emit('ready', hs.session); } ); | ||
socket.emit('ready', hs.session); | ||
} else if (arguments[0] === 'eventHub:on') { | ||
eventName = arguments[1]; | ||
args = arguments[2]; | ||
if (args.type === 'unicast' && hs.authenticated) { | ||
console.log('set unicast for ' + eventName + ' to ' + socket.id); | ||
if (events[eventName]) { | ||
// tell previous dude he's been replaced | ||
console.log('tell ' + events[eventName] + ' they are done with ' + eventName); | ||
socket.emit.call(sockets[events[eventName]], 'eventClient:done', eventName); | ||
if (args[0] === 'disconnect') { | ||
delete sockets[socket.id]; | ||
for (event in events) { | ||
if (events[event] === socket.id) { | ||
events[event] = null; | ||
} | ||
events[eventName] = socket.id; | ||
} | ||
} else if (args[0] === 'eventHub:session') { | ||
if (args[1]) { | ||
hs.session = args[1]; | ||
} | ||
socket.set('session', hs.session, function() { socket.emit('ready', hs.session); } ); | ||
} else if (args[1] === 'eventHub:on') { | ||
var eventName = args[1] | ||
, xtra = args[2]; | ||
if (xtra.type === 'unicast' && hs.authenticated) { | ||
console.log('set unicast for ' + eventName + ' to ' + socket.id); | ||
if (events[eventName]) { | ||
// tell previous dude he's been replaced | ||
console.log('tell ' + events[eventName] + ' they are done with ' + eventName); | ||
socket.emit.call(sockets[events[eventName]], 'eventClient:done', eventName); | ||
} | ||
events[eventName] = socket.id; | ||
} | ||
} else { | ||
if (arguments[0] !== 'newListener') { | ||
if (events[arguments[0]]) { // UNICAST | ||
if (args[0] !== 'newListener') { | ||
// Evenryday i'm shufflin.... - make room for session key | ||
if (hs.session && typeof args[1] !== 'object') { | ||
if (typeof args[1] !== 'undefined') { | ||
args[2] = args[1]; | ||
args[1] = {}; | ||
} | ||
} | ||
if (events[args[0]]) { // UNICAST | ||
/* | ||
@@ -118,12 +144,12 @@ * So this can be a message from a client or from a backend to | ||
*/ | ||
ss = sockets[events[arguments[0]]]; // 'ss' is socket to send to | ||
ss = sockets[events[args[0]]]; // 'ss' is socket to send to | ||
// 'socket' is where this event came from | ||
if (ss) { | ||
if (typeof(arguments[1] === 'object')) { | ||
if (typeof(args[1] === 'object')) { | ||
// toss in session key | ||
if (hs.session) { | ||
arguments[1]['eventHub:session'] = hs.session; | ||
args[1]['eventHub:session'] = hs.session; | ||
} | ||
} | ||
socket.emit.apply(ss, arguments); | ||
socket.emit.apply(ss, args); | ||
} | ||
@@ -134,14 +160,14 @@ } else { // BROADCAST | ||
// we send oursevles because 'broadcast' does not handle acknowledgements | ||
if (arguments[0] !== 'eventClient:done' || sock !== socket.id) { | ||
if (args[0] !== 'eventClient:done' || sock !== socket.id) { | ||
// If the guy we're broadcasthing this to is authenticated & there's a session | ||
// associated with the sender & then toss in the session | ||
if (sockets[sock].handshake.authenticated && hs.session && typeof arguments[1] === 'object') { | ||
arguments[1]['eventHub:session'] = hs.session; | ||
if (sockets[sock].handshake && sockets[sock].handshake.authenticated && hs.session && typeof args[1] === 'object') { | ||
args[1]['eventHub:session'] = hs.session; | ||
} else { | ||
// otherwise no session key for you | ||
if (typeof arguments[1] === 'object') { | ||
delete arguments[1]['eventHub:session']; | ||
if (typeof args[1] === 'object') { | ||
delete args[1]['eventHub:session']; | ||
} | ||
} | ||
socket.emit.apply(sockets[sock], arguments); | ||
socket.emit.apply(sockets[sock], args); | ||
} | ||
@@ -148,0 +174,0 @@ } |
@@ -13,3 +13,3 @@ { | ||
], | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"author": "Mark Ethan Trostler <mark@zzo.com>", | ||
@@ -16,0 +16,0 @@ "preferGlobal": true, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
250303
17
405