4

我正在用 GoInstant 开发一个应用程序,但是键和通道之间的区别不是很清楚。我什么时候应该使用键和通道?

4

1 回答 1

7

Keys:与键值存储一样,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!')
}

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

渠道示例:

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

于 2013-07-16T14:03:53.343 回答