Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Python3中使用Flask在html上每秒发出字符串?_Javascript_Python_Html_Flask_Socket.io - Fatal编程技术网

Javascript 如何在Python3中使用Flask在html上每秒发出字符串?

Javascript 如何在Python3中使用Flask在html上每秒发出字符串?,javascript,python,html,flask,socket.io,Javascript,Python,Html,Flask,Socket.io,我在Python3上使用SocketIO with Flask,如果不使用JavaScript在html中重新加载标记'div',我无法刷新。我不希望客户这样做,因此我有以下代码: 蟒蛇3: @app.route("/chat", methods=['GET', 'POST']) @login_required def in_game(): room = session.get('room', '') game_on = True user = Use

我在Python3上使用SocketIO with Flask,如果不使用JavaScript在html中重新加载标记'div',我无法刷新。我不希望客户这样做,因此我有以下代码:

蟒蛇3:

@app.route("/chat", methods=['GET', 'POST'])
@login_required
def in_game():
    room = session.get('room', '')
    game_on = True
    user = User.get_by_id(current_user.id)
    player = Player.get_by_id(current_user.id)
    game = Game.get_by_id(current_user.id)
    tiempo_partida = datetime.now()
    while game_on:
        socketio.emit('game', {'info': "State: "+game.state+" Time: "+(tiempo_partida - datetime.now()).__str__()[14:19]+" Piece: "+player.piece+" Position: "+player.pos}, namespace='/chat')
        return render_template("game2.html", room=room)
html:

{%extensed“base_template.html”%}
{%block title%}客户端{%endblock%}
{%block content%}
无功插座;
$(文档).ready(函数(){
socket=io.connect('http://'+document.domain+':'+location.port+'/chat');
socket.on('game',函数(数据){
$('#state').val($('#state').val()=data.info);
});
});


代码没有给我错误,但是没有显示我不想显示的字符串。 提前谢谢你的帮助

{% extends "base_template.html" %}
{% block title %}client{% endblock %}
{% block content %}
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
  var socket;
  $(document).ready(function(){
    socket = io.connect('http://' + document.domain + ':' + location.port + '/chat');
    socket.on('game', function(data) {
      $('#state').val($('#state').val() = data.info);
    });
  });
</script>
<body>
  <div id="state"></div><br><br>
</body>