StompProtocolAndroid icon indicating copy to clipboard operation
StompProtocolAndroid copied to clipboard

connect error

Open wenzixin09 opened this issue 8 years ago • 12 comments

use okhttp3.WebSocket Stomp connection error java.lang.Exception: java.net.ProtocolException: Expected HTTP 101 response but was '200 OK'

use org.java_websocket.WebSocket onClose: code=-1 reason=draft org.java_websocket.drafts.Draft_17@f2ec968 refuses handshake remote=false

use socketjs to connect is success

why?

wenzixin09 avatar Jul 21 '17 07:07 wenzixin09

Do you fixed this issue?

fishfire avatar Jul 25 '17 06:07 fishfire

"use okhttp3.WebSocket Stomp connection error java.lang.Exception: java.net.ProtocolException: Expected HTTP 101 response but was '200 OK' " I found in Socket.js framework ,it will add ‘websocket’,so i add the str in my url ,it works.

fishfire avatar Jul 25 '17 10:07 fishfire

This happens when the webserver does not respond to the request with a 101 code (Protocol Upgrade).

Websockets, as I'm sure you know, function by the server responding to HTTP requests with a Protocol Switch to WS.

So the program flow should look like this:

Client -> Server : HTTP request Server -> Client : HTTP-101 Client -> Server : WS request Server -> Client : WS connection established

The issue you're running into is with step two: your server is not responding with a 101. Instead, it's sending a 200, aka a typical response. It's probably responding with an HTML page. So you just need to figure out why your server is not responding appropriately.

If you're using Spring, you need to make sure you do not have SockJS compatibility (.withSockJS). SockJS is built for browser backwards-compatibility and fallback mechanisms (think polyfills), so obviously it responds to HTTP requests with 200/HTML before it escalates to a 101.

If you need SockJS for some other reason, you can get around this problem by adding '/websocket' onto the end of your address string. This endpoint circumvents SockJS' HTTP-200 response. The Readme demonstrates this well.

forresthopkinsa avatar Aug 01 '17 16:08 forresthopkinsa

Hello guys,

I have the exact same issue :

-use okhttp3.WebSocket Stomp connection error java.lang.Exception: java.net.ProtocolException: Expected HTTP 101 response but was '200 OK' -use org.java_websocket.WebSocket onClose: code=-1 reason=draft org.java_websocket.drafts.Draft_17@f2ec968 refuses handshake remote=false

Adding '/websocket' at the end of my address string does not solve the problem... Any other tip to fix this issue? Help please !

ElviraTagny avatar Sep 20 '17 14:09 ElviraTagny

@ElviraTagny Fancy seeing you here 😃

As I mentioned, this is a server-side problem. Before you can connect to your server by websocket, you need to figure out where your websocket endpoint actually is on your server. This error indicates that you're trying to connect to the wrong endpoint.

So, we can't really fix this problem on our side, the client side. You need to figure out where your websocket endpoint is on your server. What server framework are you using? Spring?

forresthopkinsa avatar Sep 20 '17 17:09 forresthopkinsa

Yes the server framework is Spring websocket (with sockJS). I think the endpoint is okay because the http status is '200 OK' (even if 101 is expected) We developed an Angular 4 application and it's connecting to the same server with that same endpoint. Is there any specifications to know about mobile apps pointing on Spring servers?

ElviraTagny avatar Sep 21 '17 08:09 ElviraTagny

Yeah, that is what should not be happening. If you're using an endpoint for a webpage then that's a good way to know that it's the wrong one for websockets. If you connect a browser to the websocket endpoint, the only possible response should be:

Can "Upgrade" only to "WebSocket".

So you need to ensure that you're configuring Spring to use a different endpoint for the websocket than with the front-end app:

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
	logger.info("Registered Stomp endpoint");
	registry.addEndpoint("/ws");
}

In this case, your endpoint is /ws. If you add a .withSockJS(), then your endpoint is /ws/websocket.

forresthopkinsa avatar Sep 21 '17 15:09 forresthopkinsa

Adding '/websocket' at the end of url! It works! THX!

LVinnie avatar Apr 13 '18 02:04 LVinnie

Glad it worked for you @AVinny :smile:

forresthopkinsa avatar Apr 13 '18 18:04 forresthopkinsa

@forresthopkinsa can u help me to connect for that socket http://alpha.o5service.com:6026/api/fromDriver/?access_token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NzI4MTg1MDAsInVzZXJfbmFtZSI6Ijg3MDE0OTI1MzY3IiwiYXV0aG9yaXRpZXMiOlsiUk9MRV9EUklWRVIiXSwianRpIjoiYTJmYmExMGItMTY3Ni00NDllLWI4ZDktYTMxMDc4YzBlMWZlIiwiY2xpZW50X2lkIjoiY2xpZW50Iiwic2NvcGUiOlsicmVhZCIsIndyaXRlIiwidHJ1c3QiXX0.ZyB4Y3MqUzsE0SF5lGj3Bs829NGmOwUjAvHBRk0ejvNdIC13eZkh_fgiKphTl3PFOoSUybb940s0GQN3lM43ed6S42UaXc5Hgh_5NK7-Ja188q3AFkIv_DYsijIVoIy-DvsWt0C-s16-dSCvaESNZgaNJZxr1E7M9pkRqoBhLDmBHLTekSKzPKmA39MGduvpBIeRieDGVBf56y6EuoWrf04eYkurhJK_yOsActo93V_i5TbqoZjqmRISgRLfMSATMMZOgPhYyFXknV1fVzzJW-0QR3cVPnVY3pz3hzNdI4KA0GNxkoPy3fDt-mm2-Aq1yWZqnNc5oP88SH3G3Zn14Q , when i open it in browser, i have good response, but can't connect with stompclient library, tnx

turbazik avatar Dec 01 '18 10:12 turbazik

@turbazik Please, read my comments before replying here. The response you get from that webpage is not the response that you want to be getting. I've already been over this.

forresthopkinsa avatar Dec 02 '18 06:12 forresthopkinsa

Adding '/websocket' at the end of url! It works! THX!

thank you very much. i use your solution already success. thanks!!!!

victorylk avatar Oct 14 '19 07:10 victorylk