您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍js如何实现websocket实例,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
<!-- websocket 接口 --><script type="text/javascript">var websocket_url = 'ws://127.0.0.1:' + "{$Think.config.prompt_service.ws_port}";var admin_userid = "{$Think.const.UID}";var socket_type = JSON.parse('{$mginfo.socket_type|default=[]|json_encode}');</script><script src="__JS__/socket_notify.js?v=20191018"></script>
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实例”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。