使用最新协议的 WebSocket 服务器 (hybi 10)

发布于 2024-11-29 18:11:43 字数 2093 浏览 0 评论 0原文

我浏览过这里的论坛,这是我发现的最接近的问题:

如何在 WebSockets hybi 08+ 中(解)构造数据帧?

不同之处在于我无法成功握手。我假设在握手完成后框架不会发挥作用,这是正确的吗?

当 Chrome 方便地更新到使用 HyBi 10 websocket 协议 (https://datatracker.ietf.org/doc/html/draft-ietf-hybi-thewebsocketprotocol-10)。基于握手规范中的信息 (https://datatracker.ietf.org/doc/html/draft-ietf-hybi-thewebsocketprotocol-10#section-5.2.2)我已经能够成功创建 Sec-WebSocket-Accept key(基于他们的示例成功),但在客户端,socket.onopen 函数永远不会触发。

上次我遇到了 WebSocket 协议握手问题,这是使用正确字节终止握手的问题(或者我认为字符更准确?)。我当前的实现使用 PHP,这意味着尝试解码 Python 或 C# 实现,但到目前为止还没有成功。

这是我在 Chrome 14(适用于 Windows)中运行的客户端 Javascript:

var socket;
socket = new WebSocket(host);
socket.onopen = function(msg){
    // process onopen
};
socket.onmessage = function(msg){ 
    // process message
};
socket.close = function(msg){
    // process close
};

这是我用于握手的服务器端 PHP 代码:

function dohandshake($user,$buffer){
    // getheaders and calcKey are confirmed working, can provide source if desired
    list($resource,$host,$origin,$key,$version) = $this->getheaders($buffer);
    $request = "HTTP/1.1 101 Switching Protocols\r\n" .
            "Upgrade: WebSocket\r\n" .
            "Connection: Upgrade\r\n" .
            "Sec-WebSocket-Accept: " . $this->calcKey($key) . "\r\n";
    socket_write($user->socket,$request);
    $user->handshake=true;
    return true;
}

一旦客户端发送初始握手,Javascript 套接字就会无限期地保持在 CONNECTING 状态。这意味着 onopen 永远不会被触发,所以我的套接字处于不稳定状态。任何有关如何调试或更好地确认我的握手方法的想法都会很棒。

这是一个明显的(我不能说它是否肯定有效)解决方案(https://github.com/kanaka/websockify/blob/master/websocket.py)。寻找 do_handshake 方法。

谢谢!

I have browsed the forums here and this was the closest question I found:

How to (de)construct data frames in WebSockets hybi 08+?

The difference is that I am unable to get a successful handshake. I am assuming that framing doesn't play a roll until AFTER the handshake is complete, is this correct?

I was about to launch a proof of concept when Chrome conveniently updated to version 14 which uses the HyBi 10 websocket protocol (https://datatracker.ietf.org/doc/html/draft-ietf-hybi-thewebsocketprotocol-10). Based on the information in the spec on the handshake (https://datatracker.ietf.org/doc/html/draft-ietf-hybi-thewebsocketprotocol-10#section-5.2.2) I have been able to successfully create a Sec-WebSocket-Accept key (success based on their example), but on the client-side the socket.onopen function never fires.

Last time I had an issue with the WebSocket protocol handshake, it was an issue with terminating the handshake with the correct bytes (or I suppose characters is more accurate?). I am using PHP for the current implementation and that has meant trying to decode Python or C# implementations, with no success so far.

Here is my client-side Javascript running in Chrome 14 (for Windows):

var socket;
socket = new WebSocket(host);
socket.onopen = function(msg){
    // process onopen
};
socket.onmessage = function(msg){ 
    // process message
};
socket.close = function(msg){
    // process close
};

And here is my server-side PHP code for the handshake:

function dohandshake($user,$buffer){
    // getheaders and calcKey are confirmed working, can provide source if desired
    list($resource,$host,$origin,$key,$version) = $this->getheaders($buffer);
    $request = "HTTP/1.1 101 Switching Protocols\r\n" .
            "Upgrade: WebSocket\r\n" .
            "Connection: Upgrade\r\n" .
            "Sec-WebSocket-Accept: " . $this->calcKey($key) . "\r\n";
    socket_write($user->socket,$request);
    $user->handshake=true;
    return true;
}

Once the client sends the initial handshake, the Javascript socket remains in the CONNECTING state indefinitely. This means onopen is never fired and so my socket stays in limbo. Any ideas on both how to debug, or even better confirm my handshake approach would be great.

Here is an apparent (I can not say whether it works for sure or not) solution in Python (https://github.com/kanaka/websockify/blob/master/websocket.py). Look for the do_handshake method.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

川水往事 2024-12-06 18:11:43

所以我用握手解决了我的特殊问题,这是相当菜鸟的。我需要两组“\r\n”来结束握手。因此,为了解决我上面描述的握手问题(Javascript WebSocket 不会进入 OPEN 状态),我需要对服务器端 PHP 进行以下更改(注意末尾的 \r\n\r\n,doh) :

function dohandshake($user,$buffer){
    // getheaders and calcKey are confirmed working, can provide source if desired
    list($resource,$host,$origin,$key,$version) = $this->getheaders($buffer);
    $request = "HTTP/1.1 101 Switching Protocols\r\n" .
        "Upgrade: WebSocket\r\n" .
        "Connection: Upgrade\r\n" .
        "Sec-WebSocket-Accept: " . $this->calcKey($key) . "\r\n\r\n";
    socket_write($user->socket,$request);
    $user->handshake=true;
    return true;
}

另外,对于未来的 PHP-WebSocket 爱好者,我只是使用正则表达式来解析 getheaders 中的标头,这就是 calcKey:

function calcKey($key){
     $CRAZY = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
     $sha = sha1($key.$CRAZY,true);
     return base64_encode($sha);
}

希望这对其他人有帮助!现在开始处理消息框架......

So I solved my particular issue with the handshake, and it was quite noobish. I needed two sets of "\r\n" to close out the handshake. So to fix the handshake issue I described above (the Javascript WebSocket not going to the OPEN state) I needed to make the following change to my server-side PHP (note the \r\n\r\n at the end, doh):

function dohandshake($user,$buffer){
    // getheaders and calcKey are confirmed working, can provide source if desired
    list($resource,$host,$origin,$key,$version) = $this->getheaders($buffer);
    $request = "HTTP/1.1 101 Switching Protocols\r\n" .
        "Upgrade: WebSocket\r\n" .
        "Connection: Upgrade\r\n" .
        "Sec-WebSocket-Accept: " . $this->calcKey($key) . "\r\n\r\n";
    socket_write($user->socket,$request);
    $user->handshake=true;
    return true;
}

Also for future PHP-WebSocket enthusiasts I just use regular expressions to parse the header in getheaders and this is calcKey:

function calcKey($key){
     $CRAZY = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
     $sha = sha1($key.$CRAZY,true);
     return base64_encode($sha);
}

Hope this helps someone else! Now to work on the message framing...

旧故 2024-12-06 18:11:43

这是我对这个问题的解决方案。如果在遥远的将来的某个时候此代码停止工作,请在此处发布!

https://github.com/esromneb/phpwebsocket/blob/master/websocket .class.php

查看我的播客以获取更多黑客技巧!
http://portforwardpodcast.com/

Here's my solution to this problem. Please post here if sometime in the distant future this code stops working!

https://github.com/esromneb/phpwebsocket/blob/master/websocket.class.php

Check out my podcast for more hacker tips!
http://portforwardpodcast.com/

强辩 2024-12-06 18:11:43

xsockets.net 提供了一个 C# 服务器端解决方案,可以吗?但是,您不需要编写任何服务器端代码来执行您上面尝试执行的操作。使用通用处理程序并且仅编写 JavaScript。

XSockets 具有高达 hybi10 的多协议支持,具有跨浏览器功能,并可回退到 silverlight 和 flash。
如果您想编写自己的协议等,还支持协议、处理程序和拦截器的插件...请参阅演示 http://youtu.be/MDz1jJJeXKI?hd=1

文档位于 http://xsockets.net/Documentation/Index

致以诚挚的问候
乌菲

xsockets.net provides a serverside solution in c# if that ok? However you do not need do write any serverside code to do what you are trying todo above. Use the generic handler and only write JavaScript.

XSockets has multiprotocol support up to hybi10 with crossbrowser functionality and fallback to silverlight and flash.
Also support plugins for protocols, handlers and interceptors if you want to write your own protocols etc... See the demo at http://youtu.be/MDz1jJJeXKI?hd=1

Docs at http://xsockets.net/Documentation/Index

Best Regards
Uffe

就此别过 2024-12-06 18:11:43

另一件事...您可以在 JsBin 上测试 hybi10 支持。那里有一个多房间聊天,其中包含所有可编辑的代码。

不过那里没有闪光灯和银光后备!

http://jsbin.com/ohitil

/Uffe

Another thing... you can test the hybi10 support on JsBin. There is a multiroom chat available there with all editable code.

No flash and silverlight fallback there though!

http://jsbin.com/ohitil

/Uffe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文