Ipc.vue 4.2 KB

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