0

在我的 rails 应用程序中,aUser有一个配置文件,并且该配置文件可以公开访问。我一直在考虑使用 Pusher gem ( https://github.com/pusher/pusher-gem ) 在我的应用程序中使用即插即用 websocket。

基本上我想要它,以便如果用户正在查看公共个人资料并且该个人资料的所有者碰巧更新了该个人资料,那么前端会使用新信息进行更新,并且用户会在前端收到有关更新。

任何关于从哪里开始的帮助都会很棒!

4

1 回答 1

1

每个公共配置文件都有一个唯一的频道名称,例如<user_name>-profile

每当用户的个人资料发生更新时,就会在user_name用户的频道上触发一个事件,传递更新的数据。

data = update_profile()
Pusher.trigger( '<user_name>-profile', 'profile-updated', {:profile => data} )

在运行浏览器的个人资料页面上,只有在相关频道上监听更新的代码:

var pusher = new Pusher( APP_KEY );
var channel = pusher.subscribe( '<user_name>-profile' );
channel.bind( 'profile-updated', function( update ) {
  // Update UI to show new profile information
  // Show something to indicate that an update has occurred
} );

这里的一个问题是,即使没有人查看公共配置文件,您也会触发一个事件。如果您想解决这个问题,您需要使用WebHooks并跟踪配置文件通道是否存在,occupied并且仅在存在时触发事件。

于 2013-10-05T11:20:29.643 回答