You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.5 KiB
64 lines
1.5 KiB
from tornado import web, ioloop, options, websocket
|
|
import os.path
|
|
|
|
clients = []
|
|
messages = []
|
|
|
|
class Application(web.Application):
|
|
def __init__(self):
|
|
handlers = [
|
|
(r"/", MainHandler),
|
|
(r"/chat", ChatHandler),
|
|
]
|
|
settings = dict(
|
|
cookie_secret="tes12323hhdfdfre9312313",
|
|
template_path=os.path.join(os.path.dirname(__file__), "templates"),
|
|
static_path=os.path.join(os.path.dirname(__file__), "static"),
|
|
xsrf_cookies=True,
|
|
)
|
|
super().__init__(handlers, **settings)
|
|
|
|
|
|
class MainHandler(web.RequestHandler):
|
|
|
|
def get(self):
|
|
self.render("index.html")
|
|
|
|
|
|
class ChatHandler(websocket.WebSocketHandler):
|
|
|
|
def open(self):
|
|
print("Web socket opened")
|
|
clients.append(self)
|
|
|
|
def on_message(self, message):
|
|
messages.append({'msg':message})
|
|
for msg in messages:
|
|
self.write_message(msg)
|
|
|
|
def check_origin(self, origin):
|
|
return True
|
|
|
|
|
|
def main():
|
|
options.parse_command_line()
|
|
app = Application()
|
|
app.listen(8888)
|
|
ioloop.IOLoop.current().start()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|
|
|
|
|
|
|
|
@gen.coroutine
|
|
def get_messages_from_order_chat(self, order_id):
|
|
sql = "SELECT text FROM chat_message WHERE order_id=" + order_id
|
|
cursor = yield self.db.execute(sql)
|
|
messages = cursor.fetchall()
|
|
for msg in messages:
|
|
for waiter in self.waiters:
|
|
waiter.write_message({'msg': msg[0], 'answer_type': 'contact'})
|
|
|