简体   繁体   中英

How to create simple “CHAT APP” using Flask-SocketIO, Python, Javascript?

How can I edit these codes so that when I click the button, the input texts will be appended to the empty unordered lists and be able to broadcast to other "users"?

HTML code

    <body>
       <ul>
       </ul>
       <input type="text" id="myMessage">
       <button>Send</button>
    </body>

Javascript Template code

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

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

    socket.on('connect', () => {

    });

    socket.on('announce chat', data => {

    });
});

Python code

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

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

@socketio.on("submit chat")
def chat(data):

Well you dont call your submit chat and you should format it like submit_chat not with white space in between.

On your socket.io script you dont have submit_chat called you do have announce chat...

 socket.on('submit_chat', data => {

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM