-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from thoov/socketio
Socket.IO intergration
- Loading branch information
Showing
22 changed files
with
348 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,8 @@ | |
"document", | ||
"window", | ||
"-Promise", | ||
"URI" | ||
"URI", | ||
"io" | ||
], | ||
"browser": true, | ||
"boss": true, | ||
|
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Ember from 'ember'; | ||
|
||
export default Ember.ObjectProxy.extend({ | ||
/* | ||
* This method simply passes the arguments to the socketio on method except it binds the callback function to | ||
* the run loop. | ||
*/ | ||
on(type, callbackFn, context) { | ||
this.socket.on(type, Ember.run.bind(context, callbackFn)); | ||
}, | ||
|
||
/* | ||
* This method passes the argument to the socketio emit method. If an acknowledgement function is passed then | ||
* we bind that in a run loop. | ||
*/ | ||
emit(channel, data, acknowledgementFn, context) { | ||
if(acknowledgementFn && context) { | ||
this.socket.emit.call(this.socket, channel, data, Ember.run.bind(context, acknowledgementFn)); | ||
} | ||
else { | ||
this.socket.emit.apply(this.socket, arguments); | ||
} | ||
}, | ||
|
||
send() { | ||
this.socket.send.apply(this.socket, arguments); | ||
}, | ||
|
||
close() { | ||
this.socket.close.apply(this.socket, arguments); | ||
}, | ||
|
||
connect() { | ||
this.socket.connect.apply(this.socket, arguments); | ||
} | ||
}); |
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import Ember from 'ember'; | ||
import SocketIOProxy from 'ember-websockets/helpers/socketio-proxy'; | ||
|
||
var filter = Array.prototype.filter; | ||
|
||
export default Ember.Service.extend({ | ||
/* | ||
* Each element in the array is of the form: | ||
* | ||
* { | ||
* url: 'string' | ||
* socket: SocketIO Proxy object | ||
* } | ||
*/ | ||
sockets: null, | ||
|
||
init() { | ||
this._super(...arguments); | ||
this.sockets = Ember.A(); | ||
}, | ||
|
||
/* | ||
* 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, options = {}) { | ||
var proxy = this.findSocketInCache(this.get('sockets'), url); | ||
|
||
if (proxy && this.socketIsNotClosed(proxy.socket)) { return proxy.socket; } | ||
|
||
proxy = SocketIOProxy.create({ | ||
content: this, | ||
socket: io(this.normalizeURL(url), options) | ||
}); | ||
|
||
this.get('sockets').pushObject({ | ||
url: this.normalizeURL(url), | ||
socket: proxy | ||
}); | ||
|
||
return proxy; | ||
}, | ||
|
||
/* | ||
* The native websocket object will transform urls without a pathname to have just a /. | ||
* As an example: ws://localhost:8080 would actually be ws://localhost:8080/ but ws://example.com/foo would not | ||
* change. This function does this transformation to stay inline with the native websocket implementation. | ||
* | ||
*/ | ||
normalizeURL(url) { | ||
var parsedUrl = new URI(url); | ||
|
||
if(parsedUrl.path() === '/' && url.slice(-1) !== '/') { | ||
return url + '/'; | ||
} | ||
|
||
return url; | ||
}, | ||
|
||
socketIsNotClosed(socket) { | ||
return socket.socket.io.readyState !== 'closed'; | ||
}, | ||
|
||
/* | ||
* Returns the socket object from the cache if one matches the url else undefined | ||
*/ | ||
findSocketInCache(socketsCache, url) { | ||
var cachedResults = filter.call(socketsCache, websocket => { | ||
return websocket['url'] === this.normalizeURL(url); | ||
}); | ||
|
||
if(cachedResults.length > 0) { | ||
return cachedResults[0]; | ||
} | ||
} | ||
}); |
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
normalizeEntityName: function() {}, | ||
|
||
afterInstall: function() { | ||
return this.addBowerPackageToProject('socket.io-client'); | ||
} | ||
}; |
This file contains 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 characters
Oops, something went wrong.