Flask-SocketIO简单通信器可在Chrome和Firefox上正常运行,但不能在IE上运行

时间:2019-06-19 14:09:02

标签: javascript python flask flask-socketio

使用Flask构建的非常简单的通信器在任何地方都可以正常工作,但在Internet Explorer中则不能。在Microcrap浏览器上,它返回“ 405-不允许使用方法”错误,而没有任何其他信息。事件永远不会到达Flask应用程序(没有日志),所以我认为它们被阻塞了。有人遇到这个问题吗?

整个代码如下:

app = Flask(__name__)
app.config['SECRET_KEY'] = 'very_secret_word_to_enable_encrytion#'
# eventlet.monkey_patch()
socketio = SocketIO(app)


@app.route('/')
def sessions():
    return render_template('session.html')


def messageReceived(methods=['GET', 'POST']):
    print('message was received!!!')


@socketio.on('chat event')
def handle_event(json, methods=['GET', 'POST']):
    print('received chat event: ' + str(json))
    socketio.emit('chat response', json, callback=messageReceived)


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

和相应的session.html页面正文:

<div id="main" class="container-fluid mx-auto">
    <div class="alert welcome-alert alert-primary alert-dismissible fade show" role="alert" style="width: 750px;">
        <p><strong>Snicky serverless communicator</strong> for internal network chats out of the public<br/>
           It doesn't save anything, everything you write here will be lost after closing browser session
        </p>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    <div id="second-alert"></div>

    <form action="" method="POST" style="margin-top: 20px;">
        <div style="display:inline-block;">
            <input type="text" class="username" placeholder="Your name" style="width: 120px;"/>&nbsp;
            <input type="text" class="message" placeholder="Your messages then enter or click" style="width: 400px;"/>
            <!--<textarea rows="5" class="message" placeholder="Messages"></textarea>-->
            <input type="submit" class="btn btn-outline-primary"/>
        </div>
    </form>
    <br/>
    <h3 style='color: #ccc;font-size: 30px;'>No message yet..</h3>
    <div class="message_holder"></div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.min.js"></script>
    <script type="text/javascript">

        function check_if_msie() {
            var ua = window.navigator.userAgent;
            var msie = ua.indexOf("MSIE ");

            if (msie > 0) { // If Internet Explorer, return version number
                console.log(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
                return true;
            }
            else { // If another browser, return 0
                console.log('otherbrowser');
                return false;
            }
        }

        if (check_if_msie())
            socket = io.connect(location.protocol + "//" + location.host + + ':' + location.port);
        else
            socket = io.connect('http://' + document.domain + ':' + location.port);

        socket.on( 'connect', function() {
            socket.emit( 'chat event', {
            data: 'User Connected'
        })
        var form = $( 'form' ).on( 'submit', function( e ) {
            e.preventDefault();
            let user_name = $( 'input.username' ).val();
            let user_input = $( 'input.message' ).val();
            socket.emit( 'chat event', {
                user_name : user_name,
                message : user_input
            })
            $( 'input.message' ).val( '' ).focus();
            })
        })
        socket.on( 'chat response', function( msg ) {
            console.log( msg )
            if( typeof msg.user_name !== 'undefined' ) {
              $( 'h3' ).remove()
              $( 'div.message_holder' ).append( '<div><b style="color: #000">'+msg.user_name+':</b>&nbsp; '+msg.message+'</div>' )
            }
        })
    </script>
    <script>
        $('document').ready(function() {
            setTimeout(function() {
                alerts = $(".welcome-alert");
                if (alerts) alerts.hide();

                $('<span class="badge badge-info">Communicator does not support Internet Explorer yet' +
                  ' and it is hard to say if it ever will</span>').show().appendTo('#second-alert');

            }, 6400); // <-- time in milliseconds -->
        });
    </script>
</div>
</body>

这是通过Chrome的成功通信:

(12176) accepted ('10.2.16.95', 58561)
10.2.16.95 - - [20/Jun/2019 08:06:09] "GET / HTTP/1.1" 200 4713 0.002000
10.2.16.95 - - [20/Jun/2019 08:06:10] "GET /socket.io/?EIO=3&transport=polling&t=MjpjF_7 HTTP/1.1" 200 381 0.001000
(12176) accepted ('10.2.16.95', 58567)
received chat event: {'data': 'User Connected'}
10.2.16.95 - - [20/Jun/2019 08:06:10] "POST /socket.io/?EIO=3&transport=polling&t=MjpjF_s&sid=09c17b19616b4bf681e07b7ff1cdf032 HTTP/1.1" 200 222 0.002000
10.2.16.95 - - [20/Jun/2019 08:06:10] "GET /socket.io/?EIO=3&transport=polling&t=MjpjF_t&sid=09c17b19616b4bf681e07b7ff1cdf032 HTTP/1.1" 200 215 0.001000
received chat event: {'user_name': 'smok', 'message': 'here I am'}

这与IE中的相同:

(12176) accepted ('10.2.16.95', 58750)
10.2.16.95 - - [20/Jun/2019 08:07:44] "GET / HTTP/1.1" 200 4713 0.002000
10.2.16.95 - - [20/Jun/2019 08:07:55] "POST / HTTP/1.1" 405 347 0.001000

因此IE正在连接,但无法发送消息

enter image description here

0 个答案:

没有答案