Is there any tutorial on how to use Sock.js with Ember.js? I can't find any. Any example, tutorial or guide would be nice. I can use sock.js in plain js like:
var sock = new SockJS('/echo');
sock.onmessage = function(e){
console.log(e.data);
}
sendButton.onclick = function() {
var text = field.value;
sock.send(text);
}
But don't know how to use it with Ember.js
In Ember you'd probably want to have a high level controller/route that sets up the sock. Then when a message comes in, assuming that the data
property of the message is the JSON representation of a model you can use push
on the store
. Something like this should do it:
App.Message = DS.Model.extend({...});
App.ApplicationRoute = Ember.Route.extend({
setupController : function(controller, model){
var sock = new SockJS('/echo');
var store = this.get('store');
sock.onmessage = function(e){
store.push('message',e.data);
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.