Ipc.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <div id="app-base-subwindow-ipc">
  3. <div class="one-block-1">
  4. <span>
  5. 1. 发送异步消息
  6. </span>
  7. </div>
  8. <div class="one-block-2">
  9. <a-space>
  10. <a-button @click="handleInvoke">发送 - 回调</a-button>
  11. 结果:{{ message1 }}
  12. </a-space>
  13. <p></p>
  14. <a-space>
  15. <a-button @click="handleInvoke2">发送 - async/await</a-button>
  16. 结果:{{ message2 }}
  17. </a-space>
  18. </div>
  19. <div class="one-block-1">
  20. <span>
  21. <!-- 尽量不要使用,任何错误都容易引起卡死 -->
  22. 2. 同步消息(不推荐,阻塞执行)
  23. </span>
  24. </div>
  25. <div class="one-block-2">
  26. <a-space>
  27. <a-button @click="handleSendSync">同步消息</a-button>
  28. 结果:{{ message3 }}
  29. </a-space>
  30. </div>
  31. <div class="one-block-1">
  32. <span>
  33. 3. 长消息: 服务端持续向前端页面发消息
  34. </span>
  35. </div>
  36. <div class="one-block-2">
  37. <a-space>
  38. <a-button @click="sendMsgStart">开始</a-button>
  39. <a-button @click="sendMsgStop">结束</a-button>
  40. 结果:{{ messageString }}
  41. </a-space>
  42. </div>
  43. <div class="one-block-1">
  44. <span>
  45. 4. 多窗口通信:窗口之间互相通信
  46. </span>
  47. </div>
  48. <div class="one-block-2">
  49. <a-space>
  50. <a-button @click="sendTosubWindow()">向主窗口发消息</a-button>
  51. </a-space>
  52. </div>
  53. </div>
  54. </template>
  55. <script>
  56. import { ipcApiRoute, specialIpcRoute } from '@/api/main'
  57. export default {
  58. data() {
  59. return {
  60. messageString: '',
  61. message1: '',
  62. message2: '',
  63. message3: '',
  64. mainWCid: 0,
  65. }
  66. },
  67. mounted () {
  68. this.init();
  69. },
  70. methods: {
  71. init () {
  72. const self = this;
  73. // 避免重复监听,或者将 on 功能写到一个统一的地方,只加载一次
  74. this.$ipc.removeAllListeners(ipcApiRoute.ipcSendMsg);
  75. this.$ipc.on(ipcApiRoute.ipcSendMsg, (event, result) => {
  76. console.log('[ipcRenderer] [socketMsgStart] result:', result);
  77. self.messageString = result;
  78. // 调用后端的另一个接口
  79. event.sender.send(ipcApiRoute.hello, 'electron-egg');
  80. })
  81. // 监听主窗口发来的消息
  82. this.$ipc.removeAllListeners(specialIpcRoute.window1ToWindow2);
  83. this.$ipc.on(specialIpcRoute.window1ToWindow2, (event, arg) => {
  84. this.$message.info(arg);
  85. })
  86. },
  87. sendMsgStart() {
  88. const params = {
  89. type: 'start',
  90. content: '开始'
  91. }
  92. this.$ipc.send(ipcApiRoute.ipcSendMsg, params)
  93. },
  94. sendMsgStop() {
  95. const params = {
  96. type: 'end',
  97. content: ''
  98. }
  99. this.$ipc.send(ipcApiRoute.ipcSendMsg, params)
  100. },
  101. handleInvoke () {
  102. const self = this;
  103. this.$ipc.invoke(ipcApiRoute.ipcInvokeMsg, '异步-回调').then(r => {
  104. console.log('r:', r);
  105. self.message1 = r;
  106. });
  107. },
  108. async handleInvoke2 () {
  109. const msg = await this.$ipc.invoke(ipcApiRoute.ipcInvokeMsg, '异步');
  110. console.log('msg:', msg);
  111. this.message2 = msg;
  112. },
  113. handleSendSync () {
  114. const msg = this.$ipc.sendSync(ipcApiRoute.ipcSendSyncMsg, '同步');
  115. this.message3 = msg;
  116. },
  117. sendTosubWindow () {
  118. // 获取主窗口id
  119. this.$ipc.invoke(ipcApiRoute.getWCid, 'main').then(id => {
  120. this.mainWCid = id;
  121. this.$ipc.sendTo(this.mainWCid, specialIpcRoute.window2ToWindow1, '窗口2 通过 sendTo 给主窗口发送消息');
  122. });
  123. },
  124. }
  125. }
  126. </script>
  127. <style lang="less" scoped>
  128. #app-base-subwindow-ipc {
  129. padding: 0px 10px;
  130. text-align: left;
  131. width: 100%;
  132. height: 100%;
  133. background-color: #f0f2f5;
  134. .one-block-1 {
  135. font-size: 16px;
  136. padding-top: 10px;
  137. }
  138. .one-block-2 {
  139. padding-top: 10px;
  140. }
  141. }
  142. </style>