WebSocket.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. (function (global) {
  2. var socketDebug = window.socketDebug == undefined ? false : window.socketDebug, port = window.workermanConfig === undefined ? '20005' : window.workermanConfig.port;
  3. window.uid = window.uids === undefined ? 0 : window.uids;
  4. window.room = window.room === undefined ? 0 : window.room;
  5. var send=0;
  6. var socket = {
  7. ws: null,
  8. connect: function () {
  9. var that = this;
  10. that.ws = new WebSocket("ws://" + document.domain + ":" + port);//这里如果使用127.0.0.1或者localhost会出现连接失败。当时为了方便以后的维护,这里在php的全局文件里定义了一个常量来定义ip,后来本地开发完提交到linux服务器环境之后发现链接失败!按照此行代码会有效连接~
  11. that.ws.onopen = this.onopen;
  12. that.ws.onmessage = this.onmessage;
  13. that.ws.onclose = function (e) {
  14. socketDebug && console.log("连接关闭,定时重连");
  15. that.connect();
  16. };
  17. that.ws.onerror = function (e) {
  18. socketDebug && console.log("出现错误");
  19. };
  20. },
  21. onopen: function () {
  22. var joint = '{"type":"handshake","role":"user","uid":' + window.uid + ',"room":' + window.room + '}';
  23. socket.ws.send(joint);
  24. socket.heartCheck.start();
  25. },
  26. sendMsg: function (content, type, id) {
  27. socket.ws.send("{content:'" + content + "',m_type:'" + type + "',room:" + id + ",type:'send',uid:" + window.uid + "}")
  28. },
  29. reconnection:function(){
  30. setTimeout(function () {
  31. if(send){socket.ws.close();}
  32. }, 3000);
  33. },
  34. sendOut:function(){
  35. send=1;
  36. socket.reconnection();
  37. },
  38. onmessage: function (e) {
  39. try {
  40. var data = JSON.parse(e.data);
  41. if(data){send=0;}
  42. socketDebug && console.log(data);
  43. switch (data.type) {
  44. case 'init':
  45. break;
  46. // 服务端ping客户端
  47. case 'ping':
  48. break;
  49. // 登录 更新用户列表
  50. case 'handshake':
  51. break;
  52. // 提醒
  53. case 'reception':
  54. break;
  55. //直播进行中
  56. case 'live_ing':
  57. vm.changLive(true, data.pull_url);
  58. break;
  59. //直播结束
  60. case 'live_end':
  61. vm.changLive(false);
  62. break;
  63. //消息提醒
  64. case 'message':
  65. vm.setCommentArea(data.message, data.m_type, data.userInfo, data.user_type, data.id);
  66. break;
  67. //消息撤回
  68. case 'recall':
  69. vm.CommentRecall(data.id);
  70. break;
  71. case 'ban':
  72. vm.setBanUser(data.value);
  73. break;
  74. case "room_user_count":
  75. vm.setUserCount(data.onLine_user_count, data.notice_content, data.user_type);
  76. break;
  77. // 打赏
  78. case "live_reward":
  79. vm.setGiftFloat(data);
  80. break;
  81. }
  82. } catch (e) {
  83. socketDebug && console.info(e);
  84. }
  85. },
  86. heartCheck: {
  87. timeout: 3000,
  88. timeoutObj: null,
  89. start: function () {
  90. this.timeoutObj = setInterval(function () {
  91. socket.ws.send("{'type':'ping'}");
  92. }, this.timeout);
  93. },
  94. },
  95. };
  96. socket.connect();
  97. global.socket = socket;
  98. return socket
  99. }(this));