def ExceptionCloseConnectionHandler(request, sessid): ''' 异常情况下断开链接的处理方法 ''' horner_gear = HornerGear() _application = request.application horner_gear.removeLiveSession(sessid) _application.live_request_list.removeLiveRequestHandler(sessid)
def _loadRoom(self): ''' 加载聊天室房间 :param room_id: :return: ''' horner_gear_handler = HornerGear().getHandler() if horner_gear_handler.exists(self.currentRoomKey()): self._loadSuccess = True self.ParticipantList = [ensureString(_talker) for _talker in horner_gear_handler.smembers(self.room_key)]
def removeRequestUuid(self, request_uuid): ''' 删除一个大厅中的活跃会话 :param request_uuid: :return: ''' horner_gear_handler = HornerGear().getHandler() if request_uuid in self.ParticipantList: self.ParticipantList.remove(request_uuid) horner_gear_handler.srem(self.room_key, request_uuid)
def removeRequestUuid(self, request_uuid): ''' 删除一个客户端请求标识 :param request_uuid: :return: bool ''' if request_uuid in self.ParticipantList: horner_gear_handler = HornerGear().getHandler() horner_gear_handler.srem(self.room_key, request_uuid) self.ParticipantList.remove(request_uuid) return True return False
def addRequestUuid(self, request_uuid): ''' 添加一个客户端请求标识 :param request_uuid: :return: bool ''' if self._loadSuccess: horner_gear_handler = HornerGear().getHandler() if 1 == horner_gear_handler.sadd(self.room_key, request_uuid): self.ParticipantList.append(request_uuid) return True return False
def createChatRoom(company_id, first_uuid, room_id=None): ''' 创建一个聊天室 :param company_id string 公司编号 :param first_uuid string 第一个进入聊天室的会话编号 :return: string room_id ''' room_id = str(uuid1()) if room_id is None else room_id horner_gear_handler = HornerGear().getHandler() room_key_label = ChatSupport.getRoomKey(company_id, room_id) if not horner_gear_handler.exists(room_key_label): if 1 == horner_gear_handler.sadd(room_key_label, first_uuid): return room_id return None
def closeRoomNotice(self, live_request_list): ''' 关闭(解散)房间通知 :return: ''' from business.logic.noticer.app_response import ConchSystemNoticer from business.logic.protocol import HornerPacketHandler evt_close_room = ConchSystemNoticer(HornerPacketHandler.COMMUNICATION_EVENT, HornerPacketHandler.EVT_CMD_SYSTEM, ConchSystemNoticer.SYSTEM_EVT_TYPE_CLOSE_CHATROOM) evt_close_room.setRoomId(self.room_id) evt_close_room.changeSuccess() self.broadcast(live_request_list, evt_close_room.toString()) # 清理房间资源 horner_gear_handler = HornerGear().getHandler() room_key_label = ChatSupport.getRoomKey(self.company_id, self.room_id) if horner_gear_handler.exists(room_key_label): horner_gear_handler.delete(room_key_label)
def quitFromAllTalkRoom(self): ''' 从所有聊天室中退出 ''' horner_gear_handler = HornerGear().getHandler() talk_room_wildcard_label = "%s:%s:*" % (CacheKeys.CHATROOM_KEY, self.company_id) _same_company_rooms = horner_gear_handler.keys(talk_room_wildcard_label) for _room_id in _same_company_rooms: _regular_room_name = ensureString(_room_id).split(':') if 3 == len(_regular_room_name): _check_room = Chatroom(self.company_id, _regular_room_name[2]) if _check_room.isLoadSuccess(): _check_room.removeRequestUuid(self.sess_id) if (1 == _check_room.cntParticipates()): _check_room.closeRoomNotice(self.application.getLiveRequestHandlerList()) # 从公司大厅中删除 self._quitFromHall()
def allPaticipatedRoom(company_id, sess_id): ''' 列出所有参与进的聊天室 :param comnay_id: :param sess_id: :return: list ''' ret_room_id_list = [] match_key = "%s:%s:*" % (CacheKeys.CHATROOM_KEY, company_id) horner_gear_handler = HornerGear().getHandler() room_list = horner_gear_handler.keys(match_key) for _room_label in room_list: _regular_room_name = ensureString(_room_label).split(':') if (3 == len(_regular_room_name)): _room_key = Chatroom.getRoomKey(company_id, _regular_room_name[2]) if (horner_gear_handler.sismember(_room_key, sess_id)): ret_room_id_list.append(_regular_room_name[2]) return ret_room_id_list
def run(self): horner_gear = HornerGear() if self.sameUUID(self.sess_id): # 客户端UUID冲突 err_noticer = ConchResponseNoticer(HornerPacketHandler.COMMUNICATION_RESPONSE, ConchResponseNoticer.ERR_CONFLICTION_UUID) err_noticer.setErrorID(ConchResponseNoticer.ERR_CONFLICTION_UUID) return err_noticer else: _noticer = ConchBeginEventNoticer(HornerPacketHandler.COMMUNICATION_EVENT, HornerPacketHandler.EVT_CMD_CONNECTION) _noticer.setSessId(self.live_request.getUUID()) if self.live_request.isCustomService(): # 当前用户会话是客服人员 horner_gear.addLiveSessionToCompany(self.company_id, self.live_request.getUUID()) _noticer.changeSuccess() else: # 客户进入对话,选择客服并创建聊天室 _noticer.setIsVisitor() group_id = self._package.get('gid') if group_id is None: group_id = 'default' custom_service_id = horner_gear.choiceCustomServicer(self.company_id, self._package.get('gid')) if (custom_service_id is not None): custom_service_id = ensureString(custom_service_id) c_room_id = Chatroom.createChatRoom(self.company_id, custom_service_id) if c_room_id is not None: room = Chatroom(self.company_id, c_room_id) if room.isLoadSuccess(): room.addRequestUuid(self.live_request.getUUID()) horner_gear.addLiveSessionToCompany(self.company_id, self.live_request.getUUID()) _noticer.setRoomId(c_room_id) # 通知客服有新对话房间建立 self._notice_room_created(custom_service_id, c_room_id, self.client_infor) # 房间广播新人加入事件 self._notice_room_joined(c_room_id, self.client_infor) _noticer.changeSuccess() else: _noticer.setMessage('没有客服人员在线,前端改留言板服务!') return _noticer
def createHall(company_id, first_uuid): hall_key_label = "%s:%s" % (CacheKeys.CHATHALL_KEY, company_id) horner_gear_handler = HornerGear().getHandler() return 1 == horner_gear_handler.sadd(hall_key_label, first_uuid)