def message_ex_old(uid, fid, eid): if current_user.id == int(eid): m = Message() ums = Message.query.filter(Message.uid == uid, Message.fid == fid, Message.eid == eid).order_by( Message.timestamp) eicon = User.query.filter(User.id == int(eid)).first().icon uicon = User.query.filter(User.id == int(uid)).first().icon content = request.form.get('text') if request.method == 'POST' and content: m.fid = int(fid) m.uid = int(uid) m.eid = int(eid) m.name = current_user.username # 发消息的人 m.timestamp = time.strftime("%Y-%m-%d %H:%M:%S") # 发送时间 f = UserFiles.query.filter(UserFiles.id == int(fid)).first() m.title = f.fileTitle m.content = content # 消息内容 m.expert = True # 是专家发言 m.save() return render_template('expertcenter/message_old.html', ums=ums, eicon=eicon, uicon=uicon) else: return render_template('common/nocan.html', u=current_user.username)
def message(uid, fid, eid): if current_user.id == int(uid): # 判断对方是否存在 if not User.query.filter(User.id == int(eid)).first(): return render_template('common/nonentity.html') else: m = Message() ums = Message.query.filter( Message.uid == int(uid), Message.fid == int(fid), Message.eid == int(eid)).order_by(Message.timestamp) # 页面最多显示信息条数 num = 30 # off 偏移量 跳过这么多数据再去查询 off = len(ums.all()) - num if off > 0: showums = ums.offset(off) else: showums = ums.offset(0) content = request.form.get('text') eicon = User.query.filter(User.id == int(eid)).first().icon uicon = User.query.filter(User.id == int(uid)).first().icon if request.method == 'POST' and content: m.fid = int(fid) m.uid = int(uid) m.eid = int(eid) m.name = current_user.username # 发消息的人 m.timestamp = time.strftime("%Y-%m-%d %H:%M:%S") # 发送时间 f = UserFiles.query.filter(UserFiles.id == int(fid)).first() m.title = f.fileTitle m.content = content # 消息内容 m.expert = False # 不是专家发言 m.save() return render_template('owncenter/message.html', ums=showums, eicon=eicon, uicon=uicon) else: return render_template('common/nocan.html', u=current_user.username)
def post(self): user=g.user args = parse_room_message.parse_args() roomid = args.get("roomid") room=Room.query.get(roomid) if room is None: abort(404, msg="room don't exit") picture = request.files.get("picture") if picture is None: abort(404, msg="请上传图片") try: picture.filename = secure_filename(picture.filename) picture.filename = change_filename(picture.filename) picture.filename = str(user.id) + picture.filename filepath = PIC_DIR + picture.filename picture.save(filepath) except: abort(401,"图片上传失败") message = Message() message.body = "http://39.106.119.191/uploads/pictures/"+picture.filename message.type = 2 message.message_author = user.id message.message_room = roomid message.from_user = user.username message.user_icon = user.icon message.timestamp = datetime.now() message.save() for name, u_socket in user_socket_dict[roomid].items(): try: data = dict(marshal(message, message_fields)) u_socket.send(str(data)) except Exception: pass
def get(self): user = g.user # print(user.id) usr_socket = request.environ.get("wsgi.websocket") # type:WebSocket # print(usr_socket) # 查询用户的房间 并添加到通讯字典 for room in user.rooms: if usr_socket: addtwodimdict(user_socket_dict, room.id, user.username, usr_socket) # 添加到通讯字典的同时,发送给该房间所有人上线信息 for name, u_socket in user_socket_dict[room.id].items(): data = { "status": HTTP_OK, "num": len(user_socket_dict[room.id]), "is_online": 1, "username": user.username, "roomid":room.id, "type":0, } try: u_socket.send(str(data)) except WebSocketError as e: print(user_socket_dict) if user.username in user_socket_dict[room.id]: del user_socket_dict[room.id][user.username] print(e) return # print(user_socket_dict) while True: try: msg = usr_socket.receive() print(msg) except: for room in user.rooms: if user.username in user_socket_dict[room.id]: del user_socket_dict[room.id][user.username] for name, u_socket in user_socket_dict[room.id].items(): data = { "status": HTTP_OK, "num": len(user_socket_dict[room.id]), "is_online": 0, "username": user.username, } u_socket.send(str(data)) break # break if msg is None: for room in user.rooms: if user.username in user_socket_dict[room.id]: del user_socket_dict[room.id][user.username] for name, u_socket in user_socket_dict[room.id].items(): data = { "status": HTTP_OK, "num": len(user_socket_dict[room.id]), "is_online": 0, "username": user.username, } u_socket.send(str(data)) break if msg: # print(msg) try: msg = eval(msg) except: break if msg.get("type") == 0: #登录 is_online = msg.get("is_online") #登出 if is_online == 0: for room in user.rooms: if user.username in user_socket_dict[room.id]: del user_socket_dict[room.id][user.username] for name, u_socket in user_socket_dict[room.id].items(): data = { "status": HTTP_OK, "num": len(user_socket_dict[room.id]), "is_online": is_online, "username":user.username, } u_socket.send(str(data)) if is_online == 0: break # 发送文字信息 if msg.get("type") == 1: message = Message() message.body = msg.get('msg') message.type = msg.get("type") roomid = msg.get("roomid") room = Room.query.get(roomid) if room is None: abort(400, msg="请提供正确的roomid") message.message_author = user.id message.message_room = room.id message.from_user = user.username message.user_icon = user.icon # print(datetime.now()) message.timestamp = datetime.now() message.save() for name, u_socket in user_socket_dict[room.id].items(): try: data = dict(marshal(message, message_fields)) u_socket.send(str(data)) # print("sended") except WebSocketError as e: print(user_socket_dict) print(e) if user.username in user_socket_dict[room.id]: del user_socket_dict[room.id][user.username] break