netty socket io CORS error: Access-Control-Allow-O

2019-07-23 14:24发布

问题:

I have wamp server running https, java netty socket.io server secured by SSL, and socket.io client.

In client I have folowing code:

io.connect(url, {secure: true} );

the url is: https://127.0.0.1:8080

When I open in my browser my webserver: https://127.0.0.1 I have following error:

socket.io-client:manager reconnect attempt error +1ms
socket.io.js (wiersz 1284)
socket.io-client:manager will wait 4000ms before reconnect attempt +1ms
socket.io.js (wiersz 1284)
engine.io-client:socket socket close with reason: "transport error" +3ms
socket.io.js (wiersz 3524)
engine.io-client:polling transport not open - deferring close +1ms
socket.io.js (wiersz 3524)
socket.io-client:manager attempting reconnect +4s
socket.io.js (wiersz 1284) 
socket.io-client:manager readyState closed +3ms
socket.io.js (wiersz 1284)
socket.io-client:manager opening https://127.0.0.1:8080 +5ms
socket.io.js (wiersz 1284)
engine.io-client:socket creating transport "polling" +4s
socket.io.js (wiersz 3524)
engine.io-client:polling polling +5ms
socket.io.js (wiersz 3524)
engine.io-client:polling-xhr xhr poll +4ms 
socket.io.js (wiersz 3524)
engine.io-client:polling-xhr xhr open GET:           https://127.0.0.1:8080/socket.io/?EIO=3&transport=polling&t=1433329575886-4 +4ms
socket.io.js (wiersz 3524)
engine.io-client:polling-xhr xhr data null +6ms
socket.io.js (wiersz 3524)
GET https://127.0.0.1:8080/socket.io/?   EIO=3&transport=polling&t=1433329575886-4

    37ms    
socket.io.js (wiersz 2739)
engine.io-client:socket setting transport polling +13ms
socket.io.js (wiersz 3524)
socket.io-client:manager connect attempt will timeout after 20000 +42ms
socket.io.js (wiersz 1284)
Zablokowano żądanie do zasobu innego pochodzenia: zasady „Same Origin   Policy” nie pozwalają wczytywać zdalnych zasobów z    „https://127.0.0.1:8080/socket.io/?EIO=3&transport=polling&t=1433329575886-  4”. (nieudane żądanie CORS)
engine.io-client:socket socket error      {"type":"TransportError","description":0} +151ms
 socket.io.js (wiersz 3524)
 socket.io-client:manager connect_error +145ms

I don't know cause of this error... It's error on client, server or javascript is blocking something?

my error is in polish but in english is something like this:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8008/socket.io/1/?t=1399898337175. This can be fixed by moving the resource to the same domain or enabling CORS.

But my resources are on the same domain.

Do you know how to resolve the problem? I read that adding "Access-Control-Allow-Origin" to header could fix this. How add that to netty java socket.io implementation?

EDIT:

I added to header: Access-Control-Allow-Origin: "*" and error still occurs

回答1:

I had same problem with NettySocketIO.Solution is to change NettySocketio server configurations. Issue is your NettySocketIO server and your web server (where you hosted your html web page) is in different domain and you need to add Web Server URL to your NettySocketIO server.

here is my code

Configuration _config = new Configuration();

SocketIOServer _server;

public void InitServer(){

    _config.setHostname("MYNettyWebSocketSever");
    _config.setPort(9092);
    _config.setOrigin("http://WebServerHost:WebServerPort");  // or http://domain.com

    _server = new SocketIOServer(_config);
    _server.addEventListener("chatevent", ComPacket.class, (socketIOClient, comPacket, ackRequest) -> _server.getBroadcastOperations().sendEvent("chatevent", comPacket));
}

public void StartServer(){
    _server.start();
}

Please Note that you need to host your HTML page simply opening HTML page by double click would not work.

please comment if you did not get anything.



回答2:

I had self signed SSL certificate, and my browser blocked requests from: https://127.0.0.1:8080/socket.io/?EIO=3&transport=polling&t=1433329575886-4

so I entered in browser: https://127.0.0.1:8080/socket.io/?EIO=3&transport=polling&t=1433329575886-4 and I accepted self signed certificate, and it worked.

On normal SSL certificate everything worked out of box.

also I had something similar to _config.setOrigin("http://WebServerHost:WebServerPort");

so blocking SSL by browser was the cause.