GUNS项目集成WebSocket

GUNS项目集成WebSocket

  • 添加模块

    org.springframework.boot
    spring-boot-starter-websocket

  • 注入端点服务
   /**
     * 注入一个ServerEndpointExporter
     * 使用注解@ServerEndpoint 注册服务站点
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
  • 编写端点代码
@Slf4j
@Component
@ServerEndpoint(value = "/test/oneToOne")
public class WebSocketService {

    /** 记录当前在线连接数 */
    private static AtomicInteger onlineCount = new AtomicInteger(0);

    /** 存放所有在线的客户端 */
    private static Map clients = new ConcurrentHashMap<>();

    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session) {
        onlineCount.incrementAndGet(); // 在线数加1
        clients.put(session.getId(), session);
        log.info("有新连接加入:{},当前在线人数为:{}", session.getId(), onlineCount.get());
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(Session session) {
        onlineCount.decrementAndGet(); // 在线数减1
        clients.remove(session.getId());
        log.info("有一连接关闭:{},当前在线人数为:{}", session.getId(), onlineCount.get());
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message
     *            客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        log.info("服务端收到客户端[{}]的消息[{}]", session.getId(), message);
        try {
            if("0".equals(message)){
                clients.put("0",session);
                session.getBasicRemote().sendText(message);
            }
        } catch (Exception e) {
            log.error("解析失败:{}", e);
        }
    }

    @OnError
    public void onError(Session session, Throwable error) {
        log.error("发生错误");
        error.printStackTrace();
    }

    /**
     * 服务端发送消息给客户端
     */
    public void sendMessage(String message) {
        try {
            log.info("服务端给客户端发送消息[{}]", message);
            clients.get("0").getBasicRemote().sendText(message);
        } catch (Exception e) {
            log.error("服务端发送消息给客户端失败:{}", e);
        }
    }

}

  • 将项目中的拦截的权限放开
    GUNS项目集成WebSocket_第1张图片
  • 放开权限
    GUNS项目集成WebSocket_第2张图片

运行项目时报如下错误

2021-12-10 16:20:24.614 ERROR 7280 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to register @ServerEndpoint class: class cn.stylefeng.guns.modular.nakuservice.plugin.websocket.WebSocketService$$EnhancerBySpringCGLIB$$1986263b
	at org.springframework.web.socket.server.standard.ServerEndpointExporter.registerEndpoint(ServerEndpointExporter.java:158)
	at org.springframework.web.socket.server.standard.ServerEndpointExporter.registerEndpoints(ServerEndpointExporter.java:133)
	at org.springframework.web.socket.server.standard.ServerEndpointExporter.afterSingletonsInstantiated(ServerEndpointExporter.java:111)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:862)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
	at cn.stylefeng.guns.GunsApplication.main(GunsApplication.java:36)
Caused by: javax.websocket.DeploymentException: Cannot deploy POJO class [cn.stylefeng.guns.modular.nakuservice.plugin.websocket.WebSocketService$$EnhancerBySpringCGLIB$$1986263b] as it is not annotated with @ServerEndpoint
	at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:220)
	at org.springframework.web.socket.server.standard.ServerEndpointExporter.registerEndpoint(ServerEndpointExporter.java:155)
	... 12 common frames omitted

问题源自于项目本身的代理拦截机制与自己编写的包名匹配,项目内置的拦截机制是:
GUNS项目集成WebSocket_第3张图片
我项目中写代码的包名如下
GUNS项目集成WebSocket_第4张图片
我这里将包名nakuservice换一个不是service结尾的就好了。

在我查资料的过程中,这位道友的解说很有参考价值:华阳余文乐

你可能感兴趣的:(编辑器,vscode,macos)