我正在尝试使用haproxy负载平衡我的websocket机架应用程序。
我使用redis-cli在通道rates中发布消息,这成功了puts "sent" if ws.send(msg)。
客户端确实收到了'Welcome! from server'消息,所以我知道初始握手已经完成。
但是,客户端从未在信道“速率”中收到已发布的消息。
web_socket.rb
require 'faye/websocket'
module WebSocket
class App
KEEPALIVE_TIME = 15 # in seconds
def initialize(app)
@app = app
@mutex = Mutex.new
@clients = []
# @redis = Redis.new(host: 'rds', port: 6739)
Thread.new do
@redis_sub = Redis.new(host: 'rds', port: 6379)
@redis_sub.subscribe('rates') do |on|
on.message do |channel, msg|
p [msg,@clients.length]
@mutex.synchronize do
@clients.each do |ws|
# ws.ping 'Mic check, one, two' do
p ws
puts "sent" if ws.send(msg)
# end
end
end
end
end
end
end
def call(env)
if Faye::WebSocket.websocket?(env)
# WebSockets logic goes here
ws = Faye::WebSocket.new(env, nil) # {ping: KEEPALIVE_TIME }
ws.on :open do |event|
p [:open, ENV['APPID'], ws.object_id]
ws.ping 'Mic check, one, two' do
# fires when pong is received
puts "Welcome sent" if ws.send('Welcome! from server')
@mutex.synchronize do
@clients << ws
end
p [@clients.length, ' Client Connected']
end
end
ws.on :close do |event|
p [:close, ENV['APPID'], ws.object_id, event.code, event.reason]
@mutex.synchronize do
@clients.delete(ws)
end
p @clients.length
ws = nil
end
ws.on :message do |event|
p [:message, event.data]
# @clients.each {|client| client.send(event.data) }
end
# Return async Rack response
ws.rack_response
else
@app.call(env)
end
end
end
endMy haproxy.cfg
frontend http
bind *:8080
mode http
timeout client 1000s
use_backend all
backend all
mode http
timeout server 1000s
timeout tunnel 1000s
timeout connect 1000s
server s1 app1:8080
server s2 app2:8080
server s3 app3:8080
server s4 app4:8080Chrome开发工具

请帮帮我!
编辑:我尝试过Thread running in Middleware is using old version of parent's instance variable,但这不起作用。
如前所述。下面的代码成功
puts "sent" if ws.send(msg)
发布于 2020-09-06 06:50:29
好吧,经过大量的搜索和测试..。我发现问题是不设一个ping。在服务器的websocket初始化过程中。
改变这个
ws = Faye::WebSocket.new(env, nil) # {ping: KEEPALIVE_TIME }
至
ws = Faye::WebSocket.new(env, nil, {ping: KEEPALIVE_TIME })
我的KEEPALIVE_TIME是0.5,因为我正在制作一个利率变化非常快的股票应用程序。你可以根据你的需要来做。
https://stackoverflow.com/questions/63757851
复制相似问题