[英]SprouteCore with WebSockets
I'm new to Sproutcore but want to use that framework for building my web application that connects to a WebSocket server to send/receive serialized protobuf binary messages. 我是Sproutcore的新手,但想使用该框架来构建连接到WebSocket服务器的Web应用程序,以发送/接收序列化的protobuf二进制消息。
Now I want to know how to configure the datasource in Sproutcore to fetch/commit data to the websocket. 现在,我想知道如何在Sproutcore中配置数据源以将数据提取/提交到Websocket。 If anyone has tried something similar, please share. 如果有人尝试过类似的操作,请分享。
Appreciate your help !! 感谢您的帮助 !!
Thanks 谢谢
I recently created an open-source project to do this with Firebase . 我最近创建了一个开源项目来使用Firebase进行此操作。
Basically, you need to create your DataSource and override the fetch , retrieveRecords and commitRecords methods. 基本上,您需要创建DataSource并覆盖fetch , retrieveRecords和commitRecords方法。
Below is how I did it with Firebase, but you'll definitely need to make modifications to use websockets. 以下是我如何使用Firebase进行的操作,但是您肯定需要进行修改才能使用websocket。
You may also want to checkout the Thoth project. 您可能还想签出Thoth项目。 It hasn't been updated in a while, but it has quite a bit of code dealing with Socket.IO. 它已经有一段时间没有更新了,但是它有很多处理Socket.IO的代码。
sc_require('firebase');
FireCore.DataSource = SC.DataSource.extend({
_createdIdsToIgnore: [],
firebaseApp: null,
firebase: function() {
var app = this.get('firebaseApp');
return new Firebase('https://%@.firebaseio.com/'.fmt(app));
}.property('firebaseApp').cacheable(),
fetch: function(store, query) {
var firebase = this.get('firebase'),
self = this,
record, type, name, ref, hash, id;
type = query.get('recordType');
name = self.firebaseReferenceNameFor(type);
ref = firebase.child(name);
/*
* Detach any existing observers
*/
ref.off();
/*
* Attach our new observers
*/
ref.on('child_added', function(snapshot) {
self.invokeNext(function() {
var id = snapshot.name(),
hash = self.cleanObject(snapshot.val());
/*
* Make sure we don't add a record that was
* just, or is about to be, added by a call
* to commitRecords.
*/
if (self._createdIdsToIgnore.contains(id)) {
self._createdIdsToIgnore.removeObject(id);
} else {
store.loadRecord(type, hash, id);
}
});
self.scheduleRunLoop();
});
ref.on('child_changed', function(snapshot) {
self.invokeNext(function() {
var id = snapshot.name(),
hash = self.cleanObject(snapshot.val());
store.pushRetrieve(type, id, hash);
});
self.scheduleRunLoop();
});
ref.on('child_removed', function(snapshot) {
self.invokeNext(function() {
var id = snapshot.name();
store.pushDestroy(type, id);
});
self.scheduleRunLoop();
});
return YES;
},
retrieveRecords: function(store, keys) {
var firebase = this.get('firebase'),
self = this,
type, name, id, hash, status, ref;
keys.forEach(function(key, index) {
type = store.recordTypeFor(key);
name = self.firebaseReferenceNameFor(type);
id = store.idFor(key);
ref = firebase.child(name).child(id);
ref.once('value', function(snapshot) {
hash = self.cleanObject(snapshot.val());
status = store.readStatus(key);
SC.run(function() {
if (status == SC.Record.BUSY_LOADING) {
store.dataSourceDidComplete(key, hash, id);
} else {
store.pushRetrieve(type, id, hash);
}
});
});
});
return YES;
},
commitRecords: function(store, createKeys, updateKeys, destroyKeys, params) {
var firebase = this.get('firebase'),
self = this,
type, name, hash, ref, newRef, id;
createKeys.forEach(function(key) {
type = store.recordTypeFor(key);
name = self.firebaseReferenceNameFor(type);
hash = self.cleanObject(store.readDataHash(key));
ref = firebase.child(name);
// Allow the user to specify an ID (TODO: check the primaryKey field)
if (hash.guid) {
newRef = ref.child(hash.guid);
id = hash.guid;
} else {
newRef = ref.push();
id = newRef.name();
}
// Ignore this ID in child_added calls so
// we don't add it twice
self._createdIdsToIgnore.pushObject(id);
// Save the data to firebase
newRef.set(hash);
// Then notify the store
SC.run(function() { store.dataSourceDidComplete(key, hash, id); });
});
updateKeys.forEach(function(key) {
type = store.recordTypeFor(key);
name = self.firebaseReferenceNameFor(type);
hash = self.cleanObject(store.readDataHash(key));
id = store.idFor(key);
ref = firebase.child(name).child(id);
ref.set(hash);
SC.run(function() { store.dataSourceDidComplete(key, hash, id); });
});
destroyKeys.forEach(function(key) {
type = store.recordTypeFor(key);
id = store.idFor(key);
name = self.firebaseReferenceNameFor(type);
ref = firebase.child(name).child(id);
ref.remove();
SC.run(function() { store.dataSourceDidDestroy(key); });
});
},
firebaseReferenceNameFor: function(type) {
return (type.toString().split('.')[1]).decamelize().pluralize();
},
scTypeNameFromSnapshot: function(snapshot) {
return snapshot.ref().path.m[0].camelize().singularize();
},
cleanObject: function(object) {
var self = this,
type = SC.typeOf(object),
cleaned = null, key, i, j, tmp;
if (type == SC.T_OBJECT || type == SC.T_HASH) {
cleaned = {};
for (key in object) {
if (object.hasOwnProperty(key) && !key.match(/^_/)) {
cleaned[key] = self.cleanObject(object[key]);
}
}
} else if (type == SC.T_ARRAY) {
cleaned = [];
for (i = 0, j = 0; i < object.length; i++) {
tmp = self.cleanObject(object[i]);
if (tmp) {
cleaned[j] = tmp;
j++;
}
}
} else if (type == SC.T_FUNCTION) {
// Do nothing, assume that functions should be ignored
} else {
cleaned = object;
}
return cleaned;
},
_scheduleRunLoopTimer: null,
scheduleRunLoop: function() {
var self = this;
if (!this._scheduleRunLoopTimer) {
SC.RunLoop.begin();
this._scheduleRunLoopTimer = SC.Timer.schedule({
target: self,
action: '_endRunLoop',
interval: 100
});
SC.RunLoop.end();
}
},
_endRunLoop: function() {
SC.RunLoop.begin(); SC.RunLoop.end();
this._scheduleRunLoopTimer = null;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.