Ipc.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div id="app-base-socket-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="createWindow(0)">打开新窗口2</a-button>
  51. <a-button @click="sendTosubWindow()">向新窗口2发消息</a-button>
  52. </a-space>
  53. </div>
  54. </div>
  55. </template>
  56. <script>
  57. import { ipcApiRoute, specialIpcRoute } from '@/api/main'
  58. export default {
  59. data() {
  60. return {
  61. messageString: '',
  62. message1: '',
  63. message2: '',
  64. message3: '',
  65. windowName: 'window-ipc',
  66. newWcId: 0,
  67. views: [
  68. {
  69. type: 'vue',
  70. content: '/#/special/subwindow',
  71. windowName: 'window-ipc',
  72. windowTitle: 'ipc window'
  73. },
  74. ],
  75. }
  76. },
  77. mounted () {
  78. this.init();
  79. },
  80. methods: {
  81. init () {
  82. // 避免重复监听,或者将 on 功能写到一个统一的地方,只加载一次
  83. this.$ipc.removeAllListeners(ipcApiRoute.ipcSendMsg);
  84. this.$ipc.on(ipcApiRoute.ipcSendMsg, (event, result) => {
  85. console.log('[ipcRenderer] [socketMsgStart] result:', result);
  86. this.messageString = result;
  87. // 调用后端的另一个接口
  88. event.sender.send(ipcApiRoute.hello, 'electron-egg');
  89. })
  90. // 监听 窗口2 发来的消息
  91. this.$ipc.removeAllListeners(specialIpcRoute.window2ToWindow1);
  92. this.$ipc.on(specialIpcRoute.window2ToWindow1, (event, arg) => {
  93. this.$message.info(arg);
  94. })
  95. },
  96. sendMsgStart() {
  97. const params = {
  98. type: 'start',
  99. content: '开始'
  100. }
  101. this.$ipc.send(ipcApiRoute.ipcSendMsg, params)
  102. },
  103. sendMsgStop() {
  104. const params = {
  105. type: 'end',
  106. content: ''
  107. }
  108. this.$ipc.send(ipcApiRoute.ipcSendMsg, params)
  109. },
  110. handleInvoke() {
  111. this.$ipc.invoke(ipcApiRoute.ipcInvokeMsg, '异步-回调').then(r => {
  112. console.log('r:', r);
  113. this.message1 = r;
  114. });
  115. },
  116. async handleInvoke2() {
  117. const msg = await this.$ipc.invoke(ipcApiRoute.ipcInvokeMsg, '异步');
  118. console.log('msg:', msg);
  119. this.message2 = msg;
  120. },
  121. handleSendSync() {
  122. const msg = this.$ipc.sendSync(ipcApiRoute.ipcSendSyncMsg, '同步');
  123. this.message3 = msg;
  124. },
  125. createWindow(index) {
  126. this.$ipc.invoke(ipcApiRoute.createWindow, this.views[index]).then(id => {
  127. console.log('[createWindow] id:', id);
  128. })
  129. },
  130. async sendTosubWindow() {
  131. // 新窗口id
  132. this.newWcId = await this.$ipc.invoke(ipcApiRoute.getWCid, this.windowName);
  133. this.$ipc.sendTo(this.newWcId, specialIpcRoute.window1ToWindow2, '窗口1通过 sendTo 给窗口2发送消息');
  134. },
  135. }
  136. }
  137. </script>
  138. <style lang="less" scoped>
  139. #app-base-socket-ipc {
  140. padding: 0px 10px;
  141. text-align: left;
  142. width: 100%;
  143. .one-block-1 {
  144. font-size: 16px;
  145. padding-top: 10px;
  146. }
  147. .one-block-2 {
  148. padding-top: 10px;
  149. }
  150. }
  151. </style>