例如,如果我有一个名为 Tasklist 的模型并执行以下操作:
io.socket.get('/tasklist','',function(data,res){
//在这里做事
})
我使用自定义 Controller 操作,因此客户端将接收列表中的所有当前任务其中 GROUP 字段与用户所属的组匹配。另外,它订阅每条记录,以便收到更改通知。但是,如果我 Tasklist.watch(req),客户端将收到通知并订阅每条新记录,而不仅仅是与用户组匹配的记录。
我只希望用户收到允许他们查看的记录。我意识到我可以忽略客户端上的这些消息,但框架仍然会显示在 chrome 调试工具中,并且客户端忽略不是正确的方法。 Sails 是否有首选或内置的方法来执行此操作?从文档和搜索中,我没有找到任何具体的内容。
当我回到电脑前时我会尝试这个...
// for each group user is a member of...
sails.sockets.join(req, 'tasklist:'+group, cb);
// for each POST/CREATE...
sails.sockets.broadcast('tasklist:'+group, 'CREATED', taskID)
// The clients will
io.socket.get('/tasklist/'+taskID)
for the record details and will then be subscribed
如果有比让每个客户端订阅单独记录更好的方法,也许使用 addRoomMembersToRooms 会很棒。我只是不知道 Sails 使用/生成的房间名称格式,例如 Tasklist.subscribe()
谢谢!
最佳答案
我想通了,希望这能在未来帮助其他人。
我在node中使用了console.log(io.sockets.adapter.rooms)
来查找房间的名称,命名约定是这样的:(382是记录ID)
sails_model_tasklist_382:update // notices about updates to record 382
sails_model_tasklist_382:destroy // notices about record 382 being deleted
sails_model_tasklist_382:message // misc msgs you may send about record 382
sails_model_create_tasklist: // EVERY record addeded to this table/view
// whether you should get them or not
我放弃了 Tasklist.PublishCreate()
,而是使用我自己的 channel /房间,并具有类似的命名约定:sails_model_create_tasklist:group_1
,并创建了我自己的 groupPublishCreate()
函数。
在 TasklistController 的 find
函数(不是 findOne
)的末尾,我将该 req/socket 订阅到我的自定义群组房间,而不是使用 .watch()
;这也是一句单行话,所以还不错。
在 Controller 的 create
函数中,我嵌套了一个 groupPublishCreate 函数,该函数使用 sails.sockets.addRoomMembersToRooms
为自定义房间中的每个人订阅已添加的新记录的 :update
、:destroy
和 :message
房间。
从现在开始,当该记录发生更改时,他们将收到更新,但他们仍然需要获取已创建的消息 - 他们还不知道添加了新记录。现在,我只需使用 Sails 使用的相同数据格式将新记录广播到我的自定义房间,因此(对于客户端)它看起来就像普通的 Sails“已创建”消息。
在 Controller 的 FIND 操作中
// We could use a loop if result.taskGroup was a collection/array
// It's a one-liner, no more difficult than Tasklist.watch(req)
sails.sockets.join(req, 'sails_model_create_tasklist:group_' + result.taskGroup)
// Just to be concise...
// To disable updates (eg: if a user slides a live-update toggle)
// This wouldn't actually go here, put it in the right route
sails.sockets.leave(req, 'sails_model_create_tasklist:group_' + groupIdSentByClient)
在 Controller 的 CREATE 操作中
groupPublishCreate('tasklist', result.taskGroup, result)
function groupPublishCreate(modelIdentity, groupID, record) {
var bcastRoom = 'sails_model_create_' + modelIdentity + ':group_' + groupID
var subscribeToTheseChannels = [
'sails_model_' + modelIdentity + '_' + record.id + ':update',
'sails_model_' + modelIdentity + '_' + record.id + ':destroy',
'sails_model_' + modelIdentity + '_' + record.id + ':message'
]
sails.sockets.addRoomMembersToRooms(
bcastRoom,
subscribeToTheseChannels,
function (err) {
// error handler goes here
}
)
sails.sockets.broadcast(
bcastRoom,
modelIdentity,
{verb: 'created', data: record, id: record.id}
)
}
在客户端
// The incoming frame looks like this (copied from chrome's Network dev tab)
42["tasklist",{"verb":"created","data":{"msg":"this is a test","id":437},"id":437}]
// The incoming frame of a Sails PublishCreate() message
42["tasklist",{"verb":"created","data":{"msg":"this is a test","id":437},"id":437}]
// As you can see, they are identical, and can be parsed the same way
io.socket.on('tasklist',function(msg){
// The CUD is CRUD... R is the Response to a GET
if (msg.verb === 'created') insertNewTask(msg.data)
if (msg.verb === 'updated') updateTaskList(msg.data)
if (msg.verb === 'destroyed') removeTask(msg.id)
})
不相关,但我这样做是为了使用 Sails 作为 jQuery 扩展
jQuerySails = {
extend: function () {
$.API = io.socket
if ($.API) {
return ({success: '$.API is available for use'})
} else {
return ({error: 'Sails was not extended to $.API'})
}
},
remove: function () {
delete $.API
if (!$.API) {
return ({success: '$.API has been removed'})
} else {
return ({error: '$.API is still available for use'})
}
}
}
console.log(jQuerySails.extend())
$.API.on('tasklist',function(msg){
// The CUD is CRUD... R is the Response to a GET
if (msg.verb === 'created') insertNewTask(msg.data)
if (msg.verb === 'updated') updateTaskList(msg.data)
if (msg.verb === 'destroyed') removeTask(msg.id)
})
关于node.js - 使用 SailsJS,有没有办法根据记录中的某个值进行 Model.watch() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42093380/