NaikSoftware / StompProtocolAndroid

STOMP protocol via WebSocket for Android
MIT License
597 stars 265 forks source link

connect error #49

Open wenzixin09 opened 7 years ago

wenzixin09 commented 7 years ago

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?

fishfire commented 7 years ago

Do you fixed this issue?

fishfire commented 7 years ago

"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.

forresthopkinsa commented 7 years ago

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.

ElviraTagny commented 7 years ago

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 !

forresthopkinsa commented 7 years ago

@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?

ElviraTagny commented 7 years ago

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?

forresthopkinsa commented 7 years ago

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.

LVinnie commented 6 years ago

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

forresthopkinsa commented 6 years ago

Glad it worked for you @AVinny :smile:

turbazik commented 5 years ago

@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

forresthopkinsa commented 5 years ago

@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.

victorylk commented 5 years ago

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

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