onlineUsers.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 
  2. var socket, limitCount = 5;
  3. if (!window.WebSocket) {
  4. window.WebSocket = window.MozWebSocket;
  5. }
  6. if (window.WebSocket) {
  7. socket = new WebSocket("ws://192.168.3.96:8088/glwork/onlinewebsocket/" + $.cookie("GlWorkPlatform-userid"));
  8. socket.onmessage = function (event) {
  9. $("#onlineUsers").html(event.data + "人");
  10. $.cookie("GlWorkPlatform-onlineUsers", event.data);
  11. console.log(event)
  12. };
  13. socket.onopen = function (event) {
  14. console.log(event)
  15. };
  16. socket.onclose = function (event) {
  17. console.log(event)
  18. if ($.cookie("GlWorkPlatform-onlineUsers") != null) {
  19. reconnet()
  20. //socket = new WebSocket("ws://192.168.3.96:8088/glwork/onlinewebsocket/" + $.cookie("GlWorkPlatform-userid"));
  21. } else {
  22. console.log("服务器已断开连接!")
  23. }
  24. };
  25. setInterval(function () {
  26. socket.send("")
  27. }, 1000 * 55)
  28. }
  29. else {
  30. sp.dialog("抱歉,您的浏览器不支持WebSocket协议!");
  31. }
  32. function reconnet(){
  33. if (limitCount > 0) {
  34. console.log("第" + (6 - limitCount) + "次重连!");
  35. limitCount--
  36. socket = new WebSocket("ws://192.168.3.96:8088/glwork/onlinewebsocket/" + $.cookie("GlWorkPlatform-userid"));
  37. } else {
  38. console.log("重连失败,请重新登录!")
  39. }
  40. }