ws.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import bus from "./bus";
  2. import { Message } from "element-ui";
  3. export default class wsService {
  4. constructor() {
  5. // const wsUrl = ref("ws://172.16.50.3/websocket");
  6. // this.address = "ws://172.16.50.3/websocket";
  7. this.address = "ws://106.14.237.165/websocket";
  8. this.promisePool = {};
  9. this._websocket = undefined;
  10. this.wsClosed = false;
  11. this.latestLog = "";
  12. this.latestMessage = "";
  13. }
  14. open() {
  15. return new Promise((resolve, reject) => {
  16. console.log(this.address);
  17. if (typeof this._websocket === "undefined") {
  18. this._websocket = new WebSocket(this.address);
  19. this._websocket.onopen = e => {
  20. this.wsClosed = false;
  21. resolve({ e, ws: this });
  22. };
  23. this._websocket.onerror = e => {
  24. reject(e);
  25. };
  26. }
  27. this._websocket.onclose = e => {
  28. reject(e);
  29. this.wsClosed = true;
  30. // bus.emit("websocket-onclose", e);
  31. };
  32. this._websocket.onmessage = res => {
  33. if (res.data) {
  34. let msg = JSON.parse(res.data);
  35. let [token, action, status, bench_id, data] = [
  36. msg.msg_id,
  37. msg.method,
  38. msg.status,
  39. msg.bench_id,
  40. msg.data
  41. ];
  42. // console.log(action != "play_audio");
  43. // if (action != "get_image" && action != "play_audio" && action != "collect_audio") {
  44. // console.log("onmessage", action);
  45. // }
  46. // handle message subscribtion
  47. if (action == "notify_im") {
  48. if (status === "WS_OK") {
  49. bus.emit("newMessage", data);
  50. }
  51. } else if (action == "notify_bench_status") {
  52. // console.log('notify_bench_status', data);
  53. // handle log subscribtion
  54. if (status === "WS_OK") {
  55. bus.emit("newLog", JSON.parse(data));
  56. }
  57. } else {
  58. const req = this.promisePool[token];
  59. if (status === "WS_OK") {
  60. if (action == "collect_audio") {
  61. bus.emit("sendAudio" + bench_id, { data, bench_id });
  62. // bus.emit("sendAudio", { data, bench_id });
  63. }
  64. if (this.promisePool[token]) {
  65. let benchId = this.promisePool[token][
  66. "bench_id"
  67. ];
  68. req.resolve({ data: data, benchId });
  69. delete this.promisePool[token];
  70. }
  71. } else {
  72. let err = { action, data: JSON.stringify(data) };
  73. console.log(err);
  74. console.log(err.data);
  75. Message({
  76. message: err.data,
  77. // message: "",
  78. type: "error",
  79. });
  80. // req.reject(err);
  81. }
  82. }
  83. }
  84. };
  85. });
  86. }
  87. close() {
  88. this._websocket.close();
  89. }
  90. send(method, params) {
  91. if (!method) {
  92. console.error("no method assigned");
  93. } else {
  94. let bench_id = params.bench_id;
  95. let msg_id = this.genMsgId();
  96. let msgObj = {
  97. msg_id: msg_id,
  98. method: method,
  99. ...params
  100. };
  101. let msg = JSON.stringify(msgObj);
  102. // if (!(method == "get_image" || method == "play_audio")) {
  103. // console.log(`< ${method} > ${msg}`);
  104. // }
  105. return new Promise((resolve, reject) => {
  106. this.promisePool[msg_id] = {
  107. bench_id,
  108. resolve,
  109. reject
  110. };
  111. if (this._websocket.readyState == 1) {
  112. // if (!this.wsClosed) {
  113. this._websocket.send(msg);
  114. }
  115. // setTimeout(() => {
  116. // console.log('this._websocket.readyState2', this._websocket.readyState);
  117. // }, 100)
  118. });
  119. }
  120. }
  121. genMsgId() {
  122. let rand = Math.random()
  123. .toString(36)
  124. .substr(2);
  125. return "request_" + rand;
  126. }
  127. connectBench(connectParam) {
  128. let res = this.send("connect_bench", connectParam);
  129. return res;
  130. }
  131. disconnectBench(connectParam) {
  132. let res = this.send("disconnect_bench", connectParam);
  133. return res;
  134. }
  135. getImage(id, screenType) {
  136. return this.send("get_image", {
  137. bench_id: id,
  138. screen_type: screenType
  139. });
  140. }
  141. singleFingerTouch(id, x, y, screenType = 1, debug = false) {
  142. return this.send("single_finger_touch", {
  143. bench_id:id,
  144. x: x,
  145. y: y,
  146. screen_type: screenType,
  147. debug: debug
  148. });
  149. }
  150. singleFingerDrag(id, x1, y1, x2, y2, screenType = 1, debug = false) {
  151. return this.send("single_finger_drag", {
  152. bench_id: id,
  153. x1: x1,
  154. y1: y1,
  155. x2: x2,
  156. y2: y2,
  157. screen_type: screenType,
  158. debug: debug
  159. });
  160. }
  161. singleFingerLongTouch(id, x, y, duration, screenType = 1, debug = false) {
  162. return this.send("single_finger_long_touch", {
  163. bench_id: id,
  164. x: x,
  165. y: y,
  166. duration: duration,
  167. screen_type: screenType,
  168. debug: debug
  169. });
  170. }
  171. subscribeBenchStatus(id) {
  172. return this.send("subscribe_bench_status", id);
  173. }
  174. unsubscribeBenchStatus(id) {
  175. return this.send("unsubscribe_bench_status", id);
  176. }
  177. planExecute(id) {
  178. return this.send("plan-execute", {
  179. bench_id: id,
  180. });
  181. }
  182. play_audio(data) {
  183. return this.send("play_audio", {
  184. bench_id: "",
  185. bench_ids: data.benchIds,
  186. data: data.data
  187. });
  188. }
  189. stop_play_audio(data) {
  190. return this.send("stop_play_audio", {
  191. bench_id: data.benchId,
  192. });
  193. }
  194. collect_audio(data) {
  195. return this.send("collect_audio", {
  196. bench_id: data.benchId,
  197. });
  198. }
  199. stop_collect_audio(data) {
  200. return this.send("stop_collect_audio", {
  201. bench_id: data.benchId,
  202. });
  203. }
  204. press_ptt(data) {
  205. return this.send("press_ptt", {
  206. bench_id: data.benchId,
  207. });
  208. }
  209. connect_audio_device(data) {
  210. return this.send("connect_audio_device", {
  211. bench_id: data.benchId,
  212. });
  213. }
  214. }