简体   繁体   English

通过网络套接字在通信中处理 CORS

[英]Handling CORS in communication through web-sockets

I'm developing a client and server app, that communicate over web-sockets and found that handling CORS only on the web-socket doesn't raise any additional CORS issues, without handling CORS on the server side itself.我正在开发一个客户端和服务器应用程序,它通过 Web 套接字进行通信,发现仅在 Web 套接字上处理 CORS 不会引发任何额外的 CORS 问题,而无需在服务器端处理 Z5A8FEFF0B4BDE3EEC9244B76023B791D。

Before solving the issue the error on the client side was:在解决问题之前,客户端的错误是:

Access to XMLHttpRequest at 'http://localhost:5000/socket.io/?EIO=4&transport=polling&t=NOOM0wp' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

No "Access-Control-Allow-Origin" header on the response object, that appears only if I handle CORS on the server side.响应 object 上没有“Access-Control-Allow-Origin”header,仅当我在服务器端处理 CORS 时才会出现。

But, on the client side I get an error:但是,在客户端我收到一个错误:

GET http://localhost:5000/socket.io/?EIO=4&transport=polling&t=NOONjI6 net::ERR_CONNECTION_REFUSED

I guess this is a pretty simple issue, still an explanation will be much appreciated.我想这是一个非常简单的问题,仍然会非常感谢您的解释。

Use cors configuration.使用cors配置。

Refer to Handling CORS .请参阅处理 CORS

 var express = require('express') var app = express() var app = express() var server = require('http').createServer(app) var io = require('socket.io')(server, { cors: { origin: "*" // add your own origin, or '*' for any origins allow. } })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM