首頁 > web前端 > js教程 > JS實作websocket即時訊息提示

JS實作websocket即時訊息提示

小云云
發布: 2018-05-17 16:22:33
原創
3556 人瀏覽過

本文主要介紹了JS實作websocket長輪詢即時訊息提示的效果的相關資料,需要的朋友可以參考下,希望能幫助到大家。

效果圖如下:

參考程式碼如下:

jsp程式碼:

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<p class="page-header navbar navbar-fixed-top">
  <p class="page-header-inner">
    <p 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>
      <p class="menu-toggler sidebar-toggler hide"></p>
    </p>
    <a href="javascript:;" rel="external nofollow" rel="external nofollow" class="menu-toggler responsive-toggler"
      data-toggle="collapse" data-target=".navbar-collapse"></a>
    <p 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>
    </p>
  </p>
</p>
<p class="clearfix"></p>
<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(&#39;/&#39;) + 1);
    return (host + projectName);
  }
  wsPath = wsPath();
  var websocket = null;
  if (&#39;WebSocket&#39; in window) {
    websocket = new WebSocket("ws://" + wsPath + "/websocket/socketServer");
  } else if (&#39;MozWebSocket&#39; 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 = $(&#39;li.dropdown-alert&#39;);
    var $uncheckedAlertList = $(&#39;ul&#39;, $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(&#39;<li class="place-alert"><a href="alert?menuId=274" rel="external nofollow" rel="external nofollow" > <font color="red" id="place-alert">&#39;
                + "位置报警(<font id =&#39;number&#39;>"
                + count
                + "</font>)" + &#39;</font></a></li>&#39;);
      } else {
        count = Number(a) + 1;
        $("#place-alert").html(
            "位置偏移(<font id=&#39;number&#39;>" + count + "</font>)");
      }
    }
    if (evt.data == "0") {
      var count;
      var a = $("#snum").html();
      if (a == null) {
        count = 1;
        $uncheckedAlertList
            .prepend(&#39;<li class="sos-alert"> <a href="alert?menuId=274" rel="external nofollow" rel="external nofollow" ><font color="red" id="sos-alert">&#39;
                + "SOS报警(<font id=&#39;snum&#39;>"
                + count
                + ")</font>"
                + &#39;</font></a></li>&#39;);
      } else {
        count = Number(a) + 1;
        $("#sos-alert").html(
            "SOS报警(<font id=&#39;snum&#39;>" + count + "</font>)");
      }
    }
  }
  function onError() {
    websocket.close();
  }
  function onClose() {
  }
  window.close = function() {
    websocket.onclose();
  }
</script>
登入後複製

相關推薦:

WebSocket詳細介紹

#php中關於websocket的詳細介紹

#

以上是JS實作websocket即時訊息提示的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板