简体   繁体   中英

Java websocket draft refuses handshake

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.

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

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);
    };
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM