5

I am working on android(Java) using TooTallNate's java websockets from this tutorial to consume websockets on android to connect with ws:// but I am getting error draft org.java_websocket.drafts.Draft_10@4560b1d0 refuses handshake. I tried their other draft versions but none of them worked either.

CookieMonster
  • 492
  • 6
  • 18
  • 2
    Were you managed to fix the issue? – Andrii Dec 11 '16 at 21:43
  • end of 2017 and I am still facing this – Jimmy Nov 02 '17 at 05:08
  • Found a solution. Add /websocket at the end of your endpoint. When you use ShockJs, it adds that extra string to your endpoint. So without that you are hitting wrong endpoint which is valid only for browser based app ( expected http 200 instead of 101) – Jimmy Nov 02 '17 at 17:30

3 Answers3

2

First of all, you want to use the Draft_6455, it is the current spec, the rest may or may not work on different servers reliably. There are constructors for the draft object which take a List<IProtocol>. If no protocol specified matches one offered by the server, the handshake will be refused.

public Draft_6455( List<IExtension> inputExtensions , List<IProtocol> inputProtocols )
public Draft_6455( List<IExtension> inputExtensions , List<IProtocol> inputProtocols, int inputMaxFrameSize )

I ran into a similar issue to yours with the newest version of TooTallNate's Java Websockets, my code was like so:

   knownExtensions = new java.util.ArrayList();
   knownProtocols = new java.util.ArrayList();
    if(this._protocol){
       knownProtocols.add(new org.java_websocket.protocols.Protocol(this._protocol));
    }

    this._socket = new _WebSocket(uri, new org.java_websocket.drafts.Draft_6455(knownExtensions, knownProtocols), toHashMap(this._headers), this._timeout);

You MUST have at least one valid protocol (even if it is a empty string), or you get the above error you referenced. So I changed my code to be:

...   
if(this._protocol){
   knownProtocols.add(new org.java_websocket.protocols.Protocol(this._protocol));
} 
/* -=-=-=- NEW ADDED CODE -=-=-=- */
else {
    knownProtocols.add(new org.java_websocket.protocols.Protocol(""));
}
/* -=-=-=- END NEW ADDED CODE -=-=-=- */
...

This is what broke, no protocol specified caused the "refuses handshake" error message for me.

Please note there are a couple of reasons for the above "refuses handshake", but in my case it was the missing empty protocol...

Void Star
  • 2,401
  • 4
  • 32
  • 57
Nathanael
  • 5,369
  • 18
  • 23
  • I do not understand where this code is. What type is "this"? – Void Star Feb 19 '19 at 16:49
  • 1
    I was passing in a protocol to my wrapper around the library. If a protocol wasn't set; then I then defaulted to "" – Nathanael Feb 20 '19 at 10:32
  • 1
    I submitted an edit to your answer for peer review. I hope you like it. I think it could help users such as myself who have a slightly different situation (I needed to specify a non-empty protocol, but essentially it is the same underlying issue). It may also focus the answer, pointing users at the problematic API surface rather than your particular code. Feel free to edit my edit if it needs some additional changes. – Void Star Feb 21 '19 at 20:56
0

Did you try this on broswer? You will get a err code on the broswer.

You can write a simple js file to start and test whether this problem is on the server or is on the app.

Here is a demo,it won't take you too much time.

<script type="text/javascript">
function send() {
    var url = 'ws://192.168.1.101:8080/WebSocket/echo';
    var vs  = new WebSocket(url);
    vs.onopen = function(evt){
        vs.send(te.value)
    };
    vs.onmessage = function(evt){
        alert(evt.data);
    };
}

Xiang.G
  • 86
  • 1
  • 7
  • The server already has a perfectly working browser/website version so I'm assuming the server is fine, I am making a native android copy of the site. – CookieMonster Aug 31 '16 at 14:38
0

Basically if you have for example a protocol "my-protocol"

ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
protocols.add(new Protocol("my-protocol"));

//Uncomment below if you want to have a fallback
//protocols.add(new Protocol(""));
Draft_6455 my_draft = new Draft_6455(Collections.<IExtension>emptyList(), protocols);

Taken from here

Paulo Neves
  • 1,096
  • 14
  • 22