webSocket.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import WebsocketHeartbeat from "./WebsocketHeartbeat";
  2. var server = "";
  3. if (process.env.NODE_ENV === 'development') {
  4. server = "ws://localhost:7018/imserver/r:";
  5. }else{
  6. server = "wss://im.yjf.runtzh.com/imserver/r:";
  7. //server = "ws://139.186.77.83:7018/imserver/r:";
  8. }
  9. export default {
  10. socket: {},
  11. hasLogin:false,
  12. initSocket: function(uid,reset) {
  13. if(typeof(WebSocket) === "undefined"){
  14. console.log("您的浏览器不支持socket")
  15. }else{
  16. // 实例化socket
  17. if(!this.socket || reset){
  18. console.log("实例化socket");
  19. //this.socket = new WebSocket(server+uid);
  20. this.socket=new WebsocketHeartbeat({
  21. url: server+uid,
  22. userId:'r:'+uid,
  23. pingTimeout: 15000,
  24. pongTimeout: 10000
  25. });
  26. let that=this;
  27. // this.socket.onopen = function () {
  28. // console.log('connect success');
  29. // console.log('send massage: test');
  30. // websocketHeartbeatJs.send('test');
  31. // setTimeout(() => {
  32. // console.log(`wait ${websocketHeartbeatJs.opts.pingTimeout} ms will hava '${websocketHeartbeatJs.opts.pingMsg}'`);
  33. // }, 1500);
  34. // }
  35. // that.$notify.success({
  36. // title: '正在呼叫'+mobile,
  37. // showClose: false
  38. // });
  39. this.hasLogin=true;
  40. }
  41. }
  42. }
  43. }