欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS实现websocket长轮询实时消息提示的效果

程序员文章站 2022-06-17 17:00:31
效果图如下: 参考代码如下: jsp代码: <%@ page contenttype="text/html;charset=utf-8" langu...

效果图如下:

JS实现websocket长轮询实时消息提示的效果

参考代码如下:

jsp代码:

<%@ page contenttype="text/html;charset=utf-8" language="java"%>
<div class="page-header navbar navbar-fixed-top">
  <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="top-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.com/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript"
  src="http://cdn.bootcss.com/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长轮询实时消息提示的效果,希望对大家有所帮助