2

嗨,我一直在测试 websockets,到目前为止,我已经把它连接起来了。但是当我开始向服务器发送和数据时,我得到了一堆奇怪的字符。

(通过搜索获得这些代码)

这是服务器:

import socket
import re
from base64 import b64encode
from hashlib import sha1

websocket_answer = (
    'HTTP/1.1 101 Switching Protocols',
    'Upgrade: websocket',
    'Connection: Upgrade',
    'Sec-WebSocket-Accept: {key}\r\n\r\n',
)

GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 8999))
s.listen(1)

client, address = s.accept()
text = client.recv(1024)
print "RECV----------------------------------"
print text

key = (re.search('Sec-WebSocket-Key:\s+(.*?)[\n\r]+', text)
    .groups()[0]
    .strip())

response_key = b64encode(sha1(key + GUID).digest())
response = '\r\n'.join(websocket_answer).format(key=response_key)

print "SEND----------------------------------"
print response
client.send(response)

while 1:
    try :
        print "SEND----------------------------------"
        client.sendall('hello from server')
        print "RECV----------------------------------"
        print client.recv(1024)
    except :
        print "except"
        break

这是客户端:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>WebSocket Echo Client</title>
  <meta charset="UTF-8" />
  <script>
    "use strict";
    // Initialize everything when the window finishes loading
    window.addEventListener("load", function(event) {
      var status = document.getElementById("status");
      var url = document.getElementById("url");
      var open = document.getElementById("open");
      var close = document.getElementById("close");
      var send = document.getElementById("send");
      var text = document.getElementById("text");
      var message = document.getElementById("message");
      var socket;

      status.textContent = "Not Connected";
      url.value = "ws://localhost:8999";
      close.disabled = true;
      send.disabled = true;

      // Create a new connection when the Connect button is clicked
      open.addEventListener("click", function(event) {
        open.disabled = true;
        socket = new WebSocket(url.value, "echo-protocol");

        socket.addEventListener("open", function(event) {
          close.disabled = false;
          send.disabled = false;
          status.textContent = "Connected";
        });

        // Display messages received from the server
        socket.addEventListener("message", function(event) {
          message.textContent = "Server Says: " + event.data;
        });

        // Display any errors that occur
        socket.addEventListener("error", function(event) {
          message.textContent = "Error: " + event;
        });

        socket.addEventListener("close", function(event) {
          open.disabled = false;
          status.textContent = "Not Connected";
        });
      });

      // Close the connection when the Disconnect button is clicked
      close.addEventListener("click", function(event) {
        close.disabled = true;
        send.disabled = true;
        message.textContent = "";
        socket.close();
      });

      // Send text to the server when the Send button is clicked
      send.addEventListener("click", function(event) {
        socket.send(text.value);
        text.value = "";
      });
    });
  </script>
</head>
<body>
  Status: <span id="status"></span><br />
  URL: <input id="url" /><br />
  <input id="open" type="button" value="Connect" />&nbsp;
  <input id="close" type="button" value="Disconnect" /><br />
  <input id="send" type="button" value="Send" />&nbsp;
  <input id="text" /><br />
  <span id="message"></span>
</body>
</html>

这是我点击连接按钮后得到的结果:

RECV----------------------------------
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:8999
Origin: null
Sec-WebSocket-Protocol: echo-protocol
Sec-WebSocket-Key: v6Fu1rJURofc7iIPbeaw0Q==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
Cookie: BG_PREFS=searches_includeapocrypha@no&fontsize@medium&language@en&default_version_display@all&default_version@SND&default_
version_overrides@no&quicksearch_search@&pslookup_language1@en&pslookup_language2@&pslookup_language3@&pslookup_language4@&pslooku
p_language5@&pslookup_showmoresearches@closed&pslookup_showversions@open&pslookup_showmoreversions@closed&pslookup_showoptions@ope
n&pslookup_showfootnotes@yes&pslookup_showxrefs@no&pslookup_showwoj@no&pslookup_showversenums@yes&pslookup_showheadings@yes&pslook
up_showindent@no&pslookup_multilayout@columns&pslookup_multisort@passage&pslookup_embed-versenum@true&pslookup_embed-xref@false&ps
lookup_embed-footnote@false&pslookup_embed-heading@false&keysearch_search@&keysearch_language1@en&keysearch_language@en&
SEND----------------------------------
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Protocol: echo-protocol
Sec-WebSocket-Accept: qVkmFtRs1w8OUwZSe5nMpTWNxbI=


SEND----------------------------------
RECV----------------------------------
keysearch_bookset@&keysearch_spanbegin@1&keysearch_spanend@73&keysearch_limit@none&keysearch_startnumber@1&keysearch_searchtype@al
l&keysearch_showversions@open&keysearch_showmoreversions@closed&keysearch_showoptions@open&keysearch_displayas@long&keysearch_resu
ltspp@25&keysearch_sort@bookorder&keysearch_wholewordsonly@no&commentary_source@1&topindex_source@1&topindex_search@&topindex_sear
ch_type@any&topindex_resultspp@25&audio_source@3&audio_book@&audio_chapter@&dict_source@1&dict_search@&dict_search_type@any&pslook
up_search@>>>>&pslookup_version@NIV>>>>&undefined&keysearch_version@31>>>>; CoreID6=83844698672113373932637&ci=90320803; __atuvc=2
|20

握手似乎很好,它已连接。然后当我从客户端发送文本“测试”时,我得到了这个。

üäQk╩¼%♫╣╪

这和字符编码有什么关系吗?我无法获取发送到服务器的“测试”字符串。而且我在客户端也没有得到任何东西。

注意:我在 Chrome 19 上测试了客户端。

4

2 回答 2

2

根据 RFC,客户端不发送文本数据,而是发送二进制帧

于 2012-06-22T09:37:20.933 回答
1

WebSockets 是一个框架协议。它与原始套接字具有相似的延迟,但数据不是通过网络发送的原始数据:

  • 标头:帧的开头有一个两字节的标头。如果有效负载分别长于 125 字节或 65535 字节,则这也可以是 4 字节或 10 字节标头。

  • Binary or Text:标头还指示数据是二进制还是文本。在您的情况下,数据是文本。要发送二进制数据,您必须从 Javascript 发送 ArrayBuffer 或 Blob。如果您发送一个字符串,那么数据将是文本。如果服务器发送二进制帧,则 onmessage 事件将接收 blob 或 arraybuffer,具体取决于 WebSocket.binaryType 字段的设置。

  • 屏蔽:必须屏蔽从客户端(浏览器)到服务器的所有数据。这是为了解决行为不端的缓存中介的理论问题。从服务器到客户端的数据不能被屏蔽。标头有一个位指示有效负载是否被屏蔽。如果它被屏蔽,那么头后面的前四个字节就是屏蔽。这将作为有效负载数据的运行 XOR 应用,以取消屏蔽它。

在您的示例中,您发送了一个文本字符串“test”。服务器收到 10 个字节的数据:2 个字节的标头,4 个字节的掩码,4 个字节的掩码有效载荷。

请参阅IETF 6455 WebSocket 协议规范的第 5.2 节,了解帧如何工作以及标头中字段的位级细分的图表。

于 2012-06-22T13:44:42.237 回答