我在rails应用程序上创建了一个实时聊天,我使用EventMachine运行Websocket进行实时聊天。实时聊天可在多台计算机上的localhost上运行。
它还在ubuntu上开发的Amazon EC2实例上工作,但是当我尝试将其转移到生产时,实时聊天不再有效。没有数据来回发送。
这是我的控制器:
@clients = []
EM.run do
EM::WebSocket.start(host: "0.0.0.0", port: 3060) do |ws|
crypt = ActiveSupport::MessageEncryptor.new(ENV['SECRET_KEY_BASE'])
ws.onopen do |handshake|
conversation_data = crypt.decrypt_and_verify(handshake.query_string)
@clients << {socket: ws, conv_info: conversation_data}
end
ws.onclose do
ws.send "Closed."
@clients.delete ws
end
ws.onmessage do |data|
data = data.split('L56HTY9999')
body = data[0]
key = data[-1]
conversation = crypt.decrypt_and_verify(key)
new_message = Message.new(body: body, user_id: conversation[:user_id], conversation_id: conversation[:conversation_id])
if new_message.save
@clients.each do |socket|
if socket[:conv_info][:conversation_id] == conversation[:conversation_id]
socket[:socket].send new_message.chat_show
end
end
else
if socket[:conv_info][:user_id] == conversation[:user_id]
socket[:socket].send new_message.errors.to_json
end
end
end
end
end
这是我在页面上的javascript:
function addMessage(msg) {
var content = JSON.parse(msg);
var context = {
userImage: content.image,
author: content.username,
convoText: content.body,
time: content.created_at
};
var source = $('#add-newest-comment').html();
var template = Handlebars.compile(source);
var newComment = template(context);
$('#container').append(newComment);
}
var key = '<%= @conv_id %>';
var socket, host;
host = "ws://0.0.0.0:3060?"+ key;
function onErrors(msg){
$("#chat-log .error").text( msg);
setTimeout(function() {
$( "#chat-log .error" ).text("");
}, 3000);
}
function socketHealth(msg){
console.log(msg);
}
function connect() {
try {
socket = new WebSocket(host);
socketHealth("Socket State: " + socket.readyState);
socket.onopen = function() {
socketHealth("Socket Status: " + socket.readyState + " (open)");
}
socket.onclose = function() {
socketHealth("Socket Status: " + socket.readyState + " (closed)");
}
socket.onmessage = function(msg) {
addMessage(msg.data);
}
} catch(exception) {
onErrors("Error: " + exception);
}
}
$(function() {
connect();
});
function send() {
var text = $("#message").val();
if (text == '') {
onErrors("Please Enter a Message");
return;
}
try {
socket.send(text + 'L56HTY9999' + key);
} catch(exception) {
onErrors("Failed To Send")
}
$("#message").val('');
}
$('#message').keypress(function(event) {
if (event.keyCode == '13') { send(); }
});
$("#disconnect").click(function() {
socket.close()
});
我使用此命令运行我的服务器:
bundle exec thin start -p 80 -d -l thin.log -P thin.pid
我的网站正在http://puppyplaydate.co/
上线控制台记录此
Socket State: 0
messages:153 WebSocket connection to 'ws://0.0.0.0:3060/?aVI0OFVHS2VET3d4OTQ4M2s4Rk5VMld1eVlXOUFoVUFxT1JMSm5LWDJOUEhScU50MzFub2JMQ3Y4NG1uekl3TS0tYnlBU1ZRclErK3QxWERHN0taSTVzUT09--01ec4a49ce0aeef59bee5d5b8e13d7916abc20e1' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
messages:148 Socket Status: 3 (closed)
我使用netstat来检查服务器是否正在该端口侦听并将其取回:
tcp 0 0 *:3060 *:* LISTEN
我在网上研究过,有人说他们使用了&#34;守护进程&#34;宝石在生产中运行websocket?我不确定这是否是我需要的东西。
我真的卡住了,先谢谢你的帮助!
答案 0 :(得分:1)
问题在于我运行的是Thin服务器而不是Rails,而不是运行Rails服务器,该服务器也被设置为启动Thin。