Socket.weapp.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  4. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  5. module.exports =
  6. /*#__PURE__*/
  7. function () {
  8. function SocketWeapp(serverURL) {
  9. var _this = this;
  10. (0, _classCallCheck2.default)(this, SocketWeapp);
  11. this.onopen = function () {};
  12. this.onmessage = function () {};
  13. this.onclose = function () {};
  14. this.onerror = function () {};
  15. wx.connectSocket({
  16. url: serverURL
  17. });
  18. wx.onSocketOpen(function () {
  19. _this.onopen();
  20. });
  21. wx.onSocketMessage(function (msg) {
  22. _this.onmessage(msg);
  23. });
  24. wx.onSocketClose(function () {
  25. _this.onclose();
  26. });
  27. wx.onSocketError(function (error) {
  28. _this.onerror(error);
  29. });
  30. }
  31. (0, _createClass2.default)(SocketWeapp, [{
  32. key: "send",
  33. value: function (data) {
  34. wx.sendSocketMessage({
  35. data: data
  36. });
  37. }
  38. }, {
  39. key: "close",
  40. value: function () {
  41. wx.closeSocket();
  42. }
  43. }]);
  44. return SocketWeapp;
  45. }();