Socket.weapp.js 955 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. class SocketWeapp {
  7. constructor(serverURL) {
  8. this.onopen = () => {};
  9. this.onmessage = () => {};
  10. this.onclose = () => {};
  11. this.onerror = () => {};
  12. // @ts-ignore
  13. wx.onSocketOpen(() => {
  14. this.onopen();
  15. });
  16. // @ts-ignore
  17. wx.onSocketMessage(msg => {
  18. // @ts-ignore
  19. this.onmessage(msg);
  20. });
  21. // @ts-ignore
  22. wx.onSocketClose(event => {
  23. // @ts-ignore
  24. this.onclose(event);
  25. });
  26. // @ts-ignore
  27. wx.onSocketError(error => {
  28. // @ts-ignore
  29. this.onerror(error);
  30. });
  31. // @ts-ignore
  32. wx.connectSocket({
  33. url: serverURL
  34. });
  35. }
  36. send(data) {
  37. // @ts-ignore
  38. wx.sendSocketMessage({
  39. data
  40. });
  41. }
  42. close() {
  43. // @ts-ignore
  44. wx.closeSocket();
  45. }
  46. }
  47. module.exports = SocketWeapp;
  48. var _default = exports.default = SocketWeapp;