event_handler_map = { MessageType.login: login, MessageType.send_message: send_message, MessageType.register: register, MessageType.resolve_friend_request: resolve_friend_request, MessageType.client_echo: client_echo, MessageType.add_friend: add_friend, MessageType.join_room: join_room, MessageType.create_room: create_room, MessageType.query_room_users: query_room_users, MessageType.bad: bad, } def handle_event(sc, event_type, parameters): event_handler_map[event_type].run(sc, parameters) 这段代码什么意思
时间: 2023-06-20 13:10:42 浏览: 54
这段代码定义了一个事件处理器映射表 `event_handler_map`,将消息类型(`MessageType`)与相应的事件处理函数关联起来。然后定义了一个 `handle_event` 函数,它接受三个参数:`sc` 表示客户端连接,`event_type` 表示事件类型,`parameters` 表示事件参数。`handle_event` 函数会根据 `event_type` 在 `event_handler_map` 中查找相应的事件处理函数,并调用该函数来处理事件。
相关问题
"errorMessage": "Handler 'lambda_handler' missing on module 'lambda_function'", "errorType": "Runtime.HandlerNotFound", 报错如何解决
这个错误信息表明在AWS Lambda函数中,找不到名为`lambda_handler`的处理程序。`lambda_handler`通常是一个特定的函数,当Lambda函数被触发执行时,AWS会寻找这个名称来启动执行流程。如果未找到,就会抛出`Runtime.HandlerNotFound`异常。
要解决这个问题,你需要确保以下几个步骤:
1. **检查函数定义**:确认你的函数是否定义了`lambda_handler`,并且它是正确的签名(通常是`def lambda_handler(event, context):`)。如果用其他名称,记得在`handler`参数中指定对应的函数名。
2. **代码组织**:`lambda_function.py`文件中可能有导入错误或模块结构导致`lambda_handler`无法识别。检查是否有误导入、命名空间冲突或者函数定义的位置是否正确。
3. **函数注册**:如果你使用的是AWS Lambda的Python SDK或配置文件(如`.aws/lambda`目录下的`config.json`),确保已经正确指定了`handler`属性,指向你的处理函数。
4. **更新函数配置**:在AWS Lambda控制台或通过命令行工具(如`aws lambda update-function-code`)更新函数配置,确保新版本包含了正确的`lambda_handler`。
5. **测试函数**:重新部署函数并测试,看是否能成功执行。
如果问题依然存在,检查是否有最近的代码更改可能影响了`lambda_handler`的查找,或者是否是与其他依赖项冲突。在必要时,清理并重新创建函数可能会解决问题。如果问题持续,请查看Lambda的日志输出,它可能提供了更多关于错误的上下文信息。
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class); if (annotation != null) { if (this.isRepeatSubmit(request, annotation)) { AjaxResult ajaxResult = AjaxResult.error(annotation.message()); ServletUtils.renderString(response, JSON.toJSONString(ajaxResult)); return false; } } } boolean flag = false; TokenService tokenService = SpringUtils.getBean(TokenService.class); LoginAppUser loginUser = tokenService.getLoginUser(request); if (loginUser != null && loginUser.getGuest() != null && loginUser.getGuest().getGuestNum() != null) { tokenService.verifyToken(loginUser); String guestNum = loginUser.getGuest().getGuestNum(); HttpSession httpSession = request.getSession(); RedisCache redisUtil = SpringUtils.getBean(RedisCache.class); Guest guest = redisUtil.getCacheMapValue(RedisConstants.GUEST_MAP, guestNum); SysGuest sysGuest = JSON.parseObject(JSON.toJSONString(guest), SysGuest.class); if (sysGuest == null) { flag = false; httpSession.removeAttribute(Constants.SERVER_TYPE_APP); } else { if (httpSession.getAttribute(Constants.SERVER_TYPE_APP) == null) { httpSession.setAttribute(Constants.SERVER_TYPE_APP , sysGuest); } return true; } } 优化这段代码
首先,可以将该方法拆分成两个方法,一个处理重复提交的逻辑,一个处理登录用户的逻辑,以提高代码可读性和可维护性。
处理重复提交的逻辑可以改为使用注解方式,遍历所有的参数,判断是否存在 @Token 注解,如果存在则验证 token 是否一致。
处理登录用户的逻辑可以简化代码,将获取当前登录用户和验证 token 的逻辑移动到 TokenService 中,简化该方法代码。
重构后的代码如下所示:
```
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
Token annotation = method.getAnnotation(Token.class);
if (annotation != null) {
if (!tokenService.verifyToken(request)) {
AjaxResult ajaxResult = AjaxResult.error(annotation.message());
ServletUtils.renderString(response, JSON.toJSONString(ajaxResult));
return false;
}
}
}
if (tokenService.checkLogin(request)) {
HttpSession session = request.getSession();
if (session.getAttribute(Constants.SERVER_TYPE_APP) == null) {
session.setAttribute(Constants.SERVER_TYPE_APP, tokenService.getLoginUser(request).getGuest());
}
return true;
} else {
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(HttpStatus.UNAUTHORIZED.value(), "未登录或登录超时。")));
return false;
}
}
```
其中,`TokenService` 的实现中包括了 `verifyToken` 和 `checkLogin` 方法,分别用于验证 token 和检查当前用户是否已经登录。同时,重复提交的逻辑使用 `@Token` 注解进行处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
Traceback (most recent call last): File "d:\pythonbaijiahao\baijiahao.py", line 82, in mianrun sub_element.click() File "D:\python3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 81, in click self._execute(Command.CLICK_ELEMENT) File "D:\python3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 710, in _execute return self._parent.execute(command, params) File "D:\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 425, in execute self.error_handler.check_response(response) File "D:\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element 管理商品 is not clickable at point (930, 347). Other element would receive the click: <svg viewBox="64 64 896 896" focusable="false" data-icon="vertical-align-top" width="1em" height="1em" fill="currentColor" aria-hidden="true">...</svg> (Session info: MicrosoftEdge=113.0.1774.42) Stacktrace:
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)