JS实现websocket长轮询实时消息提示的效果
网络编程 2021-07-04 17:32www.168986.cn编程入门
这篇文章主要介绍了JS实现websocket长轮询实时消息提示的效果的相关资料,需要的朋友可以参考下
效果图如下
参考代码如下
jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java"%> <div class="page-header navbar navbar-fixed-"> <div class="page-header-inner"> <div class="page-logo"> <a href="<c:url value=" rel="external nofollow" rel="external nofollow" /"/>"><img src="<c:url value="/img/logo.png"/>" style="height: 14px" alt="logo" class="logo-default" /></a> <div class="menu-toggler sidebar-toggler hide"></div> </div> <a href="javascript:;" rel="external nofollow" rel="external nofollow" class="menu-toggler responsive-toggler" data-toggle="collapse" data-target=".navbar-collapse"></a> <div class="-menu"> <ul class="nav navbar-nav pull-right"> <li class="dropdown dropdown-alert"><a href="#" rel="external nofollow" rel="external nofollow" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-close-others="true"> <span class="badge pull-left"></span><label class="hidden-sm">报警</label><i class="fa fa-bell"></i> </a> <ul class="dropdown-menu"> </ul></li> <li class="dropdown dropdown-user"><a href="#" rel="external nofollow" rel="external nofollow" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-close-others="true"> <span class="username username-hide-on-mobile">你好,${sessionScope.username}</span> <i class="fa fa-angle-down"></i> </a> <ul class="dropdown-menu"> <li><a href="javascript:;" rel="external nofollow" rel="external nofollow" id="updatePass"><i class="icon-lock"></i>修改密码</a></li> <li><a href="<c:url value=" rel="external nofollow" rel="external nofollow" /logout"/> "><i class="icon-key"></i>退出登录</a></li> </ul></li> </ul> </div> </div> </div> <div class="clearfix"></div> <script> //toastr.sos(num1) </script> <script type="text/javascript" src="http://cdn.bootcss./jquery/3.1.0/jquery.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss./sockjs-client/1.1.1/sockjs.js"></script> <script type="text/javascript"> function wsPath() { var pathName = window.document.location.pathname; var host = window.location.host; var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1); return (host + projectName); } wsPath = wsPath(); var websocket = null; if ('WebSocket' in window) { websocket = new WebSocket("ws://" + wsPath + "/websocket/socketServer"); } else if ('MozWebSocket' in window) { websocket = new MozWebSocket("ws://" + wsPath + "/bison/websocket/socketServer"); } else { websocket = new SockJS("http://" + wsPath + "/bison/sockjs/socketServer"); } websocket.onmessage = onMessage; websocket.onope = onOpen; websocket.onerror = onError; websocket.onclose = onClose; function onOpen() { } function onMessage(evt) { var $uncheckedAlertMenuBtn = $("a.dropdown-toggle", $uncheckedAlertMenu); var $uncheckedAlertBadge = $("span.badge", $uncheckedAlertMenuBtn); var $uncheckedAlertMenu = $('li.dropdown-alert'); var $uncheckedAlertList = $('ul', $uncheckedAlertMenu); var a = $uncheckedAlertBadge.html(); $uncheckedAlertBadge.html(Number(a) + 1); //判断报警类型 如果是位置偏移,place+1 if (evt.data == "1") { var count; var a = $("#number").html(); if (a == null) { count = 1; $uncheckedAlertList .prepend('<li class="place-alert"><a href="alert?menuId=274" rel="external nofollow" rel="external nofollow" > <font color="red" id="place-alert">' + "位置报警(<font id ='number'>" + count + "</font>)" + '</font></a></li>'); } else { count = Number(a) + 1; $("#place-alert").html( "位置偏移(<font id='number'>" + count + "</font>)"); } } if (evt.data == "0") { var count; var a = $("#snum").html(); if (a == null) { count = 1; $uncheckedAlertList .prepend('<li class="sos-alert"> <a href="alert?menuId=274" rel="external nofollow" rel="external nofollow" ><font color="red" id="sos-alert">' + "SOS报警(<font id='snum'>" + count + ")</font>" + '</font></a></li>'); } else { count = Number(a) + 1; $("#sos-alert").html( "SOS报警(<font id='snum'>" + count + "</font>)"); } } } function onError() { websocket.close(); } function onClose() { } window.close = function() { websocket.onclose(); } </script>
以上所述是长沙网络推广给大家介绍的JS实现websocket长轮询实时消息提示的效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程