From 7db9d8b90a64f8c39db9dbdbeed1c25f4dc3afe8 Mon Sep 17 00:00:00 2001 From: "suk.bear" Date: Sat, 7 Sep 2024 09:35:00 +0800 Subject: [PATCH] fix: add use web channel --- app/composables/webChannel.ts | 92 ++++++++++++++++ app/pages/v-screen/index/web-camera.vue | 105 ++++++++++--------- app/plugins/bridge.ts | 18 ---- {public/plugins => app/utils}/web-channel.js | 25 +++-- 4 files changed, 164 insertions(+), 76 deletions(-) create mode 100644 app/composables/webChannel.ts delete mode 100644 app/plugins/bridge.ts rename {public/plugins => app/utils}/web-channel.js (96%) diff --git a/app/composables/webChannel.ts b/app/composables/webChannel.ts new file mode 100644 index 0000000..ea3a896 --- /dev/null +++ b/app/composables/webChannel.ts @@ -0,0 +1,92 @@ +/* eslint-disable no-new */ +import QWebChannel, { isQtClient } from '~/utils/web-channel.js' + +interface QtWebSocketOptions { + onDataUpdated?: (data: any) => void + onConnected?: () => void + onDisconnected?: () => void + onError?: (error: any) => void +} + +export function useWebChannel(options: QtWebSocketOptions = {}) { + const { onDataUpdated } = options + + const qtObject = ref(null) + // const isConnected = ref(false) + + // const { status: wsStatus, send: wsSend, open, close } = useWebSocket('ws://your-websocket-url', { + // autoReconnect: true, + // onConnected: () => { + // isConnected.value = true + // onConnected?.() + // }, + // onDisconnected: () => { + // isConnected.value = false + // onDisconnected?.() + // }, + // onError, + // }) + + const sendMessageToCpp = (message: any) => { + if (qtObject.value && typeof qtObject.value.receiveMessage === 'function') { + qtObject.value.receiveMessage(message) + } + else { + console.error('myObject or receiveMessage function not available') + } + } + + const initQtWebChannel = () => { + if (import.meta.env.DEV || !isQtClient) { + window.qt = { + webChannelTransport: { + send() { + window.console.log(` + QWebChannel simulator activated ! + `) + }, + + onmessage: () => { + window.console.log(` + QWebChannel simulator activated ! + `) + }, + }, + } + } + + new QWebChannel(window.qt.webChannelTransport, (channel: any) => { + if (channel.objects.myObject) { + window.console.log('myObject is available') + qtObject.value = channel.objects.myObject + qtObject.value.dataUpdated.connect((data: any) => { + if (data) { + onDataUpdated?.(data) + } + else { + console.error('data is null') + } + }) + } + else { + console.error('myObject is not available') + } + }) + } + + onMounted(() => { + initQtWebChannel() + }) + + // onUnmounted(() => { + // close() + // }) + + return { + // isConnected, + // wsStatus, + // sendWebSocketMessage: wsSend, + sendMessageToCpp, + qtObject, + } +} diff --git a/app/pages/v-screen/index/web-camera.vue b/app/pages/v-screen/index/web-camera.vue index fad02b9..0896787 100644 --- a/app/pages/v-screen/index/web-camera.vue +++ b/app/pages/v-screen/index/web-camera.vue @@ -1,61 +1,66 @@