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

天津智能网站建设价位上海网络推广公司网站

天津智能网站建设价位,上海网络推广公司网站,给别人做网站需要什么许可证,那些网站做推广后端实现步骤 添加Spring Boot WebSocket依赖配置WebSocket端点和消息代理创建控制器,使用SimpMessagingTemplate发送消息 前端实现步骤 安装sockjs-client和stompjs库封装WebSocket连接工具类在Vue组件中建立连接,订阅主题 详细实现步骤 后端&…

后端实现步骤

  • 添加Spring Boot WebSocket依赖
  • 配置WebSocket端点和消息代理
  • 创建控制器,使用SimpMessagingTemplate发送消息

前端实现步骤

  • 安装sockjs-client和stompjs库
  • 封装WebSocket连接工具类
  • 在Vue组件中建立连接,订阅主题

详细实现步骤

后端(Spring Boot)实现步骤

1. 添加依赖
<!-- pom.xml -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2. 配置WebSocket
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {@Overridepublic void registerStompEndpoints(StompEndpointRegistry registry) {// 注册WebSocket端点,前端连接此地址registry.addEndpoint("/ws").setAllowedOriginPatterns("*") // 解决跨域问题.withSockJS(); // 支持SockJS}@Overridepublic void configureMessageBroker(MessageBrokerRegistry registry) {// 客户端订阅的主题前缀registry.enableSimpleBroker("/topic");}
}

3. 发送消息的Controller
@RestController
public class MessageController {@Autowiredprivate SimpMessagingTemplate messagingTemplate;// 发送消息到所有客户端@GetMapping("/send")public void sendToAll(String message) {messagingTemplate.convertAndSend("/topic/messages", message);}}

前端(Vue)实现步骤

1. 安装依赖
npm install sockjs-client stompjs
2. 封装WebSocket工具类
// src/utils/websocket.js
import SockJS from 'sockjs-client';
import Stomp from 'stompjs';
let stompClient = null;
export function connect(url,callback) {const socket = new SockJS(url);stompClient = Stomp.over(socket);stompClient.connect({}, () => {stompClient.subscribe('/topic/messages', (message) => {callback(message.body)});});
}
export function disconnect() {if (stompClient) {stompClient.disconnect();}
}
3. Vue组件集成
<template><div><div>收到消息: {{ receivedMsg }}</div></div>
</template>
<script>
import { connect, disconnect } from '@/ws/websocket';export default {data() {return {inputMsg: '',receivedMsg: ''};},mounted() {connect('http://localhost:8088/ws',(msg)=>{this.receivedMsg = msg;});},beforeDestroy() {disconnect();},methods: {}
};
</script>

测试

向指定客户端发送消息

后端

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency>
package com.config;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;/*** @author cyz*/
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {@Overridepublic void registerStompEndpoints(StompEndpointRegistry registry) {// 客户端连接的端点(WebSocket URL)registry.addEndpoint("/ws")// 允许所有来源(根据需求调整).setAllowedOrigins("*")// 支持 SockJS 降级(兼容不支持 WebSocket 的浏览器).withSockJS();}@Overridepublic void configureMessageBroker(MessageBrokerRegistry registry) {// 客户端订阅的地址前缀(STOMP 主题)registry.enableSimpleBroker("/portCheckProgress");}
}
package com;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/*** @author cyz* @since 2025/4/1 下午5:15*/
@RestController
public class MessageController {@Autowiredprivate SimpMessagingTemplate messagingTemplate;@GetMapping("/send-to-user")public void sendToUser(@RequestParam String userCode, @RequestParam String message) {String destination =  "/portCheckProgress/info/"+userCode;messagingTemplate.convertAndSend(destination, message);}
}
package com;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author cyz* @since 2025/4/1 下午5:12*/
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

前端 

// src/utils/websocket.js
import SockJS from 'sockjs-client';
import Stomp from 'stompjs';
let stompClient = null;
export function connect(url,userCode,callback) {const socket = new SockJS(url);stompClient = Stomp.over(socket);stompClient.connect({}, () => {stompClient.subscribe('/portCheckProgress/info/'+userCode, (message) => {callback(message.body)});});
}
export function disconnect() {if (stompClient) {stompClient.disconnect();}
}
<template><div><div>收到消息: {{ receivedMsg }}</div></div>
</template>
<script>
import { connect, disconnect } from '@/ws/websocket';export default {data() {return {inputMsg: '',receivedMsg: ''};},mounted() {var split = location.href.split("?userCode=");var userCode = split[1]connect('http://localhost:8088/ws',userCode,(msg)=>{this.receivedMsg = msg;});},beforeDestroy() {disconnect();},methods: {}
};
</script>

测试

向2中发送消息 

 向3中发送消息 

http://www.dtcms.com/wzjs/491132.html

相关文章:

  • wordpress伪静态配置seo排名赚app
  • 贵阳网站关键字优化百度账号登录中心
  • 官方网站建设手机银行郑州网络推广厂家
  • 互联网网站建设是什么企业邮箱哪个好
  • 如何建设教师网上授课网站网页分析工具
  • 帮别人做设计的网站企业qq官方下载
  • 做网站需要什么资质seo在线培训机构
  • 做网络课程的网站搜索引擎优化的基础是什么
  • 动态网站开发课程设计如何做好网络推广
  • 做网站东莞东莞建网站国内最新消息
  • 鹤壁做网站公司海南seo快速排名优化多少钱
  • 制作人漫画武汉seo搜索优化
  • wordpress怎么设置跳站外链接google中文搜索引擎入口
  • 哪些网站做京东的团购网络推广与营销
  • 怎么做网站快捷方式网站平台怎么推广
  • 有教做路桥质检资料的网站吗如何制作自己的网页
  • 网站主机空间价格数据分析网
  • wordpress网站搬家seo技术建站
  • pc访问手机网站跳转山东疫情最新情况
  • 网站图片模板seo外贸公司推广
  • 制作公司网站的步骤北京seo外包
  • 做网站的优势有哪些杭州seo全网营销
  • 北京常规网络营销电话网站关键词优化案例
  • 惠州手机网站商城建设北京网站建设专业公司
  • 比较还做的调查网站谷歌推广外贸建站
  • 华强北电子网站建设网络舆情监控
  • 网站 系统设置磁力链bt磁力天堂
  • 企业网站需求分析代做百度首页排名价格
  • seo推广员招聘seo和sem的区别与联系
  • 网站报价内容软考培训机构哪家好一点