iframe.vue 2.36 KB
<template>
  <div>
    <h2>客户端</h2>
    <p>
      <el-button type="primary" @click="open">呼叫</el-button>
      <el-button type="primary" @click="cancelCall">取消呼叫</el-button>
    </p>
    <iframe
      v-if="isStart"
      ref="srtcIframe"
      title="srtcIframe"
      :src="target"
      frameborder="2"
      allow="microphone;camera;midi;encrypted-media;fullscreen;"
    ></iframe>
    <p>callback:{{ message }}</p>
    <p>这还少恩位置</p>
    <p>这还少恩位置</p>
    <p>这还少恩位置</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      baseUrl: 'https://localhost:8080',
      isStart: false,
      target: '',
      strWindowFeatures: `
        left=2400,
        top=200
        height=500,
        width=700,
        menubar=1,
        toolbar=1,
        fullscreen=1
        `,
      message: '',
      windowRef: ''
    }
  },
  beforeMount() {
    window.addEventListener('message', this.receiveMessage, false)
  },
  methods: {
    open() {
      this.isStart = true
      this.target = `${this.baseUrl}/#/srtc-iframe`
      this.$nextTick().then(() => {
        this.$refs.srtcIframe.onload = () => {
          this.sendMessage()
        }
      })
    },
    sendMessage() {
      this.$refs.srtcIframe.contentWindow.postMessage(
        {
          type: 'startCall',
          data: {
            appUserName: '123',
            appUserPhone: '16621043587',
            orderId: '123213',
            webUserName: 'lz'
          }
        },
        this.baseUrl
      )
    },
    cancelCall() {
      this.$refs.srtcIframe.contentWindow.postMessage(
        {
          type: 'cancelCall'
        },
        this.baseUrl
      )
    },
    receiveMessage(e) {
      if (e.origin !== this.baseUrl) {
        return
      }
      const { type, data } = e
      // switch (type) {
      //   case 'userJoin':
      //     this.popMessage(data)
      //     break
      //   case 'appHangUp':
      //     this.popMessage(data)
      //     break
      //   case 'webHangUp':
      //     this.popMessage(data)
      //     break
      // }
      this.popMessage(`${type}---${data}`)
    },
    popMessage(data) {
      this.$message({
        type: 'error',
        message: data
      })
    }
  }
}
</script>

<style lang="scss" scoped>
iframe {
  width: 500px;
  height: 600px;
  position: fixed;
  top: 0;
  right: 0;
}
</style>