消息语音提醒

This commit is contained in:
Vinjor 2024-10-17 15:22:49 +08:00
parent f4b57db744
commit de9eec4689
2 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,100 @@
package cn.iocoder.yudao.module.app.controller.admin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
/**
* 维修APP消息监听
* @author vinjor-M
* @date 10:22 2024/10/17
**/
@ServerEndpoint(value = "/websocket/message/{tenantId}/{userId}")
@Component
public class NotifyMessageSocket {
private final static Logger log = LoggerFactory.getLogger(NotifyMessageSocket.class);
// 保存链接的sessionkey为用户名,value为对应的session名
public static ConcurrentHashMap<String, Session> sessionMap = new ConcurrentHashMap<>();
//关键代码设置一个静态上下文属性appcontext
private static ApplicationContext appcontext;
public static void setAppcontext(ApplicationContext appcontext) {
NotifyMessageSocket.appcontext = appcontext;
}
public static ApplicationContext getAppcontext() {
return appcontext;
}
/**
* 创建连接
* 用于监听建立连接当有客户端与该服务端点建立连接时将会自回调该注解标注的方法
* @param session
* @param userId
*/
@OnOpen
public void onOpen(Session session, @PathParam(value = "tenantId") String tenantId,@PathParam(value = "userId") String userId) {
this.sessionMap.put(tenantId+"_"+userId,session);
log.info("{}租户下用户{}已创建连接", tenantId,userId);
}
/**
* 用于监听客户端向服务端发送消息当客户端与服务端发送消息时将会回调该注解标注的方法
* {
* Stringitude:124.11,
* latitude:125.33,
* positionInfo:"山东省济南市市中区八一立交桥"
* }
* @param msg
* @param userId
*/
@OnMessage
public void onMessage(String msg,@PathParam(value = "userId") String userId){
System.out.println("消息通知+"+userId);
}
/**
* 用于监听连接关闭当客户端与该服务端点断开连接时将会回调该注解标注的方法
* @param session
* @param userId
*/
@OnClose
public void onClose(Session session,@PathParam(value = "tenantId") String tenantId,@PathParam(value = "userId") String userId){
this.sessionMap.remove(tenantId+"_"+userId);
}
/**
* 用于监听该连接上的任何错误当客户端与该服务端点的连接发生任何异常都将回调该注解标注的方法
* 注意该方法的参数必选Throwable可选Sessiion以及0-n个String参数且String参数需要使用@PathParam注解标注
* @param throwable
* @param tenantId 租户id
* @param userId 用户id
*/
@OnError
public void onError(Throwable throwable,@PathParam(value = "tenantId") String tenantId,@PathParam(value = "userId") String userId){
log.error("{}租户下用户{}已创建连接", tenantId,userId);
}
/**
* 发送给指定的用户
* @param message
*/
public void sendMessage(String message, String tenantId,String userId) throws IOException {
if (sessionMap.containsKey(tenantId+"_"+userId)){
Session session = sessionMap.get(tenantId+"_"+userId);
session.getAsyncRemote().sendText(message);
}
}
}

View File

@ -1,7 +1,10 @@
package cn.iocoder.yudao.module.base.controller.admin;
import cn.iocoder.yudao.common.MessageSend;
import cn.iocoder.yudao.common.dto.MessageDTO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.app.controller.admin.NotifyMessageSocket;
import cn.iocoder.yudao.module.base.entity.RepairWorker;
import cn.iocoder.yudao.module.base.service.RepairWorkerService;
import cn.iocoder.yudao.module.base.vo.RepairWorkerPageReqVO;
@ -13,12 +16,17 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static cn.iocoder.yudao.common.BaseConstants.QUALS_INTERIM_PERIOD;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.ok;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Tag(name = "管理后台 - 维修工人")
@ -29,6 +37,10 @@ public class RepairWorkerController {
@Resource
private RepairWorkerService workerService;
@Autowired
private NotifyMessageSocket notifyMessageSocket;
@Autowired
private MessageSend messageSend;
/**
@ -124,4 +136,30 @@ public class RepairWorkerController {
}
/**
* 测试给维修工发送消息---测试
* @author vinjor-M
* @date 18:08 2024/10/16
* @return cn.iocoder.yudao.framework.common.pojo.CommonResult<?>
**/
@GetMapping("/sendMessage")
@Operation(summary = "测试给维修工发送消息")
public CommonResult<?> sendMessage(Long userId) {
try {
MessageDTO messageDTO = new MessageDTO();
List<Long> userIds = new ArrayList<>();
userIds.add(userId);
messageDTO.setUserIds(userIds);
messageDTO.setTemplateCode(QUALS_INTERIM_PERIOD);
List<Object> paramList = new ArrayList<>();
paramList.add("测试");
paramList.add("3");
messageDTO.setParamList(paramList);
messageSend.send(messageDTO);
notifyMessageSocket.sendMessage("11","180",userId.toString());
} catch (IOException e) {
e.printStackTrace();
}
return ok();
}
}