马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
前端端口: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 {
- @Override
- public void configureMessageBroker(MessageBrokerRegistry config) {
- config.enableSimpleBroker("/topic"); // 使用内存中的简单代理来广播消息
- config.setApplicationDestinationPrefixes("/app"); // 客户端发送消息到服务器的前缀
- }
- @Override
- public 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 函数返回后立即调用 sendMessage,stompClient 大概还没有被乐成初始化,因为毗连大概还没有创建。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |