:::danger 该控件目前未全部完成 :::

    1. /* eslint-disable no-undef */
    2. // eslint-disable-next-line @typescript-eslint/no-var-requires
    3. const websocket = require('websocket');
    4. // 控件类型定义
    5. const types = {
    6. type: 'SAMPLE_WEBSOCKET_WIDGET',
    7. icon: 'https://static.codemao.cn/appcraft/tmp-extension-widgets/test/websocket.svg',
    8. title: '长连接',
    9. platforms: ['android', 'ios'],
    10. properties: [
    11. {
    12. key: 'url',
    13. label: '链接',
    14. valueType: 'string',
    15. defaultValue: '',
    16. },
    17. {
    18. key: 'timeout',
    19. label: '超时毫秒数',
    20. valueType: 'number',
    21. defaultValue: 5000,
    22. },
    23. ],
    24. methods: [
    25. {
    26. key: 'setHeader',
    27. label: '设置请求头',
    28. params: [
    29. {
    30. key: 'key',
    31. label: '请求头字段',
    32. valueType: 'string',
    33. defaultValue: '',
    34. },
    35. {
    36. key: 'value',
    37. label: '值',
    38. valueType: 'string',
    39. defaultValue: '',
    40. },
    41. ],
    42. },
    43. {
    44. key: 'deleteHeader',
    45. label: '清除定时任务',
    46. params: [
    47. {
    48. key: 'key',
    49. label: '请求头字段',
    50. valueType: 'string',
    51. defaultValue: '',
    52. },
    53. ],
    54. },
    55. {
    56. key: 'connect',
    57. label: '开始连接',
    58. params: [],
    59. },
    60. {
    61. key: 'send',
    62. label: '发送消息',
    63. params: [
    64. {
    65. key: 'msg',
    66. label: '',
    67. valueType: 'string',
    68. defaultValue: 'Hello',
    69. },
    70. ],
    71. },
    72. {
    73. key: 'close',
    74. label: '中断连接',
    75. params: [],
    76. },
    77. ],
    78. events: [
    79. {
    80. key: 'onConnect',
    81. label: '连接成功',
    82. params: [],
    83. },
    84. {
    85. key: 'onMessage',
    86. label: '收到消息',
    87. params: [
    88. {
    89. key: 'msg',
    90. label: '消息',
    91. valueType: 'string',
    92. },
    93. ],
    94. },
    95. {
    96. key: 'onError',
    97. label: '发生错误',
    98. params: [
    99. {
    100. key: 'error',
    101. label: '错误信息文本',
    102. valueType: 'string',
    103. },
    104. ],
    105. },
    106. ],
    107. };
    108. // 控件实体定义
    109. class Widget extends InvisibleWidget {
    110. constructor(props) {
    111. super(props);
    112. this.url = props.url;
    113. this.timeout = props.timeout;
    114. this.headers = {};
    115. this.webSocketId = null;
    116. }
    117. setHeader(key, value) {
    118. console.log('setHeader', key, value);
    119. this.headers[key] = String(value);
    120. }
    121. deleteHeader(key) {
    122. if (Object.prototype.hasOwnProperty.call(this.headers, key)) {
    123. delete this.headers[key];
    124. }
    125. }
    126. connect() {
    127. console.log('connect');
    128. const options = {
    129. url: this.url,
    130. timeout: this.timeout,
    131. headers: this.headers,
    132. };
    133. websocket.wsConnect(
    134. options,
    135. (data) => {
    136. console.log('onConnect', data);
    137. this.webSocketId = data.webSocketId;
    138. this.emit('onConnect');
    139. },
    140. (data) => {
    141. console.log('onMessage', data);
    142. this.emit('onMessage', data.message);
    143. },
    144. () => {
    145. this.emit('onError');
    146. }
    147. );
    148. }
    149. send(msg) {
    150. if (!this.webSocketId) {
    151. this.emit('onError', '连接未建立');
    152. return;
    153. }
    154. websocket.wsSend(this.webSocketId, msg);
    155. }
    156. close() {
    157. if (this.webSocketId) {
    158. websocket.wsClose(this.webSocketId);
    159. }
    160. }
    161. }
    162. exports.types = types;
    163. exports.widget = Widget;