WebSocket.js 4.2 KB

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