def get(self, *args, **kwargs): ''' 渲染页面 ''' chat_with_id = self.get_argument('user_id', None) chat_group_id = self.get_argument('group_id', None) self.cookie = int(self.get_cookie('user')) user_detail = user.filter(user=self.cookie)[0] frinds = user_user.filter(user_id=self.cookie) quanzi = user_group.filter(user_id=user_detail['user']) if chat_with_id: chat_with = user.filter(user=int(chat_with_id))[0] self.set_cookie('chat_id', chat_with_id) self.set_cookie('change', 'one') elif chat_group_id: _dic = groups.filter(number=int(chat_group_id))[0] chat_with = { 'name': _dic['name'], 'id': _dic['id'], 'user': _dic['number'] } self.set_cookie('group_id', str(_dic['number'])) self.set_cookie('change', 'many') #[{'id': 1, 'group_id': 1000, 'user_id': 200}] # chat_with = user.filter(user=int(_dic['user_id'])) else: chat_with = {'name': '自己', 'id': 1, 'user': 100} frinds_list = [] groups_list = [] for frind in frinds: _id = frind['frinds_id'] frind_detail = user.filter(user=_id)[0] frinds_list.append(frind_detail) for quan in quanzi: _id = quan['group_id'] group_detail = groups.filter(number=int(_id))[0] groups_list.append(group_detail) stus = { #{'passwd': '101', 'name': '121', 'id': 10, 'user': 1} 'user_detail': user_detail, #[{'name': 'a', 'passwd': '123', 'id': 1, 'user': 100}] 'frinds': frinds_list, #[{'name': 'gr', 'id': 1, 'number': 1000}] 'groups': groups_list, 'chat_with': chat_with, } self.render('chat_rom.html', stus=stus)
def open(self, *args, **kwargs): ''' 当建立连接 ''' # 将登陆的用户存储到用户列表 self.users.append(self) self.cookie = self.get_cookie('user') self.chat_id = [] if self.get_cookie('change') == 'one': self.chat_id2 = self.get_cookie('chat_id') self.chat_id.append(self.chat_id2) else: chat_id = self.get_cookie('group_id') chat_id = user_group.filter(group_id=int(chat_id)) for num in chat_id: self.chat_id.append(num['user_id']) for chat in self.chat_id: for u in self.users: if u.cookie == str(chat): print('有人接入') name = user.filter(user=int(u.cookie))[0]['name'] u.write_message("[%s]开始聊天" % (name))
def on_message(self, message): ''' 当发消息 ''' for chat in self.chat_id: for u in self.users: cookiess = u.get_cookie('user') if str(cookiess) == str(chat): print('服务器收到一条消息') name = user.filter(user=int(self.cookie))[0]['name'] u.write_message("[%s]说:%s" % (name, message))
def on_close(self): ''' 当关闭 ''' self.users.remove(self) for chat in self.chat_id: for u in self.users: if u.cookie == str(chat): print('有人断开连接') name = user.filter(user=int(u.cookie))[0]['name'] u.write_message("[%s]离开聊天" % (name))
from models import user context = {'user': '******'} a = user.filter(**context) print(a)