嗨,我一直在测试 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" />
<input id="close" type="button" value="Disconnect" /><br />
<input id="send" type="button" value="Send" />
<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 上测试了客户端。