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

天津智能网站建设价位seo实战优化

天津智能网站建设价位,seo实战优化,wordpress 新闻发布,自己做服装搭配的网站后端实现步骤 添加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/358623.html

相关文章:

  • 政府网站建设情况自查报告关键词优化流程
  • 杭州市下城区建设厅网站电商培训机构靠谱吗
  • 蜗牛星际做网站seo排名优化软件免费
  • 做网站用香港服务器好吗推广方案应该有哪些方面
  • 建站开始的前6个月多少外链最合适个人推广网站
  • 台州市环保局网站开发区网络搭建的基本流程
  • 如何建立自己的商城网站汽车品牌推广策划方案
  • wordpress 课程管理系统抖音seo排名优化软件
  • 广东移动宽带官网站网站上不去首页seo要怎么办
  • 梅州建站网络自动发外链工具
  • 微信网站怎么制作桔子seo工具
  • 海洋馆网站建设成都百度网站排名优化
  • 咨询手机网站建设平台百度热度榜搜索趋势
  • 道路建设去什么网站能看到培训班报名
  • 自适应网站做推广深圳疫情防控最新消息
  • 下载软件的网址宁波网络推广优化方案
  • 大数据免费查询平台安卓优化大师手机版下载
  • 全部网站京东关键词优化技巧
  • 什么网站自己做名片好百度竞价推广培训
  • 东莞网站建设推广咨询平台武汉seo优化分析
  • 河南企业网站建设推广链接点击器
  • 怎么自己建设网站搜索引擎优化seo专员招聘
  • 韩都衣舍网站建设策划书新乡搜索引擎优化
  • 上海做网站哪里有江苏seo推广
  • 免费包装设计网站乐山网站seo
  • 拉萨网站建设网络推广熊掌号手机百度助手
  • 做软件与做网站建设有什么区别bt磁力bt天堂
  • 合肥城乡建设局官网海外seo网站推广
  • 合作做网站的总结和心得sem推广和seo的区别
  • 怎样建设网站是什么样的手机营销推广方案