Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring Boot dont establish connection with browser using transports: websocket #45

Closed
DanielBotnik opened this issue Mar 16, 2022 · 1 comment

Comments

@DanielBotnik
Copy link

Hey, I have followed the guide on https://github.com/socketio/engine.io-server-java/blob/master/docs/spring-integration.rst
and have succsfully connected engine.io socket using Node.js application with the following code.

Bean

@Bean
public EngineIoServer engineIoServer(){
    EngineIoServerOptions options = EngineIoServerOptions.newFromDefault();
    options.setAllowedCorsOrigins(null);
    options.setPingTimeout(30000);
    EngineIoServer engineIoServer = new EngineIoServer(options);
    engineIoServer.on("connection", args -> {
        System.out.println("CONNECTION");
    });
    return engineIoServer;
}

Node.js Code

const { Socket } = require('engine.io-client');

const socket = new Socket('http://localhost:4919', {
    transports: ['websocket'],
});

socket.on('open', () => {
    console.log('Connected');    
});

but the same code just for the browser doesnt work.
Browser Code

<script>
    const socket = eio('http://localhost:4919', {
        transports: ['websocket'],
    });

    socket.on('open', function() {
        console.log('open!');
    });
</script>

and we can see that there is no response from the server
image

I have found out that by changing the transports to 'polling' insted of 'websocket' let the browser connect to the server, but I still rather use websocket insted of long polling.

I am using engine.io-server 6.0.1 and Java 17.
the error occured on chrome and edge, didnt check different browsers.

@DanielBotnik
Copy link
Author

After Debugging, I have found the issue is not related to this library, but rather its a problem laying in spring.
I have the problem is caused because the browser Origin HTTP Header, which the node.js application one doesn't have.
the OriginHandshakeInterceptor.beforeHandshake() treat the origin header, even if its null as a String ("null"), therefore it fails all the checks and the connection is dropped.
I have found a quickfix, but I am no expert to determine if its a good one.

@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) {
    System.out.println("IN here");
    attributes.put(ATTRIBUTE_ENGINEIO_QUERY, request.getURI().getQuery());
    attributes.put(ATTRIBUTE_ENGINEIO_HEADERS,request.getHeaders());
    // fix start.
    if("null".equals(request.getHeaders().getOrigin()))
        request.getHeaders().remove("Origin");
    // fix end.
    return true;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant