频道和密钥之间有什么区别

时间:2013-07-16 13:54:48

标签: javascript goinstant goinstant-platform

我正在使用GoInstant开发一款应用,但键和频道之间的差异并不是很明显。我应该何时使用密钥与渠道?

1 个答案:

答案 0 :(得分:7)

:与键值存储一样,Key对象是您在其中管理和监视GoInstant中的值的接口。您应该将它们用于CRUD(创建,读取,更新删除)。

关键示例:

// We create a new key using our room object
var movieName = yourRoom.key(‘movieName’);

// Prepare a handler for our `on` set event
function setHandler(value) {
    console.log(‘Movie has a new value’, value);
}

// Now when the value of our key is set, our handler will fire
movieName.on(‘set’, setHandler);

// Ready, `set`, GoInstant :)
movieName.set('World War Z', function(err) {
    if (!err) alert('Movie set successfully!')
}

渠道:代表全双工消息传递界面。想象一下多客户端发布/订阅系统。频道不存储数据,您无法从频道中检索消息,您只能接收它。您应该使用它在共享会话的客户端之间传播事件。

频道示例:

var mousePosChannel = yourRoom.channel('mousePosChannel');

// When the mouse moves, broadcast the mouse co-ordinates in our channel
$(window).on('mousemove', function(event) {
  mousePosChannel.message({
    posX: event.pageX,
    posY: event.pageY
  });
});

// Every client in this session can listen for changes to
// any users mouse location
mousePosChannel.on('message', function(msg) {
  console.log('A user in this room has moved there mouse too', msg.posX, msg.posY);
})

您可以在此处找到官方文档:

键:https://developers.goinstant.net/v1/key/index.html

频道:https://developers.goinstant.net/v1/channel/index.html