#!/usr/bin/env python # Set this variable to "threading", "eventlet" or "gevent" to test the # different async modes, or leave it set to None for the application to choose # the best option based on available packages. async_mode = None if async_mode is None: try: import eventlet async_mode = 'eventlet' except ImportError: pass if async_mode is None: try: from gevent import monkey async_mode = 'gevent' except ImportError: pass if async_mode is None: async_mode = 'threading' print('async_mode is ' + async_mode) # monkey patching is necessary because this application uses a background # thread if async_mode == 'eventlet': import eventlet eventlet.monkey_patch() elif async_mode == 'gevent': from gevent import monkey monkey.patch_all() import json import time from threading import Thread from flask import Flask, render_template from flask_socketio import SocketIO from numpy import exp, cos, linspace import os, re app = Flask(__name__, static_url_path='/static') app.config['SECRET_KEY'] = 'secret!' socketio = SocketIO(app, async_mode=async_mode) thread = None @socketio.on('dataPacket', namespace='/') def handle_message(message): if app.debug: print('Obtained packet from station: ') print message # print message['data'] socketio.emit('dataPacket', {'data': 'Server generated event', 'packet': message}, namespace='/web') @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': socketio.run(app, host='0.0.0.0', port=5000, debug=False)