SpringBoot 利用 WebSocket 监听第三方接口的回调验证

SpringBoot 利用 WebSocket 监听第三方接口的回调验证

导入依赖

		<dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-core</artifactId>
            <version>1.13.1</version>
        </dependency>

Websocket 相关配置类

@Configuration
public class WebSocketConfiguration {

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

具体的监听的路径

@Api("监听服务")
@ServerEndpoint("/callBack/call")     ---  监听的端口
@Component
@Slf4j
public class WebSocketServer {
    private static Session session;

    @OnOpen
    public void onOpen(Session session) throws IOException {
        System.out.println(System.currentTimeMillis()+"------------");
        System.out.println("WebSocket opened: " + session.getId());
        LogUtils.LOGGER.info("webSocket会话连接",session);
        this.session = session;
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        LogUtils.LOGGER.info("--------------会话连接成功---------------");
        LogUtils.LOGGER.info("Received message: " + message);
        System.out.println("Received message: " + message);
        // 在这里处理接收到的消息,并根据需要发送响应消息
        if (session.isOpen()) {
            try {
                session.getBasicRemote().sendText("Hello from server!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        LogUtils.LOGGER.info("--------------会话连接成功---------------");
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println(System.currentTimeMillis()+"###############");
        LogUtils.LOGGER.info("--------------会话关闭开始---------------");
        LogUtils.LOGGER.info("WebSocket closed: " + session.getId());
        System.out.println("WebSocket closed: " + session.getId());
        this.session = null;
        LogUtils.LOGGER.info("--------------会话关闭结束---------------");
    }

    @OnError
    public void onError(Throwable error) {
        LogUtils.LOGGER.info("--------------会话异常开始---------------");
        LogUtils.LOGGER.info("WebSocket error: " + error.getMessage());
        System.out.println("WebSocket error: " + error.getMessage());
        LogUtils.LOGGER.info("--------------会话异常结束---------------");
    }

    public static void sendMessage(String message) throws IOException {
        if (session != null && session.isOpen()) {
            session.getBasicRemote().sendText(message);
        }
    }
}

前端VUE3配合使用

const socket = new WebSocket("ws://localhost:8081/callBack/call");
// 连接成功时触发
socket.onopen = function(event) {
  console.log("WebSocket已连接");
};

// 接收到消息时触发
socket.onmessage = function(event) {
  console.log("接收到消息:" + event.data);
  //关闭连接
  //4秒后关闭连接并发送一个关闭帧,关闭码为1000(表示正常关闭)
  socket.close(1000,"文本描述",4000);
};

// 连接关闭时触发
socket.onclose = function(event) {
  console.log("WebSocket已关闭");
};
// 发生错误时触发
socket.onerror = function(event) {
  console.error("WebSocket发生错误:" + event.message);
};

这样就完成了!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值