在Rails中实现Pusher聊天 [英] Implementing Pusher chat in Rails

查看:97
本文介绍了在Rails中实现Pusher聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于最新版本的Rails和Pusher没有太多最新信息,如何在Rails应用程序中实现Pusher以获取用户之间的实时聊天? Pusher文档显示了如何使用Sinatra进行此操作,但没有特定于Rails的内容...

Since there is not much up-to-date information with latest versions of Rails and Pusher, how can I implement Pusher in my Rails app to get realtime chat between users? The Pusher documentation shows how to do this with Sinatra, but there isn't anything specific to Rails...

推荐答案

创建一个初始化文件

app / config / initializers / pusher.rb

app/config/initializers/pusher.rb

require 'pusher'

Pusher.url    = 'your-pusher-url'
Pusher.logger = Rails.logger

在您的控制器中,假设这是聊天消息:

In your controller, lets assume this is for chat messages:

app / controllers / messages_controller.rb

app/controllers/messages_controller.rb

MessagesController < ApplicationController
  def create
    model = Message.create params_model
    json  = model.to_json
    channel = "private-conversation.#{params[:user_id]}"
    Pusher[channel].trigger 'messages/create', json
  end

  private
    def params_model
      params.require(:message).permit(:id,:body,:user_id)
    end
end

授权

app / controllers / pusher_controller.rb

app/controllers/pusher_controller.rb

class PusherController < ApplicationController
  protect_from_forgery except: :auth_notify
  skip_before_filter :login_required, only: [:auth_notify]
  def auth
    channel    = params[:channel_name]
    socket_id  = params[:socket_id]

    valid = false
    valid = true if channel =~ /private-conversation\.(\d+)/

    if valid
      response = Pusher[channel].authenticate socket_id
      render json: response
    else
      render text: 'Not authorized', status: '403'
    end
  end
end

路线:

routes.rb

routes.rb

YourApp::Application.routes.draw do
  resources :messages
  post '/pusher/auth'    => 'pusher#auth'
end

在您的脚本中的某个位置,最有可能在应用程序中,这假设您在application.html.haml和jquery中安装了pusher cdn js文件。

Somewhere in your coffescript, most likely in application.coffee, this assumes you have the pusher cdn js file in your application.html.haml and jquery installed.

$->
    user_id = $('meta[name=user-id]').attr('user_id')
    key     = $('meta[name=pusher-key]').attr('content')
    csrf    = $('meta[name=csrf-token]').attr('content')
    @pusher = new Pusher key,
      auth:
        headers:
          'X-CSRF-Token': csrf
        params:
          user_id: user_id

请注意,您应该在头上添加meta标签,以便轻松获取csrf令牌,user_id和pusher键。当然,您需要使用csrf令牌来停止欺骗。

notice that you should added meta tags to your head so you easily grab csrf token, user_id and pusher key. You of course need csrf token to stop spoofing.

在您的application.html.haml

In your application.html.haml

!!! XML
!!! 5
%html
  %head
    = csrf_meta_tag
    = tag :meta, name: 'pusher-key', content: "#{current_user.id}"
    = tag :meta, name: 'pusher-key', content: 'pusher_public_key'
  %body
    = javascript_include_tag '//js.pusher.com/2.2/pusher.min.js'
    = javascript_include_tag :application

current_user假定您正在使用某种身份验证。 csrf_meta_tag是一个内置的rails助手。注意,我将js放在正文的最后一行。不要将您的js放在头部。

current_user assumes you are using some kind of authenication. csrf_meta_tag is a built in rails helper. Notice that I put my js on the last line of body. do not place your js in the head.

这篇关于在Rails中实现Pusher聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆