loms_web项目websocket的应用

loms_web项目websocket的应用先封装 websocket import WsServer from standard business wsServer import host from globalConfig import SockJS from sockjs client import Stomp from

大家好,我是讯享网,很高兴认识大家。

先封装websocket: 

import WsServer from '../standard-business/wsServer'; import {host} from './globalConfig'; import SockJS from 'sockjs-client'; import Stomp from 'stompjs'; class WebSocketProxy { constructor() { this.sockets = {}; this.stomps = {}; } sendMessageToBrowser(token, message) { const sockets = this.sockets[token] || []; for (const ws of sockets) { WsServer.sendMessage(ws, 'global', message); } } stompDisconnect(token) { const sockets = this.sockets[token] || []; this.stomps[token] = null; this.sockets[token] = null; for (const ws of sockets) { ws.close(); } } createStompClient(token) { const path = `${host}/epld-websocket`; const sock = new SockJS(path, null, {'transports': ['websocket']}); const stompClient = Stomp.over(sock); this.stomps[token] = stompClient; const onConnect = () => { console.log('stomp connect 成功'); stompClient.subscribe('/user/queue/notifications', ({body}) => { try { const data = JSON.parse(body); data.senderInfo = JSON.parse(data.senderInfo); this.sendMessageToBrowser(token, data); } catch (e) { console.log(e); } }); }; const onError = (error) => { console.log(error); this.stompDisconnect(token); }; stompClient.connect({token}, onConnect, onError); } webSocketClosed(ws) { const token = ws.token; const sockets = this.sockets[token]; if (sockets) { this.sockets[token] = sockets.filter(socket => socket !== ws); if (!this.sockets[token].length) { this.sockets[token] = null; if (this.stomps[token]) { try { this.stomps[token].disconnect(); } catch (e) { console.log('webSocketClosed 捕获异常'); this.stomps[token] = null; } } } } } addWebSocket(ws) { const token = ws.token; if (token) { if (this.sockets[token]) { this.sockets[token].push(ws); } else { this.sockets[token] = []; this.sockets[token].push(ws); this.createStompClient(token); } ws.on('close', () => { console.log(`${ws.username}(token:${ws.token}) disconnection`); this.webSocketClosed(ws); }); } } } export default WebSocketProxy; 

讯享网

在同构的服务器端构造代码里加入websocket服务器: 


讯享网

讯享网// 建立WS服务器 const wsProxy = new WebSocketProxy(); const wsServer = WsServer.createServer(server); wsServer.onConnect(ws => wsProxy.addWebSocket(ws));

 全局调用:

const msgConfig = [ {title: '系统消息', url: '/message/msg_system'}, {title: '订单消息', url: '/message/msg_order'}, {title: '派单消息', url: '/message/msg_dispatch'}, {title: '跟踪消息', url: '/message/msg_track'}, {title: '异常消息', url: '/message/msg_abnormal'}, ]; const showGlobalMessage = (type=0, message) => { const config = msgConfig[type]; const key = new Date().getTime(); notification.open({ key, message: config.title, description: <Link to={config.url} onClick={() => notification.close(key)}>{message}</Link>, duration: 10, placement: 'bottomRight', style: { whiteSpace: 'pre' } }); }; // 建立WS连接 const connectWsServer = () => { const wsClient = getWsClient(); wsClient.onMessage('global', ({body}) => { showGlobalMessage(body.type, body.content); }); };

 

小讯
上一篇 2025-02-21 18:09
下一篇 2025-03-03 15:47

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/44061.html