几个星期以来,我一直在尝试在生产(ubuntu)服务器(nginx)上实现我的websocket功能。我的websocket在本地工作,但我在生产中总是遇到错误。
我的socket.js看起来像这样:
public ActionResult Index(string sortOrder, string searchString = "", string currentFilter, int? page, string searchBy, string startdate = null, string enddate = null)
我的Event.js看起来像这样:
var fs = require('fs');
var options = {
type: "local",
key: fs.readFileSync("/etc/nginx/ssl/sub.domain.com/467605/server.key"),
cert: fs.readFileSync("/etc/nginx/ssl/sub.domain.com/467605/server.crt")
};
if (options.type == 'dev') {
var app = require('http').createServer(handler);
} else {
var app = require('http').createServer(options,handler);
}
var io = require('socket.io')(app);
var Redis = require('ioredis');
var redis = new Redis();
function handler(req, res) {
res.writeHead(200);
res.end('');
}
io.on('connection', function(socket) {});
// Redis UserSignedUp Channel, Channel if user signs up
var redisUserSignedUp = new Redis();
redisUserSignedUp.subscribe('signed-up-channel');
redisUserSignedUp.on('message', function(channel, message) {
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
// run server on port 3333
app.listen(3333, function () {
console.log('Server running!');
});
如果我在Event.js中这样做:
const socket = io('sub.domain.com:3333', {
secure: true
});
// ... works locally
socket.on('signed-in-channel:App\\Events\\UserSignedIn', (data) => {
this.signedInUsers = data.username;
this.$toasted.info('Success: ' + data.username, {
theme: "primary",
duration: 10000
});
});
我收到此错误:
https://sub.domain.com:3000/socket.io/?EIO=3&transport=polling&t=MdZoLnn 净:: ERR_SSL_PROTOCOL_ERROR
就像我看着服务器ip一样:
const socket = io('sub.domain.com:3333', { secure: true });
我收到此错误:
https://123.123.123.123:3333/socket.io/?EIO=3&transport=polling&t=MdZpRnE 净:: ERR_CONNECTION_TIMED_OUT
该站点具有让我们加密ssl证书的功能,此外,Web服务器是nginx,操作系统是ubuntu。在我的本地窗口(沼泽)中,它可以从节点socket.js启动。