App.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <div>
  3. <router-view></router-view>
  4. <video
  5. id="localVideo"
  6. ref="localVideoRef"
  7. autoplay
  8. playsinline
  9. :muted="muted"
  10. ></video>
  11. <div>
  12. <button @click="startAction">start</button>
  13. </div>
  14. </div>
  15. </template>
  16. <script lang="ts" setup>
  17. import { onMounted, ref } from 'vue';
  18. import { WebRTCClass } from '@/network/webRtc';
  19. import { useNetworkStore } from '@/store//network';
  20. const muted = ref(true);
  21. const localVideoRef = ref<HTMLVideoElement>();
  22. const networkStore = useNetworkStore();
  23. let stream: MediaStream;
  24. onMounted(() => {
  25. // const instance = new WebSocketClass({ url: 'ws://localhost:4300' });
  26. // networkStore.updateWsMap(roomId.value, instance);
  27. // instance.wsInstance?.on(wsConnectStatus.connect, () => {
  28. // console.log('连接websocket成功!');
  29. // handleWebRtc();
  30. // });
  31. });
  32. async function handleWebRtc() {
  33. const webrtc = new WebRTCClass();
  34. console.log(webrtc);
  35. const offer = await webrtc.createOffer();
  36. console.log(offer);
  37. }
  38. // Handles start button action: creates local MediaStream.
  39. function startAction() {
  40. // WARN navigator.mediaDevices在localhost和https才能用,http://192.168.1.103:8000局域网用不了
  41. navigator.mediaDevices
  42. .getUserMedia({ video: true, audio: true })
  43. .then((event) => {
  44. console.log('getUserMedia成功', event);
  45. stream = event;
  46. // if (!localVideoRef.value) return;
  47. // localVideoRef.value.srcObject = event;
  48. })
  49. .catch((err) => {
  50. console.log('getUserMedia失败', err);
  51. });
  52. }
  53. </script>
  54. <style lang="scss" scoped></style>