永城市12345 (县级市)- 来源于虞城县12345 - 所有县级统一API

chat.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. var uid = localStorage.getItem("uid");
  2. var ws, obj = {},
  3. lockReconnect = false;
  4. CreateWebSocket();
  5. function CreateWebSocket() {
  6. try {
  7. Connect();
  8. } catch(e) {
  9. ReConnect();
  10. }
  11. }
  12. //连接
  13. function Connect() {
  14. //ws = new WebSocket("ws://192.168.4.18:4006");
  15. //ws = new WebSocket("ws://192.168.1.21:2021");
  16. ws = new WebSocket("ws://117.158.196.116:4006");
  17. ws.onopen = function() {
  18. console.log(new Date() + " " + "建立连接");
  19. lockReconnect = false;
  20. obj.action = "cusconn";
  21. obj.code = uid;
  22. Send();
  23. };
  24. //接收到消息的回调方法
  25. ws.onmessage = function(evt) {
  26. console.log(new Date() + " receive " + evt.data);
  27. var data = JSON.parse(evt.data);
  28. if(data) {
  29. var type = data.action;
  30. switch(type.toLowerCase()) {
  31. case "cusconn":
  32. $(".main_message .chatlist_con").find("li").remove();
  33. if(data.state) {
  34. localStorage.setItem("uid", data.code);
  35. } else {
  36. // $('.notice').html('');
  37. notice("notice", data.msg);
  38. }
  39. if(data.code) {
  40. obj.action = "loglist";
  41. obj.code = data.code;
  42. obj.msg = "";
  43. Send();
  44. }
  45. break;
  46. //case "servicerson":
  47. // append("notice", data.msg);
  48. // break; //通知客服上线
  49. case "cussend":
  50. if(data.state) {
  51. append("self", data.msg,data.time);
  52. // document.getElementById("msg").value = "";
  53. $('.send_content').val('');
  54. } else {
  55. notice("notice", data.msg);
  56. // $('.notice').html(data.msg);
  57. }
  58. break
  59. case "cusreceive":
  60. append("left", data.msg,data.time)
  61. break; //收到消息
  62. case "loglist":
  63. var list = data.data.rows;
  64. $(list).each(function(i, n) {
  65. var classname = "left";
  66. if(n.F_FromUser == uid) {
  67. classname = "self";
  68. }
  69. insert(classname, n.F_Message, $(".hidnum").val());
  70. })
  71. if(!data.data.isfirst) {
  72. $(".more").css("display", "block");
  73. $(".hidnum").val(data.data.num);
  74. } else {
  75. $(".more").css("display", "none");
  76. }
  77. break; //聊天记录
  78. }
  79. }
  80. };
  81. //连接关闭的回调方法
  82. ws.onclose = function(evt) {
  83. console.log(new Date() + " " + "连接关闭");
  84. console.log(new Date() + " " + "开始重连");
  85. ReConnect();
  86. };
  87. //连接发生错误的回调方法
  88. ws.onerror = function(evt) {
  89. console.log(new Date() + " " + "连接关闭");
  90. console.log(new Date() + " " + "开始重连");
  91. ReConnect();
  92. };
  93. }
  94. //重连
  95. function ReConnect() {
  96. if(!lockReconnect) {
  97. lockReconnect = true;
  98. //没连接上会一直重连,设置延迟避免请求过多
  99. setTimeout(function() {
  100. console.log(new Date() + " " + "重连中……");
  101. lockReconnect = false;
  102. CreateWebSocket();
  103. }, 2000);
  104. }
  105. }
  106. //发送
  107. function Send() {
  108. if(ws.readyState != ws.OPEN) {
  109. ReConnect();
  110. }
  111. if(ws.readyState == ws.OPEN) {
  112. console.log(new Date() + " send " + JSON.stringify(obj));
  113. ws.send(JSON.stringify(obj));
  114. }
  115. }
  116. //查看更多
  117. function getmore() {
  118. obj.action = "loglist";
  119. obj.code = localStorage.getItem("uid");
  120. obj.msg = $(".hidnum").val();
  121. Send();
  122. }
  123. //发送消息
  124. function sendinfo() {
  125. // var msg = document.getElementById("msg").value;
  126. var msg = $('.send_content').val();
  127. if($.trim(msg)) {
  128. obj.action = "cussend";
  129. obj.msg = msg;
  130. Send();
  131. $(".error").text("");
  132. } else {
  133. $('.send_content').val('');
  134. $(".error").text("请输入内容");
  135. }
  136. }
  137. function append(cname, msg,dates) {
  138. var html='<li>' +
  139. '<p class="message_time"><span>' + dates + '</span></p>' +
  140. '<div class="message_mincon ' + cname + '">'
  141. if(cname=='self'){
  142. html +='<img class="ver_middle chat_yhtx" src="img/kefuTx.png" alt="" width="30" height="30" />'
  143. }else{
  144. html +='<img class="ver_middle" src="img/youkeTx.png" alt="" width="30" height="30"/>'
  145. }
  146. html +='<div class="message_text">' + msg + '</div>' +
  147. '</div>' +
  148. '</li>'
  149. $(html).appendTo($(".chatlist_con")[0]);
  150. console.log($(".chatlist_con")[0].scrollHeight);
  151. $(".chatlist_con").scrollTop($(".chatlist_con")[0].scrollHeight);
  152. }
  153. function insert(cname, msg, nodown) {
  154. var html='<li>' +
  155. // '<p class="message_time"><span>' + n.CreateDate + '</span></p>' +
  156. '<div class="message_mincon ' + cname + '">'
  157. if(cname=='self'){
  158. html +='<img class="ver_middle chat_yhtx" src="img/kefuTx.png" alt="" width="30" height="30" />'
  159. }else{
  160. html +='<img class="ver_middle" src="img/youkeTx.png" alt="" width="30" height="30"/>'
  161. }
  162. html +='<div class="message_text">' + msg + '</div>' +
  163. '</div>' +
  164. '</li>'
  165. var list=$(".chatlist_con").find("li");
  166. if (list.length) {
  167. $(html).insertBefore(list[0]);
  168. }
  169. else {
  170. $(html).appendTo($(".chatlist_con")[0]);
  171. }
  172. if (!nodown) {
  173. $(".chatlist_con").scrollTop($(".chatlist_con")[0].scrollHeight);
  174. }
  175. }
  176. //键盘发送事件
  177. $(document).keydown(function(event) {
  178. event = event || window.event;
  179. if($(".send_content").is(":focus")==true){
  180. if(event.keyCode == 13) {
  181. $(".sendTo").click();
  182. event.returnValue = false;
  183. return false
  184. }
  185. }
  186. });
  187. function notice(cname, msg){
  188. var html = '<li><span class="' + cname + '">' + msg + '</span></li>';
  189. $(html).appendTo($(".chatlist_con")[0]);
  190. $(".chatlist_con").scrollTop($(".chatlist_con")[0].scrollHeight);
  191. }