与Python服务器和客户端的JavaScript连接socket.io问题

问题描述 投票:0回答:1

我试图建立在Python socket.io服务器,并在JavaScript中socket.io客户端。

我已经找到了JS的客户交谈,节点服务器和客户端的Python说话蟒蛇服务器的例子 - 但对于JS的客户交谈一个Python服务器没有例子。所以我想代码不同的例子结合起来。

客户端(由阿帕奇服务的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.send('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/?

并且在镀铬我看到客户端的连接尝试:

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

他们似乎沟通 - 但什么是错的,所以连接永远不会完全建立。

javascript python python-3.x socket.io
1个回答
0
投票

发现了问题 - 我有服务器正在运行的两个实例。其中一人被解雇作为一个后台线程,只是不停地奔跑,让我忘了...有两台服务器侦听的同一个端口上,这就是为什么通信弄乱。

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

© www.soinside.com 2019 - 2024. All rights reserved.