因爲是先後端分離的demo, 程序的後端我無論,我只負責把前端作好,這只是個demo, 還有不少不完善的地方。css
2018-01-09新增:
後端的MQ事件結構如今也改了,該demo只能看看了。html
html前端
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8"> <link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <table class="table" id="event-queue"> <thead> <tr> <th>當前狀態</th> <th>狀態改變時間</th> <th>姓名</th> <th>工號</th> <th>分機號</th> <th>對方號碼</th> <th>呼入數</th> <th>呼出數</th> </tr> </thead> <tbody> <tr v-for="item in eventQueue"> <td>{{item.agentStatus | transAgentStatus}}</td> <td>{{item.agentStatusTime}}</td> <td>{{item.userName}}</td> <td>{{item.loginName}}</td> <td>{{item.deviceId}}</td> <td></td> <td></td> <td></td> </tr> </tbody> </table> <script src="http://cdn.bootcss.com/vue/1.0.26/vue.js"></script> <script src="js/websocket-suport.min.js"></script> <script src="js/main.js"></script> </body> </html>
jsvue
var tm = (function(){ var App = function(){}; var app = App.prototype; var config = { dest: 'http://xxx.xxx.xxx.xxx:58080/mvc/stomp', topic: '/topic/csta/namespace/testwdd2.com' // topic: '/topic/csta/device/8002@testwdd2.com' }; var eventQueue = []; var vm = new Vue({ el:'#event-queue', data:{ eventQueue: eventQueue } }); Vue.filter('transAgentStatus', function(status){ switch(status){ case 'NotReady': return '未就緒'; case 'WorkNotReady': return '話後處理狀態'; case 'Idle': return '就緒'; case 'OnCallIn': return '呼入通話'; case 'OnCallOut': return '呼出通話'; case 'Logout': return '登出'; case 'Ringing': return '振鈴'; case 'OffHook': return '摘機'; case 'CallInternal': return '內部通話'; case 'Dailing': return '外線已經振鈴'; case 'Ringback': return '回鈴'; case 'Conference': return '會議'; case 'OnHold': return '保持'; case 'Other': return '其餘'; } return ''; }); /** * [render description] * @Author Wdd * @DateTime 2016-12-26T16:06:16+0800 * @param {[string]} tpl [模板字符串] * @param {[object]} data [data對象] * @return {[string]} [渲染後的字符串] */ app.render = function(tpl,data){ var re = /{{([^}]+)?}}/g; while(match = re.exec(tpl)){ tpl = tpl.replace(match[0],data[match[1]] || ''); } return tpl; }; app.initWebSocket = function(dest, topic){ dest = dest || config.dest; topic = topic || config.topic; var socket = new SockJS(dest); var ws = Stomp.over(socket); ws.connect({}, function(frame) { ws.subscribe(topic, function(event) { // var eventInfo = JSON.parse(event.body); app.handerEvent(JSON.parse(event.body)); }); }, function(frame) { console.log(frame); console.error(new Date() + 'websocket失去鏈接'); }); }; /** * [findAgentIndex description] * @Author Wdd * @DateTime 2016-12-28T10:34:13+0800 * @param {[string]} agentId [description] * @return {[int]} [description] */ app.findAgentIndex = function(agentId){ for(var i = eventQueue.length - 1; i >= 0; i--){ if(eventQueue[i].agentId === agentId){ return i; } } return -1; }; /** * [handerEvent 處理websocket事件] * @Author Wdd * @DateTime 2016-12-28T10:33:03+0800 * @param {[object]} data [description] * @return {[type]} [description] */ app.handerEvent = function(data){ if(data.eventType === 'CallEvent'){ return; } if(!data.eventSrc){ return; } var eventItem = { agentStatus: '', eventName: data.eventName, agentId: '', loginName: '', userName: '', deviceId: data.deviceId, agentStatusTime: '' }; var agent = data.eventSrc.agent || ''; if(agent){ eventItem.agentId = agent.agentId; eventItem.loginName = agent.loginName; eventItem.userName = agent.userName; eventItem.agentStatus = agent.agentStatus; eventItem.agentStatusTime = agent.agentStatusTime; } // 針對登出事件的agentId在外層 else if(data.agentMode){ eventItem.agentStatus = data.agentMode; eventItem.agentId = data.agentId; } else if(data.agentStatus){ eventItem.agentStatus = data.agentStatus; } if(!eventItem.agentId){ return; } var itemIndex = app.findAgentIndex(eventItem.agentId); // 新的座席加入 if(itemIndex === -1){ eventQueue.push(eventItem); } // 更新已有座席的狀態 else{ eventQueue[itemIndex].agentStatus = eventItem.agentStatus; eventQueue[itemIndex].agentStatusTime = eventItem.agentStatusTime; eventQueue[itemIndex].eventName = eventItem.eventName; } }; return new App(); })();
打開控制檯,輸入tm.initWebsocket()後,websocket鏈接正常。
web
以後坐席狀態改變,能夠看到有事件推送過來。
bootstrap
看下整個頁面:
後端
最後,這個小小的監控若是用jQuery寫,也能夠,不過就是太坑了,每次都要去找到Dom元素,再更新DOM,用了Vue這類的框架,頁面的dom操做徹底不用關心了,真是太舒服了。\(^o^)/
瀏覽器
程序後服務端使用RabbitMQ
這裏我直接引用個人另外一個項目的部分代碼,這個沒有使用SockJS, 直接使用瀏覽器原生的WebSocket。
重連的原理很簡單,就是檢測到斷開時,去調用個人reconnectWs方法,這裏我也作了重連的次數限制。websocket
initWebSocket: function(callback, errorCallback) { callback = callback || function(){}; if(ws && ws.connected){ return; } Config.isManCloseWs = false; var url = Config.wsProtocol + Config.SDK + Config.eventPort + Config.eventBasePath + "/websocket"; if(typeof WebSocket != 'function'){ alert('您的瀏覽器版本太太太老了,請升級你的瀏覽器到IE11,或使用任何支持原生WebSocket的瀏覽器'); return; } try{ var socket = new WebSocket(url); } catch(e){ console.log(e); return; } var wsHeartbeatId = ''; ws = Stomp.over(socket); if(!Config.useWsLog){ ws.debug = null; } ws.connect({}, function(frame) { Config.currentReconnectTimes = 0; var dest = Config.newWsTopic + env.loginId.replace(/\./g,'_'); var lastEventSerial = ''; ws.subscribe(dest, function(event) { var eventInfo = {}; try{ eventInfo = JSON.parse(event.body); delete eventInfo.params; delete eventInfo._type; delete eventInfo.topics; } catch(e){ console.log(e); return; } if(lastEventSerial === eventInfo.serial){ util.error('Error: event repeat sent !'); return; } else{ lastEventSerial = eventInfo.serial; } if(Config.useEventLog){ util.debugout.log(' ' + JSON.stringify(eventInfo)); } eventHandler.deliverEvent(eventInfo); }); callback(); }, function(frame) { // websocket upexpected disconnected // maybe network disconnection, or browser in offline // this condition will emit wsDisconnected event if(Config.isManCloseWs){return;} errorCallback(); util.log(frame); util.error(new Date() + 'websocket disconnect'); // clearInterval(wsHeartbeatId); if(Config.currentReconnectTimes < Config.maxReconnectTimes){ Config.currentReconnectTimes++; util.reconnectWs(); } else{ var errorMsg = { eventName: 'wsDisconnected', msg: 'websocket disconnect' }; wellClient.ui.main({ eventName:'wsDisconnected' }); util.debugout.log('>>> websocket disconnect'); wellClient.triggerInnerOn(errorMsg); } }); }, reconnectWs: function(){ setTimeout(function(){ util.log('>>> try to reconnect'); util.debugout.log('>>> try to reconnect'); util.initWebSocket(function(){},function(){}); }, Config.timeout * 1000); },