From 31436232da228a6583969f979be889ef6c0d8009 Mon Sep 17 00:00:00 2001 From: Nobelf <13517042@std.stei.itb.ac.id> Date: Tue, 12 Apr 2022 12:52:38 +0700 Subject: [PATCH] simple websocket app --- app.py | 13 +++++++++++++ front.html | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 app.py create mode 100644 front.html diff --git a/app.py b/app.py new file mode 100644 index 0000000..1a5dea2 --- /dev/null +++ b/app.py @@ -0,0 +1,13 @@ +from flask import Flask +from flask_socketio import SocketIO, send + +app = Flask(__name__) +app.config['SECRET_KEY'] = 'secret!' +socketio = SocketIO(app, cors_allowed_origins='*') + +@socketio.on('message') +def handleMessage(msg): + send("message received : " + msg, broadcast=False) #will be replaced with QA matching function + +if __name__ == '__main__': + socketio.run(app) \ No newline at end of file diff --git a/front.html b/front.html new file mode 100644 index 0000000..cd4e97f --- /dev/null +++ b/front.html @@ -0,0 +1,31 @@ +<html> + <head> + <title>Chat Room</title> + <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js" integrity="sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA==" crossorigin="anonymous"></script> + <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> + </head> + <body> + <script type="text/javascript"> + $(document).ready(function() { + var socket = io.connect('http://127.0.0.1:5000'); + + socket.on('connect', function() { + socket.send('connected'); + }); + + socket.on('message', function(msg) { + $("#messages").append('<li>'+msg+'</li>') + }); + + $('#sendbutton').on('click',function() { + socket.send($('#myMessage').val()); + $('#myMessage').val(''); + }); + }); + + </script> + <ul id="messages"></ul> + <input type="text" id="myMessage"> + <button id="sendbutton">Send</button> + </body> +</html> \ No newline at end of file -- GitLab