change route and send message using socketio but socketio is working

。_饼干妹妹 提交于 2019-12-11 15:39:41

问题


<!DOCTYPE html>
<html>
    <head>
        <title>Login</title>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>

   <script>
        document.addEventListener('DOMContentLoaded',()=>{

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

            socket.on('connect',()=>{
                console.log('connected');
               document.querySelector('#submit').onclick =() =>{
                const user= document.querySelector('#user').value;
                const room = document.querySelector('#room').value;
                socket.emit('join',{'user':user,'room':room});
                console.log('emitted');
                return false;
               };

            });

        });

    </script>

    </head>
    <body>
    <form id="new-task" action="{{ url_for('chat') }}" method="post">
        <input type="text" autocomplete="off" autofocus id="user" placeholder="username"> 
        <input type="text" autocomplete="off" autofocus id="room" placeholder="room">
        <input type="submit" id="submit" value="join">     
    </form> 
    </body>
</html>

localhost:5000 open index.html page , when i click on submit only socketio is working but url is not being changed .

import os
import requests

from flask import Flask,jsonify,render_template,request
from flask_socketio import SocketIO,emit,join_room,leave_room,send

from werkzeug import secure_filename



app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
socketio=SocketIO(app)



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


@app.route("/chat" ,methods=["POST"])
def chat():
    print("running chat")
    return render_template("chat.html")


@socketio.on('join')
def on_join(data):
    username = data['user']
    room = data['room']
    join_room(room)
    emit('chat',{'username':username},room=room)
    print("room has been allocated")

when i did only url_for without socketio i was able to change route but now it does not seem to work. in console and cmd i am able to see socketio working but chat route is not working

来源:https://stackoverflow.com/questions/52357417/change-route-and-send-message-using-socketio-but-socketio-is-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!