#ruby-on-rails #websocket #rubygems #production-environment #ruby-on-rails-4.1
#ruby-on-rails #websocket #rubygems #производственная среда #ruby-on-rails-4.1
Вопрос:
Я пытаюсь внедрить систему, подобную чату, в своем приложении, используя websocket-rails. Мой Gemfile
выглядит так :
Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.1'
# Use sqlite3 as the database for Active Record
gem 'mysql2'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
group :development, :test do
gem 'byebug'
gem 'annotate'
gem 'better_errors'
gem 'bullet'
gem 'railroady'
gem 'quiet_assets'
gem 'jazz_hands'
end
gem 'thin'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
gem 'unicorn' , group: :production
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'capistrano','~> 2.15.5' , group: :development
gem 'rvm-capistrano'
gem 'capistrano-ext'
# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'lorempixum', '~> 0.0.3'
gem 'kaminari'
gem "opentok", "~> 2.2"
gem 'slim'
gem 'pg'
gem 'rails_12factor', group: :production
gem 'private_pub'
gem 'devise'
gem 'redis'
gem 'websocket-rails'
и
config/initializers/websocket_rails.rb
WebsocketRails.setup do |config|
# Uncomment to override the default log level. The log level can be
# any of the standard Logger log levels. By default it will mirror the
# current Rails environment log level.
# config.log_level = :debug
# Uncomment to change the default log file path.
# config.log_path = "#{Rails.root}/log/websocket_rails.log"
# Set to true if you wish to log the internal websocket_rails events
# such as the keepalive `websocket_rails.ping` event.
# config.log_internal_events = false
# Change to true to enable standalone server mode
# Start the standalone server with rake websocket_rails:start_server
# * Requires Redis
config.standalone = true
# Change to true to enable channel synchronization between
# multiple server instances.
# * Requires Redis.
config.synchronize = false
# Prevent Thin from daemonizing (default is true)
# config.daemonize = false
# Uncomment and edit to point to a different redis instance.
# Will not be used unless standalone or synchronization mode
# is enabled.
# config.redis_options = {:host => 'localhost', :port => '6379'}
# By default, all subscribers in to a channel will be removed
# when that channel is made private. If you don't wish active
# subscribers to be removed from a previously public channel
# when making it private, set the following to true.
# config.keep_subscribers_when_private = false
# Set to true if you wish to broadcast channel subscriber_join and
# subscriber_part events. All subscribers of a channel will be
# notified when other clients join and part the channel. If you are
# using the UserManager, the current_user object will be sent along
# with the event.
# config.broadcast_subscriber_events = true
# Used as the key for the WebsocketRails.users Hash. This method
# will be called on the `current_user` object in your controller
# if one exists. If `current_user` does not exist or does not
# respond to the identifier, the key will default to `connection.id`
# config.user_identifier = :id
# Uncomment and change this option to override the class associated
# with your `current_user` object. This class will be used when
# synchronization is enabled and you trigger events from background
# jobs using the WebsocketRails.users UserManager.
# config.user_class = User
# Supporting HTTP streaming on Internet Explorer versions 8 amp; 9
# requires CORS to be enabled for GET "/websocket" request.
# List here the origin domains allowed to perform the request.
# config.allowed_origins = ['http://localhost:3000']
config.standalone_port = 3245
end
На локальном компьютере все работает нормально, запустив сервер, как описаноhttps://github.com/websocket-rails/websocket-rails/wiki/Standalone-Server —
$ bundle exec rake websocket_rails:start_server
Websocket Rails Standalone Server listening on port 3245
$ ps -ef | grep 3245
sahil 8719 2290 4 11:50 ? 00:00:02 thin server (0.0.0.0:3245) [websocket_rails]
sahil 8771 3688 0 11:51 pts/1 00:00:00 grep 3245
Но то же самое на сервере возвращается как
$ ps -ef | grep 3245
deploy 7249 1348 0 02:23 pts/0 00:00:00 grep --color=auto 3245
Очевидно, что тонкий сервер для websocket_rails не запущен. В чем может быть проблема на рабочем сервере?
Комментарии:
1. пакет exec rake websocket_rails:start_server RAILS_ENV=производство сработало
2. Привет, Сахил, я столкнулся с аналогичной проблемой. Мои серверы, включая thin, passenger и nginx, работают нормально, но я не могу передать запрос websocket через прокси, поэтому он перенаправляется на thin. Вот ссылка на проблему, с которой я столкнулся -> github.com/websocket-rails/websocket-rails/issues/… Сталкивались ли вы с этой проблемой ? Как вы с этим разобрались. Дай мне знать. Спасибо
Ответ №1:
Вероятно, у вас не установлена переменная среды. Для решения аналогичных проблем на нашем рабочем сервере все приложения rails запускаются под одним пользователем с именем «passenger». У этого пользователя есть определение переменной среды в чтении сценария профиля:
export RAILS_ENV="production"
Мы сделали то же самое для нашего пользователя capistrano, поэтому предварительная компиляция ресурсов, серверные задачи и т.д. Выполняются в правильной среде.