<template>
  <div id="terminal" class="xterm"/>
</template>
<script>
import Terminal from './XTerm'
export default {
  name: 'Console',
  props: {
    // terminal: {
    //   type: Object,
    //   default: {}
    // }
  },
  data() {
    return {
      term: null,
      terminalSocket: null
    }
  },
  mounted() {
    console.log(444)
    var term = new Terminal()

    term.open(document.getElementById('terminal'))
    term.toggleFullScreen()
    term.write('Hello from \x1B[1;3;31mxterm.js\x1B[0m $ ')

    this.term = term

    var ws = new WebSocket('ws://localhost:3000/test/1')
    ws.onopen = function() {
      this.term.attach(ws)
      this.term._initialized = true
      // Web Socket 已连接上，使用 send() 方法发送数据
      ws.send('发送数据')
      console.log('数据发送中...')
    }

    ws.onmessage = function(evt) {
      var received_msg = evt.data
      console.log('数据已接收...', received_msg)
    }

    ws.onclose = function() {
      // 关闭 websocket
      console.log('连接已关闭...')
    }
  },
  beforeDestroy() {},
  methods: {}
}
</script>
