def SetupServer(app=None, socketio=None, host='localhost', port=5000, debug=True): # Initial Setup global FLASK_CORE_APP, SOCKET_IO_CORE, DB_CONNECTION, MESSAGING_GATEWAY if app is None: app = FLASK_CORE_APP if socketio is None: socketio = SOCKET_IO_CORE aPort = int(os.getenv('PORT', port)) if (aPort != port or host != 'localhost') and debug: host = '0.0.0.0' debug = False logWarning("Going to host 0") app.debug = bool(debug) #Allow some env specification of helpful test services services = [ CSVLoggingService("logfile.csv"), ] if os.getenv('TESTSERVICE', ''): logWarning('SKO_Architecture.MessagingGateway.TestService will be run') from SKO_Architecture.MessagingGateway import TestService services.append(TestService()) MESSAGING_GATEWAY = HTTPMessagingGateway( None, SOCKET_IO_CORE, flask.ext.socketio, services ) return socketio
def StartServer(app=None, socketio=None, host='localhost', port=5000, debug=True): socketio = SetupServer(app, socketio, host, port, debug) Thread(target=background_thread).start() logWarning("Starting Socket App") try: socketio.run(app, host=host, port=port) except Exception as err: DB_CONNECTION.close()
def template_imports(path): if path[-5:] in ('.html',): try: path = path.split('//')[-1] logWarning(path) return render_template(path) except Exception: abort(404) else: return abort(404)
def logfile_imports(path): if path[-4:] in ('.log', '.csv'): try: path = path.split('//') path.insert(0, 'Services') path = '//'.join(path) logWarning(path) return FLASK_CORE_APP.send_static_file(path) except Exception: abort(404) else: return abort(404)
def onDisconnect(): logWarning('Client disconnected')
def onConnect(): logWarning("Connected")
def onConnect(): logWarning("Connected") #flask.ext.socketio.emit('my response', {'data': 'Connected', 'count': 0}) @SOCKET_IO_CORE.on('disconnect', namespace='/messaging') def onDisconnect(): logWarning('Client disconnected') #First check to see if HOST and PORT were specified in the #environment - if so, we use those values directly and skip #the "usual" logic below envHost, envPort = None, None try: envHost = os.getenv('HOST', None) envPort = int(os.getenv('PORT', 0)) except: pass if envHost and envPort: logWarning("Using Host/Port from environment: %s:%d" % (envHost, envPort)) StartServer(FLASK_CORE_APP, SOCKET_IO_CORE, envHost, envPort, DEBUG_MODE) elif DEBUG_MODE: StartDebugServer(FLASK_CORE_APP, SOCKET_IO_CORE) elif DEBUG_ON_VM: StartVMDebugServer(FLASK_CORE_APP, SOCKET_IO_CORE) elif False: StartDirectServer(FLASK_CORE_APP, SOCKET_IO_CORE) else: StartProductionServer(FLASK_CORE_APP, SOCKET_IO_CORE)