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

这篇文章将为大家详细讲解有关JS怎么实现websocket长轮询实时消息提示的效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联专注于企业营销型网站建设、网站重做改版、稷山网站定制设计、自适应品牌网站建设、H5页面制作商城网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为稷山等各大城市提供网站开发制作服务。

js的作用是什么

1、能够嵌入动态文本于HTML页面。2、对浏览器事件做出响应。3、读写HTML元素。4、在数据被提交到服务器之前验证数据。5、检测访客的浏览器信息。6、控制cookies,包括创建和修改等。7、基于Node.js技术进行服务器端编程。

效果图如下:

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

参考代码如下:

jsp代码:

<%@ page contentType="text/html;charset=UTF-8" language="java"%>

  
    
      ">"  alt="logo"
        class="logo-default" />
      
    
                          报警                                         你好,${sessionScope.username}                                              
  • 修改密码
  •             
  •  ">退出登录
  •                       
      
      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(' '                 + "位置报警("                 + count                 + ")" + '');       } else {         count = Number(a) + 1;         $("#place-alert").html(             "位置偏移(" + count + ")");       }     }     if (evt.data == "0") {       var count;       var a = $("#snum").html();       if (a == null) {         count = 1;         $uncheckedAlertList             .prepend(' '                 + "SOS报警("                 + count                 + ")"                 + '');       } else {         count = Number(a) + 1;         $("#sos-alert").html(             "SOS报警(" + count + ")");       }     }   }   function onError() {     websocket.close();   }   function onClose() {   }   window.close = function() {     websocket.onclose();   }

    关于“JS怎么实现websocket长轮询实时消息提示的效果”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


    网站题目:JS怎么实现websocket长轮询实时消息提示的效果
    链接分享:http://pwwzsj.com/article/pgchjd.html