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

台前网站建设费用青岛手工活外发加工网

台前网站建设费用,青岛手工活外发加工网,在线教育oem平台,大连免费营销型建站网络推广实现功能 基于Springboot与Vue架构,首先使用Websocket实现频道订阅,在实现点对点与群发功能后,在前端调用windows自带的消息通知,实现推送功能。 开发环境 Springboot 2.6.7vue 2.6.11socket-client 1.0.0 准备工作 在 Vue.js…

实现功能

        基于Springboot与Vue架构,首先使用Websocket实现频道订阅,在实现点对点与群发功能后,在前端调用windows自带的消息通知,实现推送功能。

开发环境

  • Springboot 2.6.7
  • vue 2.6.11
  • socket-client 1.0.0

准备工作

在 Vue.js 项目中安装sockjs-client和stompjs。

npm install sockjs-client stompjs

在后端项目中添加依赖。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

WebSocket

         为完成单播或者广播的频道的订阅功能,需要在前端和后端完成WebSocket的基本配置。

前端配置

        需在vue项目中新建websocket.js文件,主要完成:

        1.获取userid

        2.定义WebSocketService,完成connect、subscribe、unsubscribe、sendMessage、disconnect等主要函数。

代码如下所示。

websocket.js
import SockJS from 'sockjs-client';
import Stomp from 'stompjs';const userid = JSON.parse(sessionStorage.getItem('CurUser')).idclass WebSocketService {constructor() {this.stompClient = null;this.subscriptions = new Map(); // 存储订阅的频道}// 连接 WebSocketconnect() {const socket = new SockJS("/api/broadcast"); // 后端地址this.stompClient = Stomp.over(socket);this.stompClient.connect({}, () => {console.log('do connect method');console.log('stompClient', this.stompClient)this.subscribe('/user/' + userid.toString() + '/alone/getResponse', (response) => {if (this.onGlobalNotification) {console.log("message print", response)// this.onGlobalNotification(JSON.parse(message.body));this.onGlobalNotification(response.body);}});});}// 订阅频道subscribe(destination, callback) {const subscription = this.stompClient.subscribe(destination, callback);this.subscriptions.set(destination, subscription);}// 取消订阅unsubscribe(destination) {const subscription = this.subscriptions.get(destination);if (subscription) {subscription.unsubscribe();this.subscriptions.delete(destination);}}// 发送消息到后端sendMessage(destination, message) {console.log("sendMessage method , message is ", message.content)this.stompClient.send(destination, {}, message.content);}// 断开连接disconnect() {if (this.stompClient) {this.stompClient.disconnect();console.log('WebSocket 断开连接!');}}
}export default new WebSocketService();

后端配置

        在后端同样需要完成频道的订阅,新建文件WebSocketConfig.java,代码如下所示。

package com.wms.common;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;import javax.annotation.Resource;@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {@Resourcepublic AppConfig appConfig;@Overridepublic void configureMessageBroker(MessageBrokerRegistry registry) {registry.enableSimpleBroker("/user", "/topic1", "/topic2", "/mass");// 点对点使用的订阅前缀(客户端订阅路径上会体现出来),不设置的话,默认也是 /user/// 注意,这里必须和上面设置的Broker:/user 一致(两个都可以自定义,但必须一致)。否则连接不上registry.setUserDestinationPrefix("/user/");// 指服务端接收地址的前缀,意思就是说客户端给服务端发消息的地址的前缀}@Overridepublic void registerStompEndpoints(StompEndpointRegistry registry) {// 注册一个STOMP的endpoint端点,并指定使用SockJS协议// 前端使用这个地址连接后端 WebSocket接口registry.addEndpoint("/broadcast", "/point")// 允许所有源跨域。还可以指定ip配置:http://ip:*// 低版本的SpringBoot(2.1.5.RELEASE 就不行)不行.setAllowedOriginPatterns(appConfig.getFrontHttpUrl()) // 此处填写前端页面地址.withSockJS();}
}

Vue

        在用户登录之后,首先初始化WebSocket 连接,然后定义全局通知的回调,在回调函数onGlobalNotification中实现调用windows自带的通知功能Notification。

// 初始化 WebSocket 连接
WebSocketService.connect();
// 定义全局通知的回调
WebSocketService.onGlobalNotification = (message) => {this.sendNotification('xxx',message)// this.$bus.$emit("postTrigger")
};
    // 发送通知的方法sendNotification (title, body) {// console.log("sendNotification", Notification.permission)// 检查浏览器是否支持 Notification APIif ('Notification' in window) {// 如果通知权限已经授予if (Notification.permission === "granted") {new Notification(title, {body: body,icon: logoIcon,requireInteraction: true});} else if (Notification.permission !== "denied") {// 请求用户授权Notification.requestPermission().then(permission => {if (permission === "granted") {new Notification(title, {body: body,icon: logoIcon,requireInteraction: true});}});}} else {console.log("浏览器不支持通知功能。");}},

        在需要发送消息的地方使用函数sendMessage,在MethodApi处填写后端对应接口。

WebSocketService.sendMessage('/MethodApi', {content: "message you want send"});

Springboot

        在后端需要在controller文件中实现对应的MethodApi接口函数, 确定消息传递的目标用户与对应的消息,调用messagingTemplate中的函数convertAndSendToUser,完成在频道/user/userid/alone/getResponse的消息通知。

controller.java
@Autowired
private SimpMessagingTemplate messagingTemplate;// p2p notification
@MessageMapping("/MethodApi")
public void p2pNotify(String content){System.out.println(content);// 可在此处通过接受到的消息确定目标用户 userid(int) 与 对应的消息 message(string)System.out.println("=====发送通知=====");messagingTemplate.convertAndSendToUser(userid.toString(),"/alone/getResponse",message);
}

效果图

        此时就可以在本地进行测试了,效果如下:

服务器部署

        因为windows系统通知需要浏览器给予网页权限,所以需要vue启用https协议,配置如下:

module.exports = {devServer: {port:8001,https:true,proxy: {'/api': {target: "http://localhost:8091/", // 代理目标的基础路径secure: true,  // 如果是https接口,需要配置这个参数changeOrigin: true, // 支持跨域pathRewrite: {'^/api': '',}}}},
}
http://www.dtcms.com/wzjs/596098.html

相关文章:

  • 优秀网站首页设计济南seo网站推广公司
  • 吉安高端网站建设公司网站 建设意见
  • 做网站搞笑口号网店加盟
  • 网站排名点击邯郸做网站哪家好
  • 做网站有自己的服务器吗物流商 网站建设方案
  • 网站推广公司兴田德润电话多少wordpress文章图片本地化
  • 建设网站的推广的软文北京商场招商
  • wordpress主页显示关键词优化的策略
  • 公司定制网站建设公司专门做孕婴用品的网站
  • 网站栏目名seo推广优化
  • 番禺网站建设三杰科技中国建设银行网站的主要功能
  • 网站建设费用上海合肥晚报社官方网站
  • 如何提高网站加载速度慢wordpress文章tags
  • 目录网站做外链长沙seo培训
  • 如何建网站免费网站建设项目数
  • 网站建设投票主题cps广告是什么意思
  • 兰州网站建设和维护工作wordpress模板网
  • 哪些网站做的比较好看的图片唯品会网站建设数据安全分析
  • 网站开发公司哪里好网络优化大师手机版
  • 青海营销网站建设服务一元购网站开发
  • 30分钟seo网站淄博城乡建设局网站
  • 花生壳做网站需要备案值得买wordpress
  • 怎样做内网网站安徽建设局网站
  • 重庆家政网站建设网站建设项目验收表
  • 宁波网站建设哪里有手机微信网站链接
  • flash企业网站源码中国域名注册中心
  • 狍与女人做爰网站如何制作个人网站教程
  • 网站优化 图片百度关键词排名代发
  • 重庆一站式建设网站平台市场咨询公司排名
  • 墨鱼网站建设合肥企业网站推广