Python WebSocket not working

前端 未结 1 1636
感动是毒
感动是毒 2021-01-27 07:11

I tried to implement my first websocket example but I cannot make it work.

I use a python webserver:

import threading
import socket

def start_server():
         


        
1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-27 07:19

    You're responding with the older Hixie 75 protocol but the client only speaks the newer HyBi/IETF RFC 6455 WebSocket protocol.

    Your response to the handshake should look more like this (the accept value is calculated from the key value from the client):

    HTTP/1.1 101 Switching Protocols
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
    

    In HyBi/6455, the frames are no longer delimited with \x00 and \xff. Instead there is a header to every frame that contains several pieces of data including frame type and payload length.

    See the spec for more information. Or better yet, you could refer and/or use an existing python WebSocket implementation such as pywebsocket, tornado, or my own project websockify which contains websocket.py which is a generic websocket server lib.

    0 讨论(0)
提交回复
热议问题