Socket.js 885 B

12345678910111213141516171819202122232425262728293031323334
  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. })
  23. this.SocketTask.onError(function (res) {
  24. this.socketEstablish = false;
  25. })
  26. this.SocketTask.onClose(function (res) {
  27. this.socketEstablish = false;
  28. })
  29. }
  30. }
  31. export {
  32. Socket
  33. }