我已经在我的 MAMP 服务器上成功实现了 socket.io 来构建一个聊天应用程序,当我将它推送到生产服务器(数字海洋,laravel forge)时,我收到一个错误:
我根据这篇文章在 forge 中设置了守护进程,并根据这篇文章遵循了安装步骤
我的 socket.js 文件是这样的:
var server = require('http').Server();
var io = require('socket.io')(server);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('test-channel');
redis.on('message', function(channel, message, username) {
console.log(channel + ":" + message);
message = JSON.parse(message);
io.emit(channel + ":" + message.event, message.data); // test-channel:nameOfEvent (UserSignedUp)
});
server.listen(3000);
我的刀片文件:
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/0.12.15/vue.min.js"></script>
<script>
//var socket = io('{{ Request::ip() }}:3000');
var socket = io('server.ip.address:3000'); //initalize socket
socket.on('test-channel:App\\Events\\NewChatMessage', function(message){
//console.log(message.messages);
if(message.messages.company_id == {{ Auth::user()->company_id }}){
if(message.messages.user_id != {{ Auth::user()->id }})
{
var html = '<div class="direct-chat-msg"><div class="direct-chat-info clearfix"><span class="direct-chat-name pull-left">' + message.username + '</span><span class="direct-chat-timestamp pull-right">time</span></div><!-- /.direct-chat-info --><div class="other avatar direct-chat-img">' + message.avatar + '</div><!-- /.direct-chat-img --><div class="direct-chat-text">' + message.messages.body + '</div><!-- /.direct-chat-text --></div><!-- /.direct-chat-msg -->';
} else {
var html = '<div class="direct-chat-msg right"><div class="direct-chat-info clearfix"><span class="direct-chat-name pull-right">' + message.username + '</span><span class="direct-chat-timestamp pull-left">time</span></div><!-- /.direct-chat-info --><div class="avatar direct-chat-img">' + message.avatar + '</div><!-- /.direct-chat-img --><div class="direct-chat-text">' + message.messages.body + '</div><!-- /.direct-chat-text --></div><!-- /.direct-chat-msg --></div><!--/.direct-chat-messages-->';
}
$('.direct-chat-messages').append(html);
$(".direct-chat-messages").animate({ scrollTop: $('.direct-chat-messages').prop("scrollHeight")}, 1000);
}
/*for (var i = 0; i < message.threads.length; i++) {
var counter = message.threads[i];
console.log(counter.subject);
}*/
//this.messages.push(message);
}); //listen for chat.message event and update messages
当我在生产环境中提交聊天消息时,由于socket.js console.log,我在终端中看到它,它只是没有到达前端。如果我要下注(并且可能输掉那个赌注),那是因为我使用的是https://example.com而不是http://example.com。如果这听起来可能是问题所在,我如何确保 socket.js 使用 https 而不是 http?
Laravel 日志显示 redis 和 socket.io 都运行良好。
更新 以下是 chrome 打印的日志,基于我有限的知识,这似乎是客户端问题,以防它帮助某人了解正在发生的事情。
socket.io.min.js:1 socket.io-client:url parse https://example.com:3000 +0ms
socket.io.min.js:1 socket.io-client new io instance for https://example.com:3000 +3ms
socket.io.min.js:1 socket.io-client:manager readyState closed +2ms
socket.io.min.js:1 socket.io-client:manager opening https://example.com:3000 +0ms
socket.io.min.js:1 engine.io-client:socket creating transport "polling" +1ms
socket.io.min.js:1 engine.io-client:polling polling +1ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr poll +0ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr open GET: https://example.com:3000/socket.io/?EIO=3&transport=polling&t=LjBd72n +2ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr data null +0ms
socket.io.min.js:1 engine.io-client:socket setting transport polling +2ms
socket.io.min.js:1 socket.io-client:manager connect attempt will timeout after 20000 +0ms
socket.io.min.js:1 socket.io-client:manager readyState opening +2ms
messages:217 GET https://example.com/files/designloud-inc/users/ 403 ()
messages:1 GET https://example.com:3000/socket.io/?EIO=3&transport=polling&t=LjBd72n net::ERR_CONNECTION_TIMED_OUT
socket.io.min.js:1 engine.io-client:socket socket error {"type":"TransportError","description":0} +8s
socket.io.min.js:1 socket.io-client:manager connect_error +0ms
socket.io.min.js:1 socket.io-client:manager cleanup +1ms
socket.io.min.js:1 socket.io-client:manager will wait 978ms before reconnect attempt +1ms
socket.io.min.js:1 engine.io-client:socket socket close with reason: "transport error" +0ms
socket.io.min.js:1 engine.io-client:polling transport not open - deferring close +1ms
socket.io.min.js:1 socket.io-client:manager attempting reconnect +981ms
socket.io.min.js:1 socket.io-client:manager readyState closed +0ms
socket.io.min.js:1 socket.io-client:manager opening https://example.com:3000 +0ms
socket.io.min.js:1 engine.io-client:socket creating transport "polling" +1ms
socket.io.min.js:1 engine.io-client:polling polling +1ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr poll +0ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr open GET: https://example.com:3000/socket.io/?EIO=3&transport=polling&t=LjBd9Hj +1ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr data null +0ms
socket.io.min.js:1 engine.io-client:socket setting transport polling +1ms
socket.io.min.js:1 socket.io-client:manager connect attempt will timeout after 20000 +1ms
:3000/socket.io/?EIO=3&transport=polling&t=LjBd9Hj:1 GET https://example.com:3000/socket.io/?EIO=3&transport=polling&t=LjBd9Hj net::ERR_CONNECTION_TIMED_OUT
socket.io.min.js:1 engine.io-client:socket socket error {"type":"TransportError","description":0} +8s
socket.io.min.js:1 socket.io-client:manager connect_error +1ms
socket.io.min.js:1 socket.io-client:manager cleanup +0ms
socket.io.min.js:1 socket.io-client:manager reconnect attempt error +1ms
socket.io.min.js:1 socket.io-client:manager will wait 1132ms before reconnect attempt +0ms
socket.io.min.js:1 engine.io-client:socket socket close with reason: "transport error" +1ms
socket.io.min.js:1 engine.io-client:polling transport not open - deferring close +0ms
socket.io.min.js:1 socket.io-client:manager attempting reconnect +1s
socket.io.min.js:1 socket.io-client:manager readyState closed +0ms
socket.io.min.js:1 socket.io-client:manager opening https://example.com:3000 +1ms
socket.io.min.js:1 engine.io-client:socket creating transport "polling" +0ms
socket.io.min.js:1 engine.io-client:polling polling +1ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr poll +0ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr open GET: https://example.com:3000/socket.io/?EIO=3&transport=polling&t=LjBdBXX +1ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr data null +0ms
socket.io.min.js:1 engine.io-client:socket setting transport polling +1ms
socket.io.min.js:1 socket.io-client:manager connect attempt will timeout after 20000 +1ms
:3000/socket.io/?EIO=3&transport=polling&t=LjBdBXX:1 GET https://example.com:3000/socket.io/?EIO=3&transport=polling&t=LjBdBXX net::ERR_CONNECTION_TIMED_OUT
socket.io.min.js:1 engine.io-client:socket socket error {"type":"TransportError","description":0} +8s
socket.io.min.js:1 socket.io-client:manager connect_error +1ms
socket.io.min.js:1 socket.io-client:manager cleanup +0ms
socket.io.min.js:1 socket.io-client:manager reconnect attempt error +1ms
socket.io.min.js:1 socket.io-client:manager will wait 5000ms before reconnect attempt +0ms
socket.io.min.js:1 engine.io-client:socket socket close with reason: "transport error" +1ms
socket.io.min.js:1 engine.io-client:polling transport not open - deferring close +0ms
socket.io.min.js:1 socket.io-client:manager attempting reconnect +5s
socket.io.min.js:1 socket.io-client:manager readyState closed +1ms
socket.io.min.js:1 socket.io-client:manager opening https://example.com:3000 +0ms
socket.io.min.js:1 engine.io-client:socket creating transport "polling" +1ms
socket.io.min.js:1 engine.io-client:polling polling +0ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr poll +1ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr open GET: https://example.com:3000/socket.io/?EIO=3&transport=polling&t=LjBdEkE +0ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr data null +1ms
socket.io.min.js:1 engine.io-client:socket setting transport polling +1ms
socket.io.min.js:1 socket.io-client:manager connect attempt will timeout after 20000 +0ms
:3000/socket.io/?EIO=3&transport=polling&t=LjBdEkE:1 GET https://example.com:3000/socket.io/?EIO=3&transport=polling&t=LjBdEkE net::ERR_CONNECTION_TIMED_OUT
socket.io.min.js:1 engine.io-client:socket socket error {"type":"TransportError","description":0} +5s
socket.io.min.js:1 socket.io-client:manager connect_error +1ms
socket.io.min.js:1 socket.io-client:manager cleanup +0ms
socket.io.min.js:1 socket.io-client:manager reconnect attempt error +1ms
socket.io.min.js:1 socket.io-client:manager will wait 5000ms before reconnect attempt +0ms
socket.io.min.js:1 engine.io-client:socket socket close with reason: "transport error" +1ms
socket.io.min.js:1 engine.io-client:polling transport not open - deferring close +0ms
socket.io.min.js:1 socket.io-client:manager attempting reconnect +5s
socket.io.min.js:1 socket.io-client:manager readyState closed +1ms
socket.io.min.js:1 socket.io-client:manager opening https://example.com:3000 +0ms
socket.io.min.js:1 engine.io-client:socket creating transport "polling" +0ms
socket.io.min.js:1 engine.io-client:polling polling +1ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr poll +0ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr open GET: https://example.com:3000/socket.io/?EIO=3&transport=polling&t=LjBdH5N +1ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr data null +0ms
socket.io.min.js:1 engine.io-client:socket setting transport polling +1ms
socket.io.min.js:1 socket.io-client:manager connect attempt will timeout after 20000 +1ms
:3000/socket.io/?EIO=3&transport=polling&t=LjBdH5N:1 GET https://example.com:3000/socket.io/?EIO=3&transport=polling&t=LjBdH5N net::ERR_CONNECTION_TIMED_OUT
socket.io.min.js:1 engine.io-client:socket socket error {"type":"TransportError","description":0} +4s
socket.io.min.js:1 socket.io-client:manager connect_error +1ms
socket.io.min.js:1 socket.io-client:manager cleanup +0ms
socket.io.min.js:1 socket.io-client:manager reconnect attempt error +1ms
socket.io.min.js:1 socket.io-client:manager will wait 5000ms before reconnect attempt +0ms
socket.io.min.js:1 engine.io-client:socket socket close with reason: "transport error" +1ms
socket.io.min.js:1 engine.io-client:polling transport not open - deferring close +0ms
socket.io.min.js:1 socket.io-client:manager attempting reconnect +5s
socket.io.min.js:1 socket.io-client:manager readyState closed +1ms
socket.io.min.js:1 socket.io-client:manager opening https://example.com:3000 +0ms
socket.io.min.js:1 engine.io-client:socket creating transport "polling" +1ms
socket.io.min.js:1 engine.io-client:polling polling +0ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr poll +1ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr open GET: https://example.com:3000/socket.io/?EIO=3&transport=polling&t=LjBdJAO +0ms
socket.io.min.js:1 engine.io-client:polling-xhr xhr data null +1ms
socket.io.min.js:1 engine.io-client:socket setting transport polling +1ms
socket.io.min.js:1 socket.io-client:manager connect attempt will timeout after 20000 +0ms