Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Python WebSocket不工作_Javascript_Python_Html_Websocket - Fatal编程技术网

Javascript Python WebSocket不工作

Javascript Python WebSocket不工作,javascript,python,html,websocket,Javascript,Python,Html,Websocket,我试图实现我的第一个websocket示例,但无法使其工作 我使用python Web服务器: import threading import socket def start_server(): tick = 0 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost', 1234)) sock.listen(100) while True:

我试图实现我的第一个websocket示例,但无法使其工作

我使用python Web服务器:

import threading
import socket

def start_server():
    tick = 0
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 1234))
    sock.listen(100)
    while True:
        print 'listening...'
        csock, address = sock.accept()
        tick+=1
        print 'connection!' 
        handshake(csock, tick)
        print 'handshaken'
        while True:
            interact(csock, tick)
            tick+=1


def send_data(client, str):
     #_write(request, '\x00' + message.encode('utf-8') + '\xff')
     str = '\x00' + str.encode('utf-8') + '\xff'
     return client.send(str)

def recv_data(client, count):
    data = client.recv(count)    
    return data.decode('utf-8', 'ignore')

def handshake(client, tick):
     our_handshake = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"+"Upgrade:         WebSocket\r\n"+"Connection: Upgrade\r\n"+"WebSocket-Origin: http://localhost:8888\r\n"+"WebSocket-Location: "+" ws://localhost:1234/websession\r\n\r\n"
     shake = recv_data(client, 255)
     print shake
     #We want to send this without any encoding
     client.send(our_handshake)

 def interact(client, tick):
     data = recv_data(client, 255)
     print 'got:%s' %(data)
     send_data(client, "clock ! tick%d" % (tick))
     send_data(client, "out ! %s" %(data))

 if __name__ == '__main__':
     start_server()
和HTML:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Web Socket Example</title>
    <meta charset="UTF-8">
    <script>
      window.onload = function() {
        var s = new WebSocket("ws://localhost:1234/");
        s.onopen = function(e) { s.send('Ping'); }
        s.onmessage = function(e) { alert("got: " + e.data); }
        s.onclose = function(e) { alert("closed"); }
      };
    </script>
    </head>
    <body>
      <div id="holder" style="width:600px; height:300px"></div>
    </body>
 </html>

Web套接字示例
window.onload=函数(){
var s=newwebsocket(“ws://localhost:1234/”;
s、 onopen=函数(e){s.send('Ping');}
s、 onmessage=函数(e){alert(“got:+e.data);}
s、 onclose=函数(e){alert(“closed”);}
};
当我将浏览器指向apache2时

我得到了以下错误:

python websocketserver.py
listening...
connection!
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:1234
Origin: http://localhost
Sec-WebSocket-Key: A4sVkUhjVlTZbJrp2NUrqg==
Sec-WebSocket-Version: 13


handshaken
got:
Traceback (most recent call last):
  File "websocketserver.py", line 43, in <module>
    start_server()
  File "websocketserver.py", line 17, in start_server
    interact(csock, tick)
  File "websocketserver.py", line 40, in interact
    send_data(client, "out ! %s" %(data))
  File "websocketserver.py", line 24, in send_data
    return client.send(str)
socket.error: [Errno 32] Broken pipe
pythonwebsocketserver.py
听。。。
连接!
GET/HTTP/1.1
升级:websocket
连接:升级
主机:localhost:1234
来源:http://localhost
Sec WebSocket密钥:A4sVkUhjVlTZbJrp2NUrqg==
Sec WebSocket版本:13
握手
得到了:
回溯(最近一次呼叫最后一次):
文件“websocketserver.py”,第43行,在
启动服务器()
文件“websocketserver.py”,第17行,在启动服务器中
交互(C锁定,勾选)
文件“websocketserver.py”,第40行,在interact中
发送_数据(客户端,“输出!%s”%(数据))
文件“websocketserver.py”,第24行,在发送数据中
返回client.send(str)
socket.error:[Errno 32]管道破裂
有人能帮我修一下吗


谢谢

您使用较旧的Hixie 75协议进行响应,但客户端只使用较新的HyBi/IETF RFC 6455 WebSocket协议

您对握手的响应应该更像这样(accept值是根据客户端的键值计算的):

在HyBi/6455中,帧不再用\x00和\xff分隔。相反,每个帧都有一个标头,其中包含多个数据段,包括帧类型和有效负载长度


有关更多信息,请参阅。或者更好的是,您可以参考和/或使用现有的python WebSocket实现,例如,或者我自己的项目,其中包含WebSocket.py,它是一个通用的WebSocket服务器库。

我的建议是,不要尝试自己实现WebSocket堆栈,而是使用一个现成的服务器和库来支持它们。例如,Tornado的一些起点有一个websocket实现。我将尝试使用websockify。我已经检查了您所构建的VNC,我将从中汲取一些想法,以便构建我自己的实现并满足我的需求。我想早些时候给你发电子邮件,但我无法通过github.kanaka给你发电子邮件,我在哪里可以找到一个简单的websockify示例,其中有一个python服务器发送图像?最后一个问题。websockify是否与flash版本的websockets配合使用,以满足没有支持HTML5浏览器的用户的需要?我没有一个简单的发送图像的示例,但是如果您使用websockify和websock.js发送原始数据字符串,则会在另一端将其作为二进制字符串。是的,WebSocket支持[WebSocket js](htttp://github.com/kanaka/websockify)如果需要,include/websock.js将自动加载它。
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=