1
对于了解yeoman完整堆栈提供的套接字服务,我有点麻烦。我提供了一个如何使用syncUpdate函数的示例,添加了modelPlace变量以知道函数的调用方式。了解yeoman完整堆栈中的套接字服务
所以,这里是一个伪工作空间模型
var working_spaceSchema = new Schema({
...
users: [],
todos: [{name:String,info:String,priority:Number,id:Number}],
chat: [{content:String,poster:String,creation:Date}],
...
});
,我叫我的角度控制器的Socket服务方式:
$http.get('/api/works/' + $location.$$path.substr(7))
.success(function(data) {
$http.post('/api/works/' + $location.$$path.substr(7) + '/connexion', {type:0});
$scope.chats = data;
socket.syncUpdates('work', 'chat', $scope.chats, function(event, chat, chats) {
$scope.chats = chats;
});
});
对于提醒,这里是Socket服务文件。你可以看到modelPlace变量,它使我知道我必须从schema.post同步:
'use strict';
angular.module('yoyoApp')
.factory('socket', function(socketFactory) {
// socket.io now auto-configures its connection when we ommit a connection url
var ioSocket = io(null, {
// Send auth token on connection, you will need to DI the Auth service above
// 'query': 'token=' + Auth.getToken()
});
var socket = socketFactory({
ioSocket: ioSocket
});
return {
socket: socket,
/**
* Register listeners to sync an array with updates on a model
*
* Takes the array we want to sync, the model name that socket updates are sent from,
* and an optional callback function after new items are updated.
*
* @param {String} modelName
* @param {Array} array
* @param {Function} cb
*/
// Should consider givin' $state.params.id to syncUpdates
// check currentUser.state and trigger notification IF =/= from update current state
syncUpdates: function (modelName, modelPlace, array, cb) {
cb = cb || angular.noop;
/**
* Syncs item creation/updates on 'model:save'
*/
socket.on(modelName + ':save', function (item) {
var event = 'created';
if (modelPlace == 'todo_list')
array = item.todos;
else if (modelPlace == 'chat')
array = item.chat;
cb(event, item, array);
});
/**
* Syncs removed items on 'model:remove'
* JE CROIS QUE CE N'EST PAS NECESSAIRE
*/
socket.on(modelName + ':remove', function (item) {
var event = 'deleted';
_.remove(array, {_id: item._id});
cb(event, item, array);
});
},
/**
* Removes listeners for a models updates on the socket
*
* @param modelName
*/
unsyncUpdates: function (modelName, modelPlace) {
socket.removeAllListeners(modelName + ':save:' + modelPlace);
socket.removeAllListeners(modelName + ':remove:' + modelPlace);
}
};
});
添加到“ioSocket变量”的问题,我想知道我应该如何使用syncUpdate就最好的做法是什么(我的不是,显然是......)。
谢谢你们!
你怎么想补充一点,听,当有人“GET”创新事物的“POST(保存和更新)”即角fullstack已有功能的自定义监听器? 详细说明,当某人加入时,您将如何向聊天发送消息? – 2015-04-01 15:16:17