| | |
| | | handleBusinessMessage(ctx, messageObj); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | // 如果不是JSON格式,按普通文本处理 |
| | | log.debug("消息不是JSON格式,按普通文本处理: {}", message); |
| | | ctx.channel().writeAndFlush(new TextWebSocketFrame("服务器时间: " + LocalDateTime.now() + "->" + message)); |
| | |
| | | if (appUserId != null) { |
| | | log.info("收到用户 {} 的业务消息: {}", appUserId, messageObj.toJSONString()); |
| | | |
| | | businessMsgHandlers.get(messageObj.getString("type")).handle(appUserId, messageObj.getString("message")); |
| | | //判断处理用户实时位置 |
| | | if ("location".equals(messageObj.getString("type"))) { |
| | | log.info("收到用户 {} 的实时位置消息: {}", appUserId, messageObj.toJSONString()); |
| | | Double latitude = messageObj.getDouble("latitude"); |
| | | Double longitude = messageObj.getDouble("longitude"); |
| | | saveAppUserLocation(Long.parseLong(appUserId), latitude, longitude); |
| | | } |
| | | businessMsgHandlers.get(messageObj.getString("type")).handle(appUserId, messageObj.toJSONString()); |
| | | |
| | | // 回复消息 |
| | | JSONObject response = new JSONObject(); |
| | | response.put("type", "message_response"); |
| | | response.put("appUserId", appUserId); |
| | | response.put("originalMessage", messageObj); |
| | | response.put("serverTime", LocalDateTime.now().toString()); |
| | | |
| | | ctx.channel().writeAndFlush(new TextWebSocketFrame(response.toJSONString())); |
| | | // JSONObject response = new JSONObject(); |
| | | // response.put("type", "message_response"); |
| | | // response.put("appUserId", appUserId); |
| | | // response.put("originalMessage", messageObj); |
| | | // response.put("serverTime", LocalDateTime.now().toString()); |
| | | // |
| | | // ctx.channel().writeAndFlush(new TextWebSocketFrame(response.toJSONString())); |
| | | } else { |
| | | log.warn("收到未认证连接的业务消息: {}", messageObj.toJSONString()); |
| | | |
| | |
| | | ctx.channel().close(); |
| | | } |
| | | |
| | | |
| | | |
| | | private void saveAppUserLocation(Long appUserId, Double latitude, Double longitude) { |
| | | AppUserPosition userPosition = mongoTemplate.findOne(Query.query(Criteria.where("appUserId").is(appUserId)), AppUserPosition.class); |
| | | GeoJsonPoint point = new GeoJsonPoint(longitude, latitude); |
| | | if(null != userPosition){ |
| | | mongoTemplate.updateFirst(Query.query(Criteria.where("appUserId").is(appUserId)), |
| | | Update.update("point", point).set("addTime", System.currentTimeMillis()), AppUserPosition.class); |
| | | }else{ |
| | | userPosition = new AppUserPosition(); |
| | | userPosition.setAppUserId(appUserId); |
| | | userPosition.setPoint(point); |
| | | userPosition.setAddTime(System.currentTimeMillis()); |
| | | mongoTemplate.save(userPosition); |
| | | } |
| | | } |
| | | } |