js如何实现websocket实例
这篇文章主要介绍js如何实现websocket实例,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
福建网站建设公司创新互联公司,福建网站设计制作,有大型网站制作公司丰富经验。已为福建成百上千家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的福建做网站的公司定做!
socket_notify.js
var ws_text = document.location.protocol == 'https:' ? 'wss' : 'ws'; websocket_url = ws_text + '://' + window.location.host + '/socket.io/'; var socket = new WebSocket(websocket_url); //连接成功时触发 socket.onopen = function() { console.log('connected to server!'); // 登录 socket.send(JSON.stringify({ type: 'login', uid: admin_userid, })); setInterval(function() { console.log('Hello!'); socket.send('Hello!'); }, 30000) }; var socket_func = {}; //监听收到的消息 socket.onmessage = function(res) { let Sound = false; let alerttitle = false; let alerttext = false; let alertlocal = false; let content = ''; let func = ''; var data = JSON.parse(res.data); console.log(data); var type = data.type; try { content = JSON.parse(data.data); } catch (err) { content = data.data; } const alert_check = data.alert; switch (type) { case 'newCpOrder': func = type; Sound = 1; alerttitle = '新公司入款订单'; alerttext = '订单ID:' + content.id; alertlocal = '/cp_recharge/index.html'; break; case 'newAgentOrder': func = 'newTxOrder'; Sound = 2; alerttitle = '新代理出款订单'; alerttext = '订单ID:' + content.id; alertlocal = '/exchange/index.html'; break; } if (alert_check) { if ((!socket_type[type] || socket_type[type]['sound'] === 1) && Sound !== false) { playSound(Sound); } if ((!socket_type[type] || socket_type[type]['text'] === 1) && alerttitle !== false) { notify(alerttitle, alerttext, alertlocal); } } // 执行方法 if (func) { try { socket_func[func](content); } catch (err) { console.log('没有当前方法' + func); } } }; // 断开 socket.onclose = function(e) { console.log('websocket 断开: ' + e.code + ' ' + e.reason + ' ' + e.wasClean) console.log(e); } var Notification = window.Notification || window.webkitNotification || window.mozNotification; var stopSound = function() {}; // 播放声音 var playSound = function(type) { var audio_id = 'recharge_audio'; switch (type) { case 1: audio_id = 'recharge_audio'; break; case 2: audio_id = 'withdraw_audio'; break; case 3: audio_id = 'withdraw_audio'; break; case 4: audio_id = 'untreated_recharge'; break; case 5: audio_id = 'untreated_withdraw'; break; case 6: audio_id = 'large_recharge'; break; default: } var audio = document.getElementById(audio_id); audio.currentTime = 0; audio.play(); }; var notify = function() {}; // web 页面通知 if (Notification) { Notification.requestPermission().then(function(permission) { if (permission === 'granted') { notify = function(title, content, url) { var options = { body: content, }; var instance = new Notification(title, options); instance.onshow = function() { setTimeout(function() { instance.close(); }, 18000); }; instance.onclick = function() { window.open(url); }; }; window.notify = notify; } }); }
Buy me a cup of coffee :)
以上是“js如何实现websocket实例”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
网页名称:js如何实现websocket实例
新闻来源:http://cdiso.cn/article/joshsh.html