application.py
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from engineio.async_drivers import gevent
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = False
# socketio = SocketIO(app, engineio_logger=True,logger=True, log_output=False, async_mode='gevent_uwsgi')
socketio = SocketIO(app, async_mode="gevent", allow_upgrades=True)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/test')
def test():
return render_template('index.html')
@socketio.on('myevent')
def test_message(message):
emit('myresponse', {'data': 'got it!'})
@socketio.on('chat message')
def test_message(message):
print('ohkay')
emit('chat message', message)
if __name__ == '__main__':
socketio.run(app,debug=True)
templates/index.html
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
#messages { margin-bottom: 40px }
</style>
<!-- <link rel="icon" href="#" type="image/x-icon">-->
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/socket.io.js"></script>
<script>
$(function () {
var socket = io.connect(document.domain + ':' + location.port,{ transports: [ 'websocket','pooling'],upgrade:false});
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
console.log("emitted event");
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
window.scrollTo(0, document.body.scrollHeight);
});
});
</script>
my requirments.txt
certifi
click
eventlet
Flask
Flask-Login
Flask-Session
Flask-SocketIO
gevent
greenlet
itsdangerous
Jinja2
MarkupSafe
python-engineio
python-socketio
six
waitress
Werkzeug
wfastcgi
gevent-websocket
gunicorn
i put we socket into on
web app is deployed and code running but got error like
websocket.js:111 WebSocket connection to 'wss://chatbotpython.azurewebsites.net/socket.io /?EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 503 WS.doOpen @ websocket.js:111