简体   繁体   English

Websocket帮助!

[英]Websocket help!

I'm working on a websocket application. 我正在研究websocket应用程序。 I have a server that is written in C#. 我有一个用C#编写的服务器。 I have tested it using another C# application for sending and receiving data. 我使用另一个C#应用程序测试它来发送和接收数据。 The problem occurs when I use a JavaScript on the chrome developer tool (console) and use Websockets to connect to my server. 当我在chrome开发人员工具(控制台)上使用JavaScript并使用Websockets连接到我的服务器时,会出现问题。

  1. I receive the header string from the websocket script, with two keys and the last 8 characters for hashing. 我从websocket脚本接收头字符串,带有两个键,最后8个字符用于散列。

  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). 我使用标题字符串键生成哈希代码并打开标题以发送回chrome(开发人员工具上的j脚本)。

Issues:- 问题: -

  1. the onopen event is never triggered and the websocket does not receive the header(I assume). onopen事件永远不会被触发,websocket也不会收到标题(我假设)。 I use the onerror to capture any errors. 我使用onerror来捕获任何错误。 Which never occur. 从未发生过。

  2. The readystate on the websocket is 0 or 2(always). websocket上的readystate是0或2(总是)。

    • But when I send a disconnect response from the server, the websocket triggers the onclose method. 但是当我从服务器发送断开连接响应时,websocket会触发onclose方法。 (So I assume that he was open but not ready to communicate) (所以我认为他是开放但没有准备好沟通)

Any suggestions??????? 有什么建议么??????? Here's the JavaScript if it helps. 如果它有帮助,这是JavaScript。

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 HTTP / 1.1 101 WebSocket协议握手

Upgrade: WebSocket 升级:WebSocket

Connection: Upgrade 连接:升级

Sec-WebSocket-Origin: chrome://newtab Sec-WebSocket-Origin:chrome:// newtab

Sec-WebSocket-Location: ws://My server IP:8080 ??i???m?!??9? Sec-WebSocket-Location:ws://我的服务器IP:8080 ??我?????? ?? 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. 与Robin提到的一样,握手现在更加复杂,并且对新版本的WebSocket协议提出了挑战。 This is a good article that explains the version 76 handshake in more detail. 这是一篇很好的文章 ,更详细地解释了版本76握手。

Here's a code sample that detects the WebSocket protocol version and replies with the appropriate response. 这是一个代码示例,它检测WebSocket协议版本并回复相应的响应。 (It's in Java, so YMMV, but it should point you in the right direction.) (它是Java,所以YMMV,但它应该指向正确的方向。)

// 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? 您是否实施了正确的Websockets协议版本? Chrome has moved to version 76 , which means the handshake is more complex than it was. Chrome已移至版本76 ,这意味着握手比以前更复杂。 If your Javascript client is failing to connect properly this is possibly the cause. 如果您的Javascript客户端无法正确连接,则可能是原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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