我的聊天服务器在http上工作正常,但是现在我正在使用letencrypt,并且当尝试接收socket.io.js时,客户端在Chrome控制台中返回ERROR 404
在以下代码示例中,我将我的真实域替换为mydomain.de
服务器正在运行,没有错误:
var app = require('express')();
var fs = require('fs');
var https = require('https');
var io = require('socket.io')(https);
var mysql = require('mysql');
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/mydomain.de/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/mydomain.de/cert.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/mydomain.de/chain.pem')
};
var serverPort = 8888;
var server = https.createServer(options, app);
io.on('connection', function(socket){
console.log('a user connected');
socket.on('join room', function (data) {
socket.join(data.room);
console.log('user' + socket.id + ' joined ' + data.room);
});
socket.on('chat message', function(data){
socket.broadcast.to(data.room).emit('chat message', {is_sender: 0, msg: data.msg});
socket.emit('chat message', {is_sender: 1, msg: data.msg});
});
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
server.listen(serverPort, function(){
console.log('listening on *:' + serverPort);
});
客户端代码如下:
<script src="https://mydomain.de:8888/socket.io/socket.io.js"></script>
<script>
var socket = io('https://mydomain.de:8888');
</script>
似乎客户端无法连接,因为服务器根本没有记录任何连接。我在这里想念什么?数小时以来一直试图找出答案,并阅读了许多示例或类似问题,但找不到我犯的错误。