class NetCore(threading.Thread): def __init__(self, engine): super().__init__() self.__engine = engine self.__app = Flask(__name__) self.__sio = SocketIO(self.__app) def run(self): self.__app.add_url_rule('/', 'core', self.core) self.__app.add_url_rule('/<path:path>', 'res', self.res) self.__sio.on_event('connect', self.on_connect) self.__sio.on_event('data', self.on_data) self.__sio.run(self.__app, port=80) def core(self): resp = make_response(send_file('../ui/index.html')) return resp def res(self, path): """""" if os.path.exists('ui/' + path): resp = make_response(send_file('../ui/' + path)) return resp print('FILE NOT FOUND: {}'.format('ui/' + path)) return 'PAGE NOT FOUND: {}'.format(path) def send(self, data): print('SEND: {}'.format(data)) print(self.__sio.__hash__()) print(threading.current_thread().__hash__()) self.__sio.emit('data', data) self.__sio.sleep() def on_connect(self): """""" self.__engine.info('Connected from ID: {}'.format(request.sid)) self.__engine.on_connect(request.sid) resp = make_response() return resp def on_data(self, data): print('RECV: {}'.format(data)) print(self.__sio.__hash__()) print(threading.current_thread().__hash__()) self.send(data) self.__engine.recv(data)
class oNetCore(threading.Thread): def __init__(self, engine): """""" super().__init__() self.__engine = engine self.__app = Flask(__name__) self.__sio = SocketIO(self.__app) self.__address = None def run(self): """""" self.create_server(('localhost', 80)) def create_server(self, address=None): """""" self.address = address self.__app.add_url_rule('/', 'core', self.core) self.__app.add_url_rule('/<path:path>', 'res', self.res) self.__sio.on_event('connect', self.on_connect) self.__sio.on_event('data', self.on_data) self.__sio.on_error(self.on_error) self.server_core() # t = threading.Thread(target=self.server_core) # t.run() def server_core(self): """""" if self.address: config = { 'host': self.address[0], 'port': self.address[1], 'debug': False } self.__sio.run(self.__app, **config) else: self.__sio.run(self.__app) def on_connect(self): """""" def t(): while True: self.send('{}') time.sleep(1) a = threading.Thread(target=t) a.start() self.__engine.info('Connected from ID: {}'.format(request.sid)) self.__engine.on_connect(request.sid) # self.__sio.emit('msg', 'msg') resp = make_response() # resp.set_cookie('sid', request.sid) return resp def on_data(self, data): """""" self.__engine.debug('RECV: {}'.format(data)) self.__engine.emit(data['type'], data) def core(self): """""" resp = make_response(send_file('../ui/index.html')) # resp.set_cookie('id', '123') return resp # with open('ui/index.html', encoding='utf-8') as f: # return f.read() def res(self, path): """""" # print(path) if os.path.exists('ui/' + path): resp = make_response(send_file('../ui/' + path)) return resp # with open('front/'+path, 'r', encoding='utf-8') as f: # return f.read() print('FILE NOT FOUND: {}'.format('ui/' + path)) return 'PAGE NOT FOUND: {}'.format(path) def send(self, data): self.__engine.debug('SEND: {}'.format(data)) self.__sio.emit('data', data) print(self.__sio.__hash__()) SocketIO.sleep(0)