c# - 网络套接字帮助!

标签 c# javascript websocket

我正在开发一个 websocket 应用程序。我有一个用 C# 编写的服务器。我已经使用另一个用于发送和接收数据的 C# 应用程序对其进行了测试。当我在 Chrome 开发人员工具(控制台)上使用 JavaScript 并使用 Websockets 连接到我的服务器时,会出现此问题。

  1. 我从 websocket 脚本收到 header 字符串,其中包含两个键和最后 8 个用于散列的字符。

  2. 我使用 header 字符串键生成哈希码并创建 header 以发送回 chrome(开发人员工具上的 j 脚本)。

问题:-

  1. onopen 事件永远不会被触发,并且 websocket 不会收到 header (我假设)。我使用 onerror 来捕获任何错误。这永远不会发生。

  2. websocket 上的就绪状态为 0 或 2(始终)。

    • 但是当我从服务器发送断开连接响应时,websocket 会触发 onclose 方法。 (所以我认为他很开放,但还没有准备好沟通)

有什么建议吗???????如果有帮助的话,这是 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); 
}

以及我发回的 header

HTTP/1.1 101 WebSocket Protocol Handshake

Upgrade: WebSocket

Connection: Upgrade

Sec-WebSocket-Origin: chrome://newtab

Sec-WebSocket-Location: ws://My server IP:8080 ??i???m?!??9?

谢谢大家。

最佳答案

您似乎正在尝试在没有适当质询响应的情况下响应握手请求。正如 Robin 提到的,握手现在更加复杂,并且涉及对较新版本的 WebSocket 协议(protocol)的挑战。 This is a good article这更详细地解释了版本 76 握手。

下面是一个代码示例,用于检测 WebSocket 协议(protocol)版本并回复适当的响应。 (它是用 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...

关于c# - 网络套接字帮助!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5043685/

相关文章:

javascript - 重新渲染期间组件丢失,如何重新附加?

amazon-web-services - 无服务器框架不是通过配置在 AWS 上部署 API 网关

c# - 使用 C# 列出使用的 TCP 端口

javascript - 将数据绑定(bind)到 d3 中的新元素

C# Razor 重定向到 .Net Core 5 中具有自定义路由的页面

javascript - 计算字符串中重复字符的函数

sockets - 有没有保存socket.io消息历史的好方法

java - 从同一端口提供网络套接字和网页

c# - 当用户更改为新值时验证 DataGridViewComboBoxCell

c# - 创建一个匿名对象,如何使用循环添加项目