在模型的 ember 指南中它说 ( 1 ):
Ember Data is also designed to work with streaming APIs like socket.io, Firebase, or WebSockets. You can open a socket to your server and push changes to records into the store whenever they occur.
我尝试编写一个使用 websocket 的自定义适配器,但我没有取得太大进展。我在任何地方都找不到任何工作示例。
这是我完全未完成的原型(prototype):
DS.WSAdapter = DS.Adapter.extend(Ember.Evented, {
websocket: undefined,
init: function () {
if(this.websocket === undefined)
{
this.websocket = new WebSocket('ws://localhost:8887');
this.websocket.onopen = function(e) {
console.log("Connection established!");
};
this.websocket.onmessage = function(e) {
// What to do here?
};
}
this._loadData();
},
//....
有人可以帮我解决 websocket 适配器吗?
我的主要问题是当 websocket.onmessage()
被执行时,我不知道该做什么。我什至无法访问商店(使用 DS.get('defaultStore')
)或任何东西
最佳答案
我没有在 Ember 中直接使用套接字的经验,但是我最近完成了一个 Ember Data + Firebase 适配器,它应该遵循非常相似的方法。
至少,您应该能够将其用作灵感:
https://github.com/sandersonet/ember-data-firebase
Firebase 确实从下面的套接字中提供了额外的抽象层,但方法非常相似。
关于javascript - 使用 Websockets 的 Ember 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19390319/