【问题标题】:socket.io connection problem with python server and javascript clientpython服务器和javascript客户端的socket.io连接问题
【发布时间】:2019-02-03 16:41:25
【问题描述】:

我正在尝试在 Python 中设置 socket.io 服务器,在 JavaScript 中设置 socket.io 客户端。

我找到了 JS 客户端与节点服务器对话的示例和 python 客户端与 python 服务器对话的示例 - 但没有 JS 客户端与 python 服务器对话的示例。所以我正在尝试组合来自不同示例的代码。

客户端(由 apache 提供的 html 文件,托管在您想要的任何地方)

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
<script>

socket = io.connect('http://localhost:5000');

socket.on('connect',function() {
  console.log('Client has connected to the server!');
});

socket.on('msg',function(data) {
  console.log('Received a message from the server!',data);
});

socket.on('disconnect',function() {
  console.log('The client has disconnected!');
});

// Sends a message to the server via sockets
function send(message) {
  socket.emit('msg',message);
};

</script>

服务器:

import eventlet
import socketio

sio = socketio.Server()
# the index.html file hosted by eventlet is a dummy file
# it appears to be required to host some html file.. 
app = socketio.WSGIApp(sio, static_files={
    '/': {'content_type': 'text/html', 'filename': 'index.html'}
})

@sio.on('connect')
def connect(sid, environ):
    print('connect ', sid)

@sio.on('msg')
def message(sid, data):
    print('message ', data)

@sio.on('disconnect')
def disconnect(sid):
    print('disconnect ', sid)

if __name__ == '__main__':
    eventlet.wsgi.server(eventlet.listen(('', 5000)), app)

运行此设置时,我可以在 Python 终端中看到客户端尝试连接的方式:

    disconnect  db71fa07154c45d4b6e6c80073d27e17
    127.0.0.1 - - [03/Feb/2019 17:28:04] "POST /socket.io/?EIO=3&transport=polling&t=MYqC39S&sid=4e1045977dd14223b0d2913752dd5cf1 HTTP/1.1" 400 233 0.000296
    127.0.0.1 - - [03/Feb/2019 17:28:04] "POST /socket.io/?EIO=3&transport=polling&t=MYqC39Z&sid=4e1045977dd14223b0d2913752dd5cf1 HTTP/1.1" 400 233 0.000201
    connect  8650c5c7f735492e84a6e8774a20b069
    127.0.0.1 - - [03/Feb/2019 17:28:05] "GET /socket.io/?EIO=3&transport=polling&t=MYqC3Q3 HTTP/1.1" 200 396 0.001240
    disconnect  8650c5c7f735492e84a6e8774a20b069
    127.0.0.1 - - [03/Feb/2019 17:29:05] "GET /socket.io/?EIO=3&transport=polling&t=MYqC3QC&sid=8650c5c7f735492e84a6e8774a20b069 HTTP/1.1" 400 233 60.005087
    (12281) accepted ('127.0.0.1', 46478)
    127.0.0.1 - - [03/Feb/2019 17:29:23] "GET /socket.io/?

在 chrome 中我看到客户端连接尝试:

index.js:83 POST http://localhost:5000/socket.io/?EIO=3&transport=polling&t=MYqDzXq&sid=d08f2f55533b43ea81bd2f98201de277 400 (BAD REQUEST)
    i.create @ index
    index.js:83 POST http://localhost:5000/socket.io/?EIO=3&transport=polling&t=MYqDzXz&sid=d08f2f55533b43ea81bd2f98201de277 400 (BAD REQUEST)
    i.create @ index.
    index.js:83 WebSocket connection to 'ws://localhost:5000/socket.io/?EIO=3&transport=websocket&sid=f23be764110d44cda684e633a990e0d7' failed: Error during WebSocket handshake: Unexpected response code: 400

他们似乎在交流——但出了点问题,所以连接从未完全建立。

【问题讨论】:

  • 你只需要再调试一下。一个好的第一步是在客户端周围添加 try/catch 并打印错误。

标签: javascript python python-3.x socket.io


【解决方案1】:

发现问题 - 我有两个服务器实例正在运行。其中一个作为后台线程被触发并一直运行,所以我忘记了...有两台服务器在同一个端口上侦听,这就是通信混乱的原因。

现在一切正常 - 所以这段代码可以用作 python-JS socket.io 通信的最小工作示例。

【讨论】:

    【解决方案2】:

    要从 JS 客户端脚本向服务器发送消息,您必须使用 emit 而不是 send。

    // Sends a message to the server via sockets
    function send(message) {
      socket.emit('msg',message);
    };
    

    【讨论】:

      猜你喜欢
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      相关资源
      最近更新 更多