开发者

Websocket help!

I'm working on a websocket application. I have a server that is written in C#. I have tested it using another C# application for sending and receiving data. The problem occurs when I use a JavaScript on the chrome developer tool (console) and use Websockets to connect to my server.

  1. I receive the header string from the websocket script, with two keys and the last 8 characters for hashing.

  2. I used the header string keys to generate a hash code and crate a header to send back to chrome(j script on the developer tool).

Issues:-

  1. the onopen event is never triggered and the websocket does not receive the header(I assume). I use the onerror to capture any errors. Which never occur.

  2. The readystate on the websocket is 0 or 2(always).

    • But when I send a disconnect response from the server, the websocket triggers the onclose method. (So I assume that he was open but not ready to communicate)

Any suggestions??????? Here's the JavaScript if it helps.

websocket = new WebSocket('ws://My server IP here:8080'); 

try {
    websocket.onopen = function(evt) { 
        open(evt)
        //websocket.send("Message to send");
        alert("Message is sent...");
    }
}
catch(err) { 
    debug(err,'error')
} 

websocket.onerror = function(evt) {
    error(evt)
} 

websocket.onclose = function(evt) { 
    close(evt) 
}

websocket.onmessage = function(evt) {
    message(evt) 
}

function open(evt) { 
    alert("CONNECTED"); 
    doSend("WebSocket rocks"); 
} 

function error(evt) {
    alert (evt.data)
}

function close(evt) { 
    alert("DISCONNECTED"); 
} 

function message(evt) { 
    alert(evt.data); 
} 

function doSend(message) {
    alert(message); 
    websocket.send(message); 
}

And the header I sent back

HTTP/1.1 101 WebSocket Protocol Handshake

Upgrade: WebSocket

Connection: Upgrade

Sec-WebSocket-Origin: chrome://newtab

Sec-WebSocket-L开发者_StackOverflow社区ocation: ws://My server IP:8080 ??i???m?!??9?

Thanks everyone.


It looks like you're trying to respond to the handshake request without the appropriate challenge response. Like Robin mentioned, the handshake is now more complex and involves a challenge for newer versions of the WebSocket protocol. This is a good article that explains the version 76 handshake in more detail.

Here's a code sample that detects the WebSocket protocol version and replies with the appropriate response. (It's in Java, so YMMV, but it should point you in the right direction.)

// Create the WebSocket handshake response.
HttpResponse res = new DefaultHttpResponse(HTTP_1_1,
    new HttpResponseStatus(101, "Web Socket Protocol Handshake"));
res.addHeader(Names.UPGRADE, WEBSOCKET);
res.addHeader(CONNECTION, Values.UPGRADE);

// Fill in the headers and contents depending on handshake method.
// New handshake specification has a challenge.
if (req.containsHeader(SEC_WEBSOCKET_KEY1)
        && req.containsHeader(SEC_WEBSOCKET_KEY2)) {

    // New handshake method with challenge
    res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
    res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketLocation(req));

    String protocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL);

    if (protocol != null) {
        res.addHeader(SEC_WEBSOCKET_PROTOCOL, protocol);
    }

    // Calculate the answer of the challenge.
    String key1 = req.getHeader(SEC_WEBSOCKET_KEY1);
    String key2 = req.getHeader(SEC_WEBSOCKET_KEY2);
    int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1
            .replaceAll("[^ ]", "").length());
    int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2
            .replaceAll("[^ ]", "").length());
    long c = req.getContent().readLong();
    ChannelBuffer input = ChannelBuffers.buffer(16);
    input.writeInt(a);
    input.writeInt(b);
    input.writeLong(c);
    ChannelBuffer output = ChannelBuffers
            .wrappedBuffer(MessageDigest.getInstance("MD5").digest(
                    input.array()));
    res.setContent(output);
} else {
    // Old handshake method with no challenge:
    res.addHeader(WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
    res.addHeader(WEBSOCKET_LOCATION, getWebSocketLocation(req));
    String protocol = req.getHeader(WEBSOCKET_PROTOCOL);
    if (protocol != null) {
        res.addHeader(WEBSOCKET_PROTOCOL, protocol);
    }
}

// Send the response...


Are you implementing the correct Websockets Protocol version? Chrome has moved to version 76, which means the handshake is more complex than it was. If your Javascript client is failing to connect properly this is possibly the cause.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜