我在MAMP服務器上成功實現了socket.io以構建聊天應用程序,當我將它推送到生產服務器(數字海洋,laravel僞造)時,我收到錯誤: 帶LetsEncrypt,Redis,Laravel Forge和Digital Ocean的Socket.io
我建立了基於this article在鍛造守護進程,隨後安裝基於this
我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都運行良好。
UPDATE 以下是鉻這似乎是根據我有限的知識,客戶端問題的情況下,它可以幫助別人看到是怎麼回事打印日誌。
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
這兩個問題是具有進出口同樣的問題,但也沒有的情況下,答案很容易理解我的問題:http://stackoverflow.com/questions/37026314/socket-io-timeout-on-laravel-forge?rq = 1 http://stackoverflow.com/questions/35635174/laravel-forge-node-js-websocket-timeout?rq=1 – Derek