V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Akikiki
V2EX  ›  Python

flask-SocketIO 客户端使用原生 WebSocket 不能成功连接,但使用 socket.io 却能正常连接?

  •  
  •   Akikiki · 2018-07-05 18:43:34 +08:00 · 8383 次点击
    这是一个创建于 2094 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Python 2.7 使用 flask_socketio(3.0.1) 时,浏览器控制台下用原生

    var socket = new WebSocket("ws://127.0.0.1:5000/char")
    

    错误信息如下:

    VM167:1 WebSocket connection to 'ws://127.0.0.1:5000/chat' failed: Error during WebSocket handshake: Unexpected response code: 200

    引入 socket.io

    socket = io.connect('ws://127.0.0.1:5000/chat')
    

    查看 socket 的 connected 属性显示为 true

    flask_socketio 的 git 地址: https://github.com/miguelgrinberg/Flask-SocketIO

    是不是只能使用 socket.io 创建 client 进行连接?给的 demo 也都是使用 socket.io

    求踩过坑的朋友指点一下,谢谢!

    31 条回复    2019-12-15 18:15:40 +08:00
    hand515
        1
    hand515  
       2018-07-05 18:45:16 +08:00   ❤️ 1
    socket.io 用了 polling 模式吗?
    Akikiki
        2
    Akikiki  
    OP
       2018-07-05 18:47:16 +08:00 via iPhone
    @hand515 你是说客户端吗?应该没有。我只是测试了下能不能连接成功。
    xiaoshenke
        3
    xiaoshenke  
       2018-07-05 18:47:44 +08:00   ❤️ 1
    是的,只能用 socket.io 的 client。我这边本来有个 python ws 的需求,现在就是一直往后延。。。python 做 web 可能确实不是特别合适吧。。
    Akikiki
        4
    Akikiki  
    OP
       2018-07-05 18:49:12 +08:00 via iPhone
    @xiaoshenke 这就有点蛋疼了。。。 好像 tornado 没有这个问题。
    Akikiki
        5
    Akikiki  
    OP
       2018-07-05 19:08:00 +08:00
    golmic
        6
    golmic  
       2018-07-05 19:08:06 +08:00 via Android   ❤️ 1
    @Akikiki 我用 Python Flask socketio 正常。Python 版本 3.7.0
    hcymk2
        7
    hcymk2  
       2018-07-05 19:14:37 +08:00   ❤️ 1
    F12 看下 network 里面 ws 有没有包。
    Akikiki
        8
    Akikiki  
    OP
       2018-07-05 19:18:03 +08:00
    @golmic client 用的是 socket.io 吗?
    golmic
        9
    golmic  
       2018-07-05 19:19:46 +08:00 via Android
    @Akikiki
    Akikiki
        10
    Akikiki  
    OP
       2018-07-05 19:20:42 +08:00
    @hcymk2 用 new 出来的 WebSocket 去连接会看到 WS 里面有包,但是用 socket.io 看不到。
    Akikiki
        11
    Akikiki  
    OP
       2018-07-05 19:21:04 +08:00
    @golmic 好吧,我用 socket.io 也是没有问题的。
    xiaoxinxiaobai
        12
    xiaoxinxiaobai  
       2018-07-05 19:22:58 +08:00 via Android   ❤️ 1
    地址对了吗,flask socketio 就是那个作者写的 socketio 的 Py 版本,但是客户端应该不影响,只要是个 ws
    Akikiki
        13
    Akikiki  
    OP
       2018-07-05 19:25:56 +08:00
    @xiaoxinxiaobai 两种连接方式使用的是同一个地址。我看到官方文档写了 *The client-side application can use any of the SocketIO official clients libraries in Javascript, C++, Java and Swift, or any compatible client to establish a permanent connection to the server.*,这算是声明了吗?
    wheato
        14
    wheato  
       2018-07-05 19:31:46 +08:00   ❤️ 1
    看看是不是 python 少了一个依赖,貌似叫 gevent ?如果 websoket 建立不了链接,socket.io 会自动退化到 polling 模式。
    xiaoxinxiaobai
        15
    xiaoxinxiaobai  
       2018-07-05 19:35:00 +08:00 via Android
    @Akikiki 咦,那有可能是,实现的不是单纯的 websocket 协议
    Trim21
        16
    Trim21  
       2018-07-05 19:37:46 +08:00 via Android   ❤️ 1
    socket io 服务器和客户端虽然可以通过 websocket 连接 但是不能用浏览器原生的 websocket 连接 socket io 服务器 socket io 有自己自定义的一套协议。
    ahjsrhj
        17
    ahjsrhj  
       2018-07-05 19:39:26 +08:00
    问一个问题:使用 socket.io 做服务端能直接用 websocket 当做客户端链接么?
    Akikiki
        18
    Akikiki  
    OP
       2018-07-05 19:41:33 +08:00
    @wheato 这个是装了的,因为没装的时候会有 Warning。但是我用 flask_socketio 作者自己的 demo 跑,显示的链接也是 /socket.io/?EIO=3&transport=polling&t=1530787155994-0 这种
    baelish
        19
    baelish  
       2018-07-05 19:44:47 +08:00   ❤️ 1
    /char or /chat?
    Akikiki
        20
    Akikiki  
    OP
       2018-07-05 19:51:17 +08:00
    @baelish 谢谢提醒,刚去我测试代码看了下,代码里没有错。帖子里写错了。
    baelish
        21
    baelish  
       2018-07-05 21:01:47 +08:00
    文档中提到 “ Unofficial clients may also work, as long as they implement the Socket.IO protocol ”. 所以只有实现 socket.io 协议的客户端可以连, 直接用 WebSocket 是不行的。
    momocraft
        22
    momocraft  
       2018-07-05 21:06:07 +08:00
    socket.io 连不着时会 fallback 到 http
    wwqgtxx
        23
    wwqgtxx  
       2018-07-05 23:12:37 +08:00
    其实楼上说的都不完全,socket.io 本身是一套私有协议,但是他可以基于 http polling/flash socket/websocket 等等通道进行传输,其实直接用原生的 websocket 去连接 socket.io 的服务器也不是不可以,但是实际操作的步骤就和你直接用 tcp 连接一个 http server 一样,你需要自行完成 socket.io 的握手步骤
    hand515
        24
    hand515  
       2018-07-06 09:11:56 +08:00
    &transport=polling 基本就是用了 polling 模式,基于 http 长轮询,并不是 ws
    BOYPT
        25
    BOYPT  
       2018-07-06 10:13:48 +08:00
    websocket 只是 socket.io 的其中一种工作方式。
    xiaoheijw
        26
    xiaoheijw  
       2018-07-06 12:31:15 +08:00   ❤️ 1
    flask-sockerio 是假的 websocket,建议用 tornado
    wwqgtxx
        27
    wwqgtxx  
       2018-07-06 18:44:56 +08:00
    @xiaoheijw flask-socketio 是真的 websocket 好吧
    Akikiki
        29
    Akikiki  
    OP
       2018-07-25 17:38:39 +08:00
    看到了这个。。。 作者自己回答了,so sad!
    kayseen
        30
    kayseen  
       2019-12-14 14:59:39 +08:00
    @Akikiki
    @golmic
    你好,我现在也是在使用 python3.7 和 flask-socketio,在本地跑作者的 example 没问题,
    但是我部署到服务器之后,在浏览器打开控制台就一直报错 400 错误,并且每一秒都会尝试重连,
    请问你们当时部署到服务器的时候,nginx 是怎么配置的呢,求解答...
    Akikiki
        31
    Akikiki  
    OP
       2019-12-15 18:15:40 +08:00
    @kayseen nginx 关于 websocket 的配置可以上网搜一搜,一堆。flask-socketio 的支持和前端使用的库有关,原生的是不支持的。你可以找个测试网站试试就知道了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2874 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:48 · PVG 21:48 · LAX 06:48 · JFK 09:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.