Skip to content

Commit

Permalink
Upgraded JS library, integrates tightly with the RemoteEventTransport…
Browse files Browse the repository at this point in the history
…. Note that it is optional to use this transport. Native WebSocket connections are possible when not using this additional functionality.
  • Loading branch information
Devristo committed Dec 2, 2013
1 parent 880f80e commit 7f38483
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 45 deletions.
143 changes: 99 additions & 44 deletions js/phpws.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,94 @@
*/

!function($){
var Client = function(url){
var self = this;
var ws = null;
var openPromise = $.Deferred();
var Emitter = function(){
var callbacks = {};

var triggerEvent = function(event){
this.on = function(event, callback){
if(!(event in callbacks))
callbacks[event] = $.Callbacks();

callbacks[event].add(callback);
return this;
};

this.removeListener = function(event, callback){
if(event in callbacks){
callbacks[event].fireWith(this, Array.prototype.slice.call(arguments, 1));
callbacks[event].remove.apply(callbacks[event], Array.prototype.slice.call(arguments, 1));
}

return this;
};

this.once = function(event, callback){
var self = this;
var _once = function(){
callback();
self.removeListener(event, arguments.callee);
};

this.on = function(event, callback){
if(!(event in callbacks))
callbacks[event] = $.Callbacks();

callbacks[event].add.apply(callbacks[event], Array.prototype.slice.call(arguments, 1));
return this;
return this.on(event, _once);
};

this.unsubscribe = function(event, callback){
this.trigger = function(event, data){
if(event in callbacks){
callbacks[event].remove.apply(callbacks[event], Array.prototype.slice.call(arguments, 1));
callbacks[event].fireWith(this, Array.prototype.slice.call(arguments, 1));
}

return this;
};
};

this.connect = function(url){
var Client = function(){
var self = this;
var ws = null;
var openPromise = $.Deferred();
var reconnectTimer = null;

this.connect = function(url, autoReconnect){
ws = new WebSocket(url);

ws.addEventListener('open', function(){
openPromise.resolveWith(self);
triggerEvent("open", arguments);
self.trigger("open", arguments);
});

ws.addEventListener('close', function(){
openPromise.resolveWith(self);
triggerEvent("close", arguments);
self.trigger("close", arguments);
});

ws.addEventListener('error', function(){
openPromise.rejectWith(self);
triggerEvent("error", arguments);
self.trigger("error", arguments);
});

ws.addEventListener('message', function(event){
triggerEvent("message", event.data);
self.trigger("message", event.data);
});


if(autoReconnect && !reconnectTimer){
reconnectTimer = setInterval(
function(){
if(!ws.readyState == WebSocket.OPEN)
self.connect(url);
},
5000
);
}

return openPromise;
};

this.whenConnected = function(callback, context){
this.on("open", function(){callback.call(context);});

if(ws.readyState == WebSocket.OPEN){
callback.call(context);
}
};

this.send = function(data){
return ws.send(data);
};
Expand All @@ -68,9 +100,42 @@
}
};

var JsonTransport = function(phpws){
Client.prototype = new Emitter();

var Room = function(client, transport, name){
var subscribed = false;

this.on = function(event, callback){
// Subscribe if we haven't done that yet
if(!subscribed){
throw Error("Not subscribed to room " + name);
}

this.constructor.prototype.on.apply(this, arguments);
return this;
};

this.emit = function(event, data){
transport.emit(name, event, data);
return this;
};

this.subscribe = function(){
client.whenConnected(function(){
console.log("Subscribing to room " + name);
transport.emit(name, "subscribe");
subscribed = true;
});

return this;
};
};

Room.prototype = new Emitter();

var EventTransport = function(phpws){
var self = this;
var callbacks = {};
var rooms = {};
var previousTag = 0;
var generateTag = function(){
previousTag += 1;
Expand All @@ -82,14 +147,6 @@
phpws.send(JSON.stringify(obj));
};

var triggerEvent = function(event){
if(event in callbacks){
callbacks[event].fireWith(this, Array.prototype.slice.call(arguments, 1));
}

return this;
};

var addReply = function(obj){
obj.reply = function(data){
var msg = {
Expand All @@ -107,24 +164,18 @@
var messageObj = JSON.parse(message);
addReply(messageObj);

self.trigger(messageObj.event, messageObj);

triggerEvent(messageObj.event, messageObj);
if('room' in messageObj){
self.room(message.room).trigger(messageObj.event, messageObj);
}
});

this.on = function(event, callback){
if(!(event in callbacks))
callbacks[event] = $.Callbacks();

callbacks[event].add.apply(callbacks[event], Array.prototype.slice.call(arguments, 1));
return this;
};

this.unsubscribe = function(event, callback){
if(event in callbacks){
callbacks[event].remove.apply(callbacks[event], Array.prototype.slice.call(arguments, 1));
}
this.room = function(name){
if(!(name in rooms))
rooms[name] = new Room(phpws, this, name);

return this;
return rooms[name];
};

this.emit = function(room, event, args){
Expand All @@ -136,12 +187,16 @@
};

sendObj(msg);

return this;
}
};

EventTransport.prototype = new Emitter();

window.Phpws = {
Client: Client,
RemoteEvents: JsonTransport
RemoteEvents: EventTransport
};

}(window.jQuery);
1 change: 0 additions & 1 deletion src/Devristo/Phpws/Messaging/RemoteEventMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public function toJson(){
'room' => $this->getRoom(),
'event' => $this->getEvent()
));
return $JsonMessage;
}

/**
Expand Down

0 comments on commit 7f38483

Please sign in to comment.