| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- <template>
- <div>
- <h1 v-if="NODE_ENV === 'development'">
- 我的id:{{ mySocketId }},<n-button @click="copyToClipBoard(mySocketId)">
- 复制
- </n-button>
- </h1>
- <div>
- <n-input-group>
- <n-input-group-label>被控id</n-input-group-label>
- <n-input
- :style="{ width: '200px' }"
- placeholder="请输入被控id"
- v-model:value="receiverId"
- />
- <n-button
- type="error"
- @click="handleClose"
- v-if="appStore.remoteDesk.isRemoteing"
- >
- 结束控制
- </n-button>
- <n-button
- v-else
- type="primary"
- @click="handleRemote"
- >
- 开始远程
- </n-button>
- </n-input-group>
- </div>
- <div
- class="wrap"
- ref="remoteVideoRef"
- @mousedown="handleMouseDown"
- @mousemove="handleMouseMove"
- @mouseup="handleMouseUp"
- @dblclick="handleDoublelclick"
- @contextmenu="handleContextmenu"
- ></div>
- </div>
- </template>
- <script lang="ts" setup>
- import { Key } from '@nut-tree/shared';
- import { copyToClipBoard, getRandomString } from 'billd-utils';
- import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
- import { usePull } from '@/hooks/use-pull';
- import { useTip } from '@/hooks/use-tip';
- import { useAppStore } from '@/store/app';
- import { useNetworkStore } from '@/store/network';
- import {
- RemoteDeskBehaviorEnum,
- WsMsgTypeEnum,
- WsRemoteDeskBehaviorType,
- WsStartRemoteDesk,
- } from '@/types/websocket';
- import { NODE_ENV } from 'script/constant';
- const num = '123456';
- const appStore = useAppStore();
- const networkStore = useNetworkStore();
- const { videoWrapRef, initPull } = usePull(num);
- const roomId = ref(num);
- const receiverId = ref('');
- const remoteVideoRef = ref<HTMLDivElement>();
- const isDown = ref(false);
- let clickTimer;
- let isLongClick = false;
- const mySocketId = computed(() => {
- return networkStore.wsMap.get(roomId.value)?.socketIo?.id || '-1';
- });
- onUnmounted(() => {
- window.removeEventListener('keydown', handleKeyDown);
- handleClose();
- });
- onMounted(() => {
- initPull({
- isRemoteDesk: true,
- });
- videoWrapRef.value = remoteVideoRef.value;
- window.addEventListener('keydown', handleKeyDown);
- });
- function handleKeyDown(e: KeyboardEvent) {
- // console.log(e.key, e.code);
- const keyMap = {
- Enter: Key.Enter,
- Space: Key.Space,
- Backspace: Key.Backspace,
- ShiftLeft: Key.LeftShift,
- ShiftRight: Key.RightShift,
- AltLeft: Key.LeftAlt,
- AltRight: Key.RightAlt,
- Tab: Key.Tab,
- Backquote: Key.Quote,
- Backslash: Key.Backslash,
- ArrowUp: Key.Up,
- ArrowDown: Key.Down,
- ArrowLeft: Key.Left,
- ArrowRight: Key.Right,
- CapsLock: Key.CapsLock,
- ControlLeft: Key.LeftControl,
- ControlRight: Key.RightControl,
- MetaLeft: Key.LeftCmd,
- LeftWin: Key.LeftCmd,
- MetaRight: Key.RightCmd,
- RightWin: Key.RightCmd,
- Fn: Key.Fn,
- F1: Key.F1,
- F2: Key.F2,
- F3: Key.F3,
- F4: Key.F4,
- F5: Key.F5,
- F6: Key.F6,
- F7: Key.F7,
- F8: Key.F8,
- F9: Key.F9,
- F10: Key.F10,
- F11: Key.F11,
- F12: Key.F12,
- F13: Key.F13,
- F14: Key.F14,
- F15: Key.F15,
- F16: Key.F16,
- F17: Key.F17,
- F18: Key.F18,
- F19: Key.F19,
- F20: Key.F20,
- F21: Key.F21,
- F22: Key.F22,
- F23: Key.F23,
- F24: Key.F24,
- };
- networkStore.rtcMap
- .get(receiverId.value)
- ?.dataChannelSend<WsRemoteDeskBehaviorType['data']>({
- requestId: getRandomString(8),
- msgType: WsMsgTypeEnum.remoteDeskBehavior,
- data: {
- roomId: roomId.value,
- sender: mySocketId.value,
- receiver: receiverId.value,
- type: RemoteDeskBehaviorEnum.keyboardType,
- keyboardtype: keyMap[e.code] || e.key,
- x: 0,
- y: 0,
- },
- });
- }
- watch(
- () => appStore.remoteDesk.isClose,
- (newval) => {
- if (newval) {
- networkStore.removeRtc(receiverId.value);
- useTip({
- content: '远程连接断开',
- hiddenCancel: true,
- hiddenClose: true,
- }).catch();
- }
- }
- );
- function handleDoublelclick() {
- networkStore.rtcMap
- .get(receiverId.value)
- ?.dataChannelSend<WsRemoteDeskBehaviorType['data']>({
- requestId: getRandomString(8),
- msgType: WsMsgTypeEnum.remoteDeskBehavior,
- data: {
- roomId: roomId.value,
- sender: mySocketId.value,
- receiver: receiverId.value,
- type: RemoteDeskBehaviorEnum.doubleClick,
- keyboardtype: 0,
- x: 0,
- y: 0,
- },
- });
- }
- function handleContextmenu() {
- networkStore.rtcMap
- .get(receiverId.value)
- ?.dataChannelSend<WsRemoteDeskBehaviorType['data']>({
- requestId: getRandomString(8),
- msgType: WsMsgTypeEnum.remoteDeskBehavior,
- data: {
- roomId: roomId.value,
- sender: mySocketId.value,
- receiver: receiverId.value,
- type: RemoteDeskBehaviorEnum.rightClick,
- keyboardtype: 0,
- x: 0,
- y: 0,
- },
- });
- }
- function handleMouseDown(event: MouseEvent) {
- isDown.value = true;
- clickTimer = setTimeout(function () {
- console.log('长按');
- isLongClick = true;
- clearTimeout(clickTimer);
- }, 300);
- // 获取点击相对于视窗的位置
- const clickX = event.clientX;
- const clickY = event.clientY;
- // 获取目标元素的位置和尺寸信息
- // @ts-ignore
- const rect: DOMRect = event.target.getBoundingClientRect();
- // 计算点击位置相对于元素的坐标
- const xInsideElement = clickX - rect.left;
- const yInsideElement = clickY - rect.top;
- const x = (xInsideElement / rect.width) * 1000;
- const y = (yInsideElement / rect.height) * 1000;
- console.log('handleMouseDown', x, y, xInsideElement, yInsideElement);
- networkStore.rtcMap
- .get(receiverId.value)
- ?.dataChannelSend<WsRemoteDeskBehaviorType['data']>({
- requestId: getRandomString(8),
- msgType: WsMsgTypeEnum.remoteDeskBehavior,
- data: {
- roomId: roomId.value,
- sender: mySocketId.value,
- receiver: receiverId.value,
- keyboardtype: 0,
- type: isLongClick
- ? RemoteDeskBehaviorEnum.pressButtonLeft
- : RemoteDeskBehaviorEnum.pressButtonLeft,
- x,
- y,
- },
- });
- }
- function handleMouseMove(event: MouseEvent) {
- // 获取点击相对于视窗的位置
- const clickX = event.clientX;
- const clickY = event.clientY;
- // 获取目标元素的位置和尺寸信息
- // @ts-ignore
- const rect: DOMRect = event.target.getBoundingClientRect();
- // 计算点击位置相对于元素的坐标
- const xInsideElement = clickX - rect.left;
- const yInsideElement = clickY - rect.top;
- const x = (xInsideElement / rect.width) * 1000;
- const y = (yInsideElement / rect.height) * 1000;
- console.log('handleMouseMove', x, y, xInsideElement, yInsideElement);
- networkStore.rtcMap
- .get(receiverId.value)
- ?.dataChannelSend<WsRemoteDeskBehaviorType['data']>({
- requestId: getRandomString(8),
- msgType: WsMsgTypeEnum.remoteDeskBehavior,
- data: {
- roomId: roomId.value,
- sender: mySocketId.value,
- receiver: receiverId.value,
- type: RemoteDeskBehaviorEnum.move,
- keyboardtype: 0,
- x,
- y,
- },
- });
- }
- function handleMouseUp(event: MouseEvent) {
- if (clickTimer) {
- clearTimeout(clickTimer);
- }
- isDown.value = false;
- // 获取点击相对于视窗的位置
- const clickX = event.clientX;
- const clickY = event.clientY;
- // 获取目标元素的位置和尺寸信息
- // @ts-ignore
- const rect: DOMRect = event.target.getBoundingClientRect();
- // 计算点击位置相对于元素的坐标
- const xInsideElement = clickX - rect.left;
- const yInsideElement = clickY - rect.top;
- const x = (xInsideElement / rect.width) * 1000;
- const y = (yInsideElement / rect.height) * 1000;
- console.log('handleMouseUp', x, y, xInsideElement, yInsideElement);
- networkStore.rtcMap
- .get(receiverId.value)
- ?.dataChannelSend<WsRemoteDeskBehaviorType['data']>({
- requestId: getRandomString(8),
- msgType: WsMsgTypeEnum.remoteDeskBehavior,
- data: {
- roomId: roomId.value,
- sender: mySocketId.value,
- receiver: receiverId.value,
- keyboardtype: 0,
- type: isLongClick
- ? RemoteDeskBehaviorEnum.releaseButtonLeft
- : RemoteDeskBehaviorEnum.releaseButtonLeft,
- x,
- y,
- },
- });
- isLongClick = false;
- }
- function handleClose() {
- networkStore.removeRtc(receiverId.value);
- }
- function handleRemote() {
- networkStore.wsMap.get(roomId.value)?.send<WsStartRemoteDesk['data']>({
- requestId: getRandomString(8),
- msgType: WsMsgTypeEnum.startRemoteDesk,
- data: {
- roomId: roomId.value,
- sender: mySocketId.value,
- receiver: receiverId.value,
- },
- });
- }
- </script>
- <style lang="scss" scoped>
- .wrap {
- line-height: 0;
- cursor: none;
- }
- </style>
|