GET http://127.0.0.1:3001/socket.io/?EIO=4&transport=polling&t=O7nczsQ net::ERR_CONNECTION_REFUSED问题,需要在服务端添加
server.listen(3001)//端口自己定义,不要与koa2启动端口冲突
Access to XMLHttpRequest at 'http://127.0.0.1:3001/socket.io/?EIO=4&transport=polling&t=O7ndNvO' from origin 'http://127.0.0.1:5501' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
polling-xhr.js:202 GET http://127.0.0.1:3001/socket.io/?EIO=4&transport=polling&t=O7ndNvO net::ERR_FAILED 200
显示200,但是跨域
socket.io官网示例
const io = new Server(httpServer, {
cors: {
origin: ["https://example.com", "https://dev.example.com"],
allowedHeaders: ["my-custom-header"],
credentials: true
}
});
origin参数修改为客户端调用参数,如:
const io = require('socket.io')(server,{
cors: {
origin: ["https://example.com", "http://127.0.0.1:5501"],
allowedHeaders: ["my-custom-header"],
credentials: true
}
})