我的Rails聊天应用程序的FAYE_URL值应该是什么? [英] What should be my FAYE_URL value for Rails chat application?

查看:81
本文介绍了我的Rails聊天应用程序的FAYE_URL值应该是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用"private_pub" gem创建一个基于Rails的聊天Web应用程序,该应用程序可以在我的本地主机服务器上完美运行.现在,我的网站托管在DigitalOcean上,我想将聊天代码推送到服务器上,以查看实时聊天应用程序将如何工作.

I am trying to make a Chat web application based on Rails using "private_pub" gem which works perfectly on my localhost server. Now my site is hosted on DigitalOcean and i want to push chat code on the server to see how real-time chat app will work.

这是我的private_pub.yml文件

This is my private_pub.yml file

development:
 server: "http://localhost:9292/faye"
 secret_token: "secret"
test:
 server: "http://localhost:9292/faye"
 secret_token: "secret"
staging:
 server: <%= ENV["FAYE_URL"] %>
 secret_token:"secret_key"
 signature_expiration: 3600 # one hour
production:
 server: <%= ENV["FAYE_URL"] %>
 secret_token: "secret_key"
 signature_expiration: 3600 # one hour

我的问题是我应该怎么做才能使其在任何Linux服务器上工作(对我而言,这里是Digital Ocean).我正在DigitalOcean上使用Nginx服务器.

My question is What should i have to do to make it work on any Linux server ( Here Digital Ocean for me). I am using Nginx server on DigitalOcean.

private_pub.yml文件中FAYE_URL的值应该是什么?

What should be the value for FAYE_URL in private_pub.yml file?

rackup private_pub.ru -s thin -E production

我必须在服务器终端上运行rack命令吗?还是还有其他方法可以将Faye托管在其他服务器上?

Do i have to run rack command on my server terminal? Or is there any other way to host Faye on a different server then?

推荐答案

我不知道Digital Ocean的服务器.
我曾经在操作系统Ubuntu 14.04上使用Google Cloud Engine虚拟机.

I don't know Digital Ocean's servers.
I have worked with a Google Cloud Engine virtual machine with OS Ubuntu 14.04.

这是我配置Nip的Nginx Web服务器上的HTTPS中使用Faye和dns解析器的pub/sub应用程序的方式.

Here's how I configured my pub/sub app that uses Faye with a dns resolver in HTTPS on Nginx web server with Passenger.

我将Faye服务器设置为Rack应用程序,以便在启动时通过"Thin" Web服务器自动启动.

I set up the Faye server, as a Rack application, to start automatically at boot through the 'Thin' web server.

使用参数启动/停止Thin的脚本:thin配置文件,faye-rackup-server-script.

Script to start/stop Thin with params: thin-configuration-file, faye-rackup-server-script.

/home/user/apps/myapp/config/thin.sh

#!/bin/sh
set -e
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/user/apps/myapp
PID=$APP_ROOT/tmp/pids/thin.pid
CMD="cd $APP_ROOT; thin -C $APP_ROOT/config/thin.yml -R $APP_ROOT/faye.ru"
AS_USER=user
set -u
startme() {
  run "$CMD start"
}
stopme() {
  run "thin stop"
}
run () {
  if [ "$(id -un)" = "$AS_USER" ]; then
    eval $1
  else
    su -c "$1" - $AS_USER
  fi
}
case "$1" in
  start)   startme ;;
  stop)    stopme ;;    
  restart) stopme; startme ;;
  *) echo "usage: $0 start|stop|restart" >&2
  exit 1
  ;
esac

所以我设置了脚本Thin.sh的执行权限

so I set the execution permissions for the script thin.sh

chmod ugo+x /home/user/apps/myappconfig/thin.sh

-rwxr-xr-x 1个用户... thin.sh *

然后我将脚本Thin.sh定义为服务

then I defined the script thin.sh as a service

cd /etc/init.d
sudo ln -s /home/user/apps/myapp/config/thin.sh thin

lrwxrwxrwx 1 root root ... Thin->/home/user/apps/myapp/config/thin.sh *

最后我在启动时配置了启动

and finally I configured the boot on startup

sudo update-rc.d thin defaults

我已经定义了脚本Thin.sh所需的参数(作为config/thin.sh中$ CMD中的参数)

I have defined the parameters necessary for the script thin.sh (as params in $CMD in config/thin.sh)

首先是瘦Web服务器配置文件

First the thin web server configuration file

/home/user/apps/myapp/config/thin.yml

chdir: "/home/user/apps/myapp"
environment: production
address: 127.0.0.1
port: 9292
timeout: 30
wait: 30
max_conns: 1024
log: /home/user/apps/myapp/log/thin.log
pid: /home/user/apps/myapp/tmp/pids/thin.pid
max_persistent_conns: 100
require: []
threadpool_size: 20
daemonize: true
ssl: true
ssl-key-file: "/etc/ssl/private/example_com.key"
ssl-cert-file: "/etc/ssl/certs/example_com.crt"
#ssl-disable-verify: true

然后是Faye服务器启动脚本(通过机架)

and then the Faye server startup script (via rackup)

/home/user/apps/myapp/faye.ru

require 'eventmachine'
require 'rack'
require 'thin'
require 'faye'
# set FAYE_TOKEN you prefer for faye authentication
require File.expand_path('../config/initializers/faye_token.rb', __FILE__)
Faye::WebSocket.load_adapter('thin')
#define authentication extension 
class ServerAuth
  def incoming(message, callback)
    if message['channel'] !~ %r{^/meta/}
      if !message['ext'].nil?
        if message['ext']['auth_token'] != FAYE_TOKEN
          message['error'] = 'Invalid authentication token'
        end
      end
    end
    if message['channel'] =~ %r{/meta/subscribe}
      ...
    end
    callback.call(message)
  end
end
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 30)
faye_server.add_extension(ServerAuth.new)
run faye_server

这时将设置faye服务器的自动启动.
在启动"/etc/init.d/thin"服务时,使用config/thin.yml执行脚本"faye.ru"启动"thin server".

at this point the automatic start of faye server is set.
At boot '/etc/init.d/thin' service start 'thin server' with config/thin.yml executnig rackup script 'faye.ru'.

我参数化了faye客户的网址

I parametrized the urls for faye client

/home/user/apps/myapp/config/initializers/urls.rb

FAYE_MOUNTPOINT_URL='https://example.com/faye'
FAYE_JSCLIENT_URL='https://example.com/faye/client.js'

我在

/home/user/apps/myapp/app/views/layouts/application.html.erb

因此,当您访问应用程序时会向他们收费

so then they are charged when you access the app

<!DOCTYPE html>
<html lang="it">
<head>
  <%= javascript_include_tag FAYE_JSCLIENT_URL %>
</head>
...

当您访问有聊天室(作为频道)的页面时,将创建Faye客户端.
正确的地方是

The Faye client is created when you access the page where there are your chat rooms (as channel).
The right place is

/home/user/apps/myapp/app/assets/javascript/application.js

...
var faye = null;
$(document).ready(function(){
  // https
  faye = new Faye.Client('<%= FAYE_MOUNTPOINT_URL %>');
  faye.disable('autodisconnect');
  // divs with '.subscribe' class contain channels name
  $('.subscribe').each(function() {
    // subscribe each channel
    faye.subscribe('/'+$(this).text(), function (data) {
      // here I process 'data' to refresh page with new content using jquery
      // I'm using hidden div
      // when I receive notification I clone hidden div, then I fill some fields and I exhibit the new cloned div
      ...
      msg = $('#message_template').clone();
      msg.html(data.message.content);
      msg.css('visibility','visible');
    });
  });
});

从客户端向服务器Faye发送新消息:
(使用远程呼叫创建新的消息模型:true(ajax))

Sending a new message from client to server Faye:
(creating a new Message model with a call remote: true (ajax))

由controller_message#create触发的create.js响应作为ajax响应渲染器调用

called by create.js rensponse triggerd by controller_message#create as ajax response renderer

/home/user/apps/myapp/app/helpers/application_helper.rb

def broadcast(channel, <your_params>)
  # prepare 'data' as json format
  data = { your_key: your_params.as_json }
  message = { channel: '/' + channel.name, data: data, ext: { auth_token: FAYE_TOKEN } }
  # https
  uri = URI.parse(FAYE_MOUNTPOINT_URL)
  Net::HTTP.post_form(uri, message: message.to_json, use_ssl: true)
end

仅此而已

为了更好的理解,我附上了Nginx配置文件

For a better comprehension, I enclose Nginx configuration file

worker_processes  1;
error_log   logs/err.log debug;
events {
  worker_connections  1024;
}
http {
  upstream faye {
    server unix:/tmp/faye.0.sock;
    server unix:/tmp/faye.1.sock;
  }
passenger_root /home/user/.rvm/gems/ruby-2.3.0/gems/passenger-5.1.11;
server {
  listen 80;
  server_name example.com;
  return 301 https://example.com/$request_uri;
}
server {
  listen 443 ssl default_server;
  server_name example.com;
  ssl_certificate   /etc/ssl/certs/example_com.crt;
  ssl_certificate_key /etc/ssl/private/example_com.key;
  ssl on;
  passenger_enabled on;
  passenger_app_root /home/user/apps/myapp;
  passenger_ruby /home/user/.rvm/gems/ruby-2.3.0/wrappers/ruby;
  location / {
    root /home/user/apps/myapp/public;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   html;
  }
  location /faye {
    proxy_pass https://127.0.0.1:9292/faye;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_redirect off;
  }
}

这篇关于我的Rails聊天应用程序的FAYE_URL值应该是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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