我想通了,所以希望这会在未来对其他人有所帮助。
我在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(),转而使用我自己的频道/房间,命名约定类似:sails_model_create_tasklist:group_1,并创建了我自己的groupPublishCreate() 函数。
在TasklistController 的find 函数(不是findOne)的末尾,我将该req/socket 订阅到我的自定义组房间,而不是使用.watch();它也是单线的,所以还不错。
在控制器的 create 函数中,我嵌套了一个 groupPublishCreate 函数,该函数使用 sails.sockets.addRoomMembersToRooms 为我的自定义房间中的每个人订阅 :update、:destroy 和 :message 房间,以获取添加的新记录。
从现在开始,当这条记录发生变化时,他们会得到更新,但他们仍然需要得到一条创建的消息——他们还不知道刚刚添加了一条新记录。现在,我只需使用 Sails 使用的相同数据格式将新记录广播到我的自定义房间,因此(对客户而言)它看起来就像普通的 Sails“创建”消息。
在控制器的 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)
在控制器的 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)
})