当前位置: 首页 > news >正文

vue3和springboot使用websocket通信

前端端口:9090

后端端口:8080

vue3

引入依赖:

npm install sockjs-client @stomp/stompjs

vue页面

<template><div><h1>WebSocket 示例</h1><button @click="sendMessage">发送消息</button><div>{{ messages }}</div></div>
</template><script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from "vue";
import SockJS from "sockjs-client";
import { Stomp } from "@stomp/stompjs";const messages = ref();
let stompClient: any = null;//websocket连接
const connect = () => {const socket = new SockJS("http://localhost:8080/ws");stompClient = Stomp.over(socket);stompClient.connect({},(frame: string) => {console.log("Connected: " + frame);stompClient.subscribe("/topic/greetings", (message: { body: string }) => {console.log("Received: " + message.body);messages.value = message.body;//messages.value.push(message.body);});},(error: string) => {console.error("Error: " + error);});
};//发送消息
const sendMessage = () => {if (stompClient && stompClient.connected) {stompClient.send("/app/hello", {}, "hello, world");} else {console.error("No STOMP connection available");}
};onMounted(() => {connect();
});onBeforeUnmount(() => {if (stompClient) {stompClient.disconnect();}
});
</script><style scoped>
/* 添加你的样式 */
</style>

springboot

引入依赖

        <!-- Spring Boot WebSocket依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency>

websocket配置

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {@Overridepublic void configureMessageBroker(MessageBrokerRegistry config) {config.enableSimpleBroker("/topic"); // 使用内存中的简单代理来广播消息config.setApplicationDestinationPrefixes("/app"); // 客户端发送消息到服务器的前缀}@Overridepublic void registerStompEndpoints(StompEndpointRegistry registry) {registry.addEndpoint("/ws").setAllowedOrigins("http://localhost:9090") // 允许的来源列表.withSockJS(); // 注册 WebSocket 端点,并启用 SockJS 备份传输方式}
}

controller

@MessageMapping("/hello")@SendTo("/topic/greetings")public String greeting(String message) {System.out.println(message);return "你好";}

测试

点击按钮

 另一个连接这个广播主题的页面也会接受到信息

后端控制台

 

 ===============================注意====================================

这里说明一下,假如vue文件里的onMounted将连接和发送两个函数写在一起,即:

onMounted(() => {
  connect();

  sendMessage();
});

 你会发现sendMessage()里并没有发送到后端,后端你也没有返回消息。

原因:

异步性:WebSocket 连接是异步的。这意味着 connect 函数会立即返回,而实际的连接过程会在之后发生。因此,如果您在 connect 函数返回后立即调用 sendMessagestompClient 可能还没有被成功初始化,因为连接可能还没有建立。

http://www.lryc.cn/news/510397.html

相关文章:

  • JS 解构、数组扩展符和模板字符串的常见用法
  • 低代码开源项目Joget的研究——Joget7社区版安装部署
  • Golang 为什么没有注解?
  • Visual Studio Code(VS Code)配置C/C++环境
  • LabVIEW软件开发的未来趋势
  • Node.js 助力前端开发:自动化操作实战
  • HuggingFace peft LoRA 微调 LLaMA
  • 记-编译magisk_v22
  • 前端登录业务
  • 项目2路由交换
  • 易语言 OCR 文字识别
  • 云手机+YouTube:改变通信世界的划时代技术
  • C++-----------映射
  • 清空DNS 缓存
  • 计算机网络习题( 第3章 物理层 第4章 数据链路层 )
  • UE5 崩溃问题汇总!!!
  • 基于ArcGIS Pro的SWAT模型在流域水循环、水生态模拟中的应用及案例分析;SWAT模型安装、运行到结果读取全流程指导
  • Docker下TestHubo安装配置指南
  • AWS、Google Cloud Platform (GCP)、Microsoft Azure、Linode和 桔子数据 的 价格对比
  • 基础优化方法
  • v语言介绍
  • Ubuntu安装Apache Airflow详细指南
  • 【数据可视化复习方向】
  • CentOS下安装RabbitMQ
  • 探究音频丢字位置和丢字时间对pesq分数的影响
  • 音视频入门基础:MPEG2-TS专题(23)——通过FFprobe显示TS流每个packet的信息
  • Bert各种变体——RoBERTA/ALBERT/DistillBert
  • Go入门篇:(一)golang的安装和编辑工具安装
  • 【技术实战】R语言统计分析与可视化从入门到精通
  • 【Lua之·Lua与C/C++交互·Lua CAPI访问栈操作】