我有一个托管在A2Hosting共享托管中的socket.io应用程序,问题是它仅使用xhr轮询,因为似乎存在错误建立套接字握手。
客户端
我收到的错误如下:
index.js:83 WebSocket connection to 'wss://www.xxxxxxx.com/socket.io/?
EIO=3&transport=websocket&sid=7KqOx4VgE6xd507zAACf' failed: Error
during WebSocket handshake: Unexpected response code: 400
app.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
const port = 65533
app.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin",
"https://www.xxxxx.com/xxxx/xxxx");
res.setHeader("Access-Control-Allow-Credentials", true);
next();
});
server.listen(port, () => console.log(`Example app listening on port
${port}!`));
app.get('/', (req, res) => res.send('Socket is running....'))
// io.set('transports', ['websocket']);
io.on('connection', function (socket) {
socket.on('scan_complete', function (data) {
io.emit("scan_result", data);
console.log(data);
});
});
针对app.js的.htaccess配置
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:65533/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:65533/$1 [P,L]
客户端
$(function () {
var socket = io.connect("https://www.xxxxx.com/");
socket.on('scan_result', function (msg) {
$('#content').html(profile);
})
})