// 安装依赖包
npm install @stomp/stompjs
npm install sockjs-client
// websocket
import { Stomp } from '@stomp/stompjs';
import * as SockJS from 'sockjs-client';
private host: string = environment.service;
public stompClient = null;
ngOnInit(): void {
this.connectSocket();
}
ngOnDestroy() {
this.disconnectSocket();
}
/**
* websocket 监听交易的工作流状态是否改变,若服务器有返回root_id,则弹出消息提示并刷新页面
*/
private connectSocket() {
// 与广播节点建立连接
const socket = new SockJS(`${this.host}/workflow/notifications`);
this.stompClient = Stomp.over(socket);
this.stompClient.connect({}, (frame: any) => {
// 订阅主题 /notifications/process-instance
this.stompClient.subscribe(
'/workflow/notifications/process-trade',
(message: { body: string }) => {
// 服务器返回消息
console.log(JSON.parse(message.body));
}
);
});
}
private disconnectSocket() {
if (this.stompClient != null) {
this.stompClient.disconnect();
}
}
WebSocket 实现实时消息推送
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 我们把我们的系统叫做webpush设计思路 新连接接入需要获取验证凭证token,验证凭证成为token已。与t...
- 一、socket协议的简介 WebSocket是什么,有什么优点 WebSocket是一个持久化的协议,这是相对于...
- 一、功能实现消息实时提醒,利用 SpringBoot + WebSocket实时消息推送。二、设计 三、实现代码 ...
- 一、任务要求 商家的后台管理系统实现新订单提醒推送功能,利用Spring Boot + WebSocket实时消息...
- 用户登陆后即时推送业务信息,使用element-UI的Notification 通知进行提示,并通过Notific...