-1

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

Prateek Naik
  • 2,522
  • 4
  • 18
  • 38

1 Answers1

0

I have seen similar issue fixed by either of the following, kindly try these steps and let us know.

  1. If Web Socket option is not enabled on the app itself. Kindly turn it on from Azure Portal > Your App> Settings> Configuration > General Settings > Web socket -> On.

I understand, you have mentioned "i put we socket into on", kindly confirm if my understanding correct. If the WebSocket is already On, please try 2.

  1. Scale-up your App Service Plan (ASP) to a higher tier and then test. If it's Free or Share ASP, you may see such error.

Capture F12 network trace from the browser and review for any specific error.

Review/isolate to see if local firewall is causing any issues.

You may also leverage App Service diagnostics from Azure Portal to fetch more details on the error, To access App Service diagnostics, navigate to your app in the Azure portal > App Service Settings> blade > 'All App Service settings' > In the left navigation, click on Diagnose and solve problems – Checkout the tile for “Diagnostic Tools” and “Availability and Performance”.

AjayKumar
  • 2,812
  • 1
  • 9
  • 28