我有我的虛擬服務器(Ubuntu的),它運行作爲一個systemd服務,這是積極地運行,創造了一個socket.io聊天應用從客戶機到服務器的連接。Socket.io - 通過https
我server.js位於:
/var/www/vhosts/mywebpage.de/w1.mywebpage.de/chat/
的server.js看起來是這樣的:
const io = require('socket.io')(3055);
io.on('connection', function(socket) {
// When the client emits 'addUser', this listens and executes
socket.on('addUser', function(username, room) {
...
});
// When the client emits 'sendMessage', this listens and executes
socket.on('sendMessage', function(msg) {
...
});
// Disconnect the user
socket.on('disconnectUser', function(username, room) {
...
});
});
在我的網站(HTTPS),我嘗試連接如下:
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
<script type="text/javascript">
var loSocket;
$(document).ready(function() {
if(typeof(loSocket) == 'undefined') {
loSocket = io('https://w1.mywebpage.de:3055', {
reconnectionAttempts: 5,
forceNew: true
});
}
});
</script>
但我不能得到有效的連接。
開發工具這樣說:
(failed) ERR_CONNECTION_CLOSED with initiator polling-xhr.js:264.
可能是什麼錯誤?
好,socket.io是在其上沒有Web服務器,那麼你將使用節點http或expressjs在server.js需要安裝一個服務器。見https://socket.io/docs/ –
但大約2年前,我得到了同樣的server.js工作。我不知道自那以後發生了什麼變化。 server.js使用/ usr/bin/node永久運行。 – Kelturio