socketio客户端随机重新连接,生成新请求。SID

时间:2019-10-24 01:10:39

标签: javascript python flask socket.io

客户端连接并接收一个请求SID,但一段时间后,它会继续随机重新连接并接收新的SID。

app.py:


    import json
    from flask import Flask, render_template, request
    from flask_socketio import SocketIO, emit

    app = Flask(__name__)

    socketio = SocketIO(app)

    @app.route("/")
    def index():
        return render_template('myindex.html')

    @socketio.on('connect')
    def test_connect():
        emit('commands', json.dumps({'userid': request.sid}))

    @socketio.on('commands')
    def line1(*args,**kwargs):
        emit('commands', json.dumps('ECHO: '+str(args)))

    if __name__ == '__main__':
        socketio.run(app, debug=True)

myindex.html:

<!doctype html>
<html lang="en">
<head>
    <script type="application/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
    <script>
            document.addEventListener('DOMContentLoaded', () => {

            var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port);

            socket.on('commands', message => {
                    let li = document.createElement("li");
                    li.innerText = message;
                    document.querySelector('#messages').appendChild(li);
            });

            document.querySelector('#send').onclick = () => {
                    let line = document.querySelector('#command').value;
                    socket.emit('commands', line);
                    document.querySelector('#command').value = '';
            };

        });
    </script>
</head>
<body>
    <ul id="messages"></ul>
    <input id="command" autocomplete="off">
    <button id="send">Send</button>
</body>
</html>

预期:客户端连接并接收单个请求SID,然后一直保持这种状态直到断开连接。

客户端连接时会发生什么:

{"userid": "6e53d11141604e16ab608cc6f93c9dd7"}

,然后继续随机重新连接并接收新的SID:

{"userid": "411d61092a8b415bb46a4ec5cd4d4503"}
{"userid": "2ac00248046d45c2b0c34f960f035236"}
{"userid": "46e9017b9cd746bebd08f63a6e30c201"}

发生了什么事?

0 个答案:

没有答案