我有我的虚拟服务器(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