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

Error during WebSocket handshake: Unexpected response code: 400 #17

Closed
sprabhakaran opened this issue May 16, 2019 · 14 comments
Closed

Comments

@sprabhakaran
Copy link

sprabhakaran commented May 16, 2019

I have to use engine.io-server-java in my project. When I initiate the eio function, The request has failed and thrown 400 Bad requests. How do I debug this problem? Are there any documents available for engine.io-server-java?

Error

WebSocket connection to 'ws://localhost:8080/engine.io/?EIO=3&transport=websocket&sid=Mg_ZSd5' failed: Error during WebSocket handshake: Unexpected response code: 400
WS.doOpen @ engine.io.js:4403
Transport.open @ engine.io.js:1700
Socket.probe @ engine.io.js:468
Socket.onOpen @ engine.io.js:489
Socket.onHandshake @ engine.io.js:549
Socket.onPacket @ engine.io.js:511
(anonymous) @ engine.io.js:346
Emitter.emit @ engine.io.js:3096
Transport.onPacket @ engine.io.js:1765
callback @ engine.io.js:1510
exports.decodePayload @ engine.io.js:2199
Polling.onData @ engine.io.js:1514
(anonymous) @ engine.io.js:1071
Emitter.emit @ engine.io.js:3096
Request.onData @ engine.io.js:1244
Request.onLoad @ engine.io.js:1311
xhr.onreadystatechange @ engine.io.js:1197

Request Header

Code:

@WebServlet("/engine.io/*")
public class SocketIO extends HttpServlet {
	private final EngineIoServer server = new EngineIoServer();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        server.handleRequest(request, response);
        server.on("connection", new Emitter.Listener() {
			@Override
			public void call(Object... arg0) {
				System.out.println("Socket connected");			
			}
		});
    }
}

Client Code:

var io = eio("http://localhost:8080", {transport: ["websocket"], upgrade: true, reconnection: false});
@trinopoty
Copy link
Collaborator

Please refer to WebSocket documentation on how to setup websocket handling.

@sprabhakaran
Copy link
Author

@trinopoty I tried both ways of implementation, but, I got the same 400 issues. The tomcat server didn't log the 400 request's trace. So, I can't able to close to the problem.

@trinopoty
Copy link
Collaborator

Try setting breakpoints in the websocket handler. If that doesn't work, the request isn't reaching the server and the problem is elsewhere.
What version of tomcat are you using?

@sprabhakaran
Copy link
Author

Tomcat version: 9.x
Java version: 1.8.x
engine.io-server-java: master source
engine.io.js: master source

@trinopoty
Copy link
Collaborator

If the websocket handler is never being invoked, there's some problem in configuring tomcat.
Try to get a normal websocket connection first and then build up from that.

@sprabhakaran
Copy link
Author

I tried with the normal WebSocket connection and It works.

I found the below code in handleRequest method. Is this correct? I assumed that transport param accepts multiple values (websocket and polling). But, this check always checking polling content alone. In my client code, I passed the websocket string. The below always throw 400 error.

if (!query.containsKey("transport") || !query.get("transport").equals("polling")) {
            sendErrorMessage(response, ServerErrors.UNKNOWN_TRANSPORT);
            return;
 }

@trinopoty The given docs are not enough to use(even I can't make an upgrade the HttpRequest) and it really painful to understand the workflow. Can you please post sample code, at least the HelloWorld example.

Curious to know, does anyone using this framework now?

@trinopoty
Copy link
Collaborator

trinopoty commented May 17, 2019

That is for the http request handling only. There's a separate handler for websocket connections named handleWebsocket. You need to setup websockets on your server and call that.

@trinopoty
Copy link
Collaborator

trinopoty commented May 17, 2019

This code should work for tomcat.

Curious to know, does anyone using this framework now?

Yes, I happen to be using this in a production environment right now.

@trinopoty
Copy link
Collaborator

I'll try to get some examples included.

@sprabhakaran
Copy link
Author

@trinopoty Please check the below code that I have used handleWebsocket method. Now I getting different error,

engine.io.js:4403 WebSocket connection to 'ws://localhost:8080/engine.io/?EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 200

Am I missed anything in the below code?

package com.ide.socketio;

import java.io.IOException;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.socket.engineio.server.EngineIoServer;
import io.socket.engineio.server.EngineIoWebSocket;
import io.socket.parseqs.ParseQS;

@WebServlet("/engine.io/*")
public class EchoServlet extends HttpServlet {

	private final EngineIoServer server = new EngineIoServer();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
		final Map<String, String> query = ParseQS.decode(request.getQueryString());
		server.handleWebSocket(new MyClass(query));
	}

}

class MyClass extends EngineIoWebSocket {

	Map<String, String> query;

	MyClass(Map<String, String> query) {
		this.query = query;
	}

	@Override
	public Map<String, String> getQuery() {
		return this.query;
	}

	@Override
	public void write(String message) throws IOException {
		System.out.println("write msg called " + message);

	}

	@Override
	public void write(byte[] message) throws IOException {
		// TODO Auto-generated method stub

	}

	@Override
	public void close() {
		// TODO Auto-generated method stub
		System.out.println("-------------  closed ");

	}

}

@trinopoty
Copy link
Collaborator

The mechanism for registering websocket endpoints are different than http endpoints.
All the code needed to get websocket working is given here.
Try that code out and let me know.

@sprabhakaran
Copy link
Author

Dear @trinopoty I tried given code snippet. Yes, The code is working. The HTTP request has been upgraded.

Thanks a lot.

@sprabhakaran
Copy link
Author

Dear @trinopoty

After the setup the connection establishing works without error. But, the data transmission is not working. I enabled the client debugger and find the below log logs,

engine.io.js:3394 engine.io-client:socket creating transport "websocket" +0ms
engine.io.js:3394 engine.io-client:socket setting transport websocket +27ms
engine.io.js:3394 engine.io-client:socket socket receive: type "error", data "parser error" +130ms
engine.io.js:3394 engine.io-client:socket socket error {"code":"parser error"} +1ms
engine.io.js:3394 engine.io-client:socket socket close with reason: "transport error" +1ms

Any idea of the above error?

@trinopoty
Copy link
Collaborator

"transport error" may be caused by connecting to websocket of a different protocol (not engine.io) or network issues.
If you're using Chrome, you can go into the Developer Console > Network tab and see exactly what data is being transferred.

@trinopoty trinopoty reopened this May 21, 2019
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

2 participants