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