Skip to content

Commit

Permalink
Adding send method
Browse files Browse the repository at this point in the history
  • Loading branch information
thoov committed Jun 9, 2015
1 parent 6bd10a8 commit b25aeb0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
9 changes: 8 additions & 1 deletion addon/helpers/socketio-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ export default Ember.ObjectProxy.extend({
this.socket.emit(channel, data, Ember.run.bind(context, acknowledgementFn));
}
else {
this.socket.emit(channel, data);
this.socket.emit.apply(null, arguments);
}
},

/*
* A pass through method for the underlining send method.
*/
send() {
this.socket.apply(null, arguments);
}
});
21 changes: 10 additions & 11 deletions app/services/socketio.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import SocketIOProxy from 'ember-websockets/helpers/socketio-proxy';
var filter = Ember.EnumerableUtils.filter;

export default Ember.Service.extend({

/*
* Each element in the array is of form:
* Each element in the array is of the form:
*
* {
* url: 'string'
* socket: WebSocket Proxy object
* socket: SocketIO Proxy object
* }
*/
sockets: null,
Expand All @@ -21,18 +20,18 @@ export default Ember.Service.extend({
},

/*
* socketFor returns a websocket proxy object. On this object there is a property `socket`
* which contains the actual websocket object. This websocket object is cached based off of the url meaning
* multiple requests for the same socket will return the same object.
* socketFor returns a socketio proxy object. On this object there is a property `socket`
* which contains the actual socketio object. This socketio object is cached based off of the
* url meaning multiple requests for the same socket will return the same object.
*/
socketFor(url) {
socketFor(url, options = {}) {
var proxy = this.findSocketInCache(this.get('sockets'), url);

if (proxy && this.websocketIsNotClosed(proxy.socket)) { return proxy.socket; }
if (proxy && this.socketIsNotClosed(proxy.socket)) { return proxy.socket; }

proxy = SocketIOProxy.create({
content: this,
socket: io(this.normalizeURL(url))
socket: io(this.normalizeURL(url), options)
});

this.get('sockets').pushObject({
Expand All @@ -59,8 +58,8 @@ export default Ember.Service.extend({
return url;
},

websocketIsNotClosed(websocket) {
return websocket.socket.readyState !== window.WebSocket.CLOSED;
socketIsNotClosed(socket) {
return socket.socket.io.readyState !== 'closed';
},

/*
Expand Down
4 changes: 1 addition & 3 deletions app/services/websockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ var forEach = Ember.EnumerableUtils.forEach;
var filter = Ember.EnumerableUtils.filter;

export default Ember.Service.extend({

/*
* Each element in the array is of form:
* Each element in the array is of the form:
*
* {
* url: 'string'
Expand All @@ -27,7 +26,6 @@ export default Ember.Service.extend({
* multiple requests for the same socket will return the same object.
*/
socketFor(url) {
var sockets;
var proxy = this.findSocketInCache(this.get('sockets'), url);

if (proxy && this.websocketIsNotClosed(proxy.socket)) { return proxy.socket; }
Expand Down

0 comments on commit b25aeb0

Please sign in to comment.