部署 Faye 部署到服务器上前台握手失败

liguangsong · August 26, 2015 · Last by tianlibo replied at December 24, 2015 · 3787 hits

启动 faye.ru rackup faye.ru -s thin -E production

本地测试挺好的, 但是部署到服务就完蛋了

WebSocket connection to 'ws://blind.codefordream.com:9292/faye' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

这是 faye.ru 代码

require 'faye'
require File.expand_path('../config/initializers/faye_token.rb', __FILE__)
require 'active_record'
require 'mysql2'
require 'yaml'
Faye::WebSocket.load_adapter('thin')
RAILS_ENV = ENV['RACK_ENV']
# require File.expand_path('../app/models/user_chat_room.rb', __FILE__)
require File.expand_path('../app/models/user_chat_list.rb', __FILE__)
require File.expand_path('../app/models/chat_message.rb', __FILE__)
require File.expand_path('../app/models/user_chat_online.rb', __FILE__)

environment = ENV['RACK_ENV'] || 'production'
dbconfig = YAML.load(File.read('config/database.yml'))
ActiveRecord::Base.establish_connection(dbconfig[environment])
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 60)
class MarkOnline
  def incoming(message, callback)
    faye_client ||= Faye::Client.new('http://localhost:9292/faye')

    if message['channel'].match('/chat/*')
      to_is_online= UserChatOnline.check_on_line(message['data']['to'], message['data']['from'], message['data']['content'], message['clientId'])

      if !to_is_online
        UserChatList.change_chat_list(message['data']['to'], message['data']['from'], "false")
        ChatMessage.creat_new_chat_message(message['data']['from'], message['data']['to'], message['data']['content'])
      end
    end

    if message['channel'] == '/meta/subscribe'
      UserChatOnline.create_on_line_user(message['data']["from"], message['clientId'])
    end

    if message['channel'] == '/meta/disconnect'
      UserChatOnline.remove_on_line_user(message['clientId'])
    end

    callback.call(message)
  end
end

faye_server.on(:disconnect) do |client_id|
  UserChatOnline.remove_on_line_user(client_id)
end


faye_server.add_extension(MarkOnline.new)
run faye_server

这是前台配置

var client = new Faye.Client('http://' + document.domain + ':9292' + '/faye');
    client.subscribe('/chat/'+user_name, function (data) {
        get_message_from_server(data.from, data.to, data.content);
        show_from_message("from",data.content)
    });
    client.addExtension({
        outgoing: function (message, callback) {
            if (message.channel == '/meta/subscribe') {
                message.data = {from: user_name};
            }
            if (message.channel == '/meta/disconnect') {
                message.data = {from: user_name};
            }
            callback(message);
        }
    });
    window.client = client;

兄弟你服务器端口开了吗?

@poshboytl 怎么开,我的服务器没有域名是代理过去的

@poshboytl var client = new Faye.Client('http://' + ‘xx.xx.x.xx’ + ':9292' + '/faye'); 这样没有域名的事可以没事了吧?这样可以么?没有效果

@poshboytl

22/tcp   open          ssh
111/tcp  open          rpcbind
1723/tcp open          pptp
3306/tcp open          mysql
3333/tcp open          dec-notes
8080/tcp open          http-proxy
111/udp  open          rpcbind
123/udp  open          ntp
838/udp  open|filtered unknown
5353/udp open|filtered zeroconf

好像没开这个端口

所以你已经有答案了!

@poshboytl 我是应该把 faye 的端口号改了,还是想办法把 9292 的端口号打开

netstat -tanp


Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      4101/mysqld
tcp        0      0 127.0.0.1:9292          0.0.0.0:*

这是打开了吧,还是握手失败

小弟用的的 faye NodeJS 版本实现的,目前运行正常。 可以考虑用 nodeJS 版本写一个最简单的 DEMO,测试成功了再试验下。

socket.io 有内存泄露,faye 有没有这方面的问题?

如果用 1.4 以上版本呢的 nginx,可以直接开 websocket 的代理。

好像知道了没有指定 hostname

#12 楼 @liguangsong 你好,楼主,我也遇到相同的问题,你之前是怎么解决的?

#12 楼 @liguangsong 知道了,多谢提示

You need to Sign in before reply, if you don't have an account, please Sign up first.