Socket.js 955 B

1234567891011121314151617181920212223242526272829303132333435
  1. class Socket {
  2. constructor({
  3. socket
  4. }) {
  5. this.baseUrl = socket;
  6. this.socketEstablish = false; //是否建立链接
  7. this.socketCallback = () => {
  8. };
  9. }
  10. initSocket() {
  11. this.SocketTask = uni.connectSocket({
  12. url: this.baseUrl + '/waserver/webSocket/' + uni.getStorageSync('userMsg').token,
  13. complete: (res) => {
  14. console.log(res)
  15. }
  16. })
  17. this.SocketTask.onOpen(function (res) {
  18. this.socketEstablish = true;
  19. })
  20. this.SocketTask.onMessage(function (res) {
  21. // this.socketCallback(res)
  22. this.$Http.updateMessage && this.$Http.updateMessage()
  23. })
  24. this.SocketTask.onError(function (res) {
  25. this.socketEstablish = false;
  26. })
  27. this.SocketTask.onClose(function (res) {
  28. this.socketEstablish = false;
  29. })
  30. }
  31. }
  32. export {
  33. Socket
  34. }