console.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <div class="console" id="terminal"></div>
  3. </template>
  4. <script>
  5. import SockJS from 'sockjs-client';
  6. // import Terminal from './term';
  7. import { Terminal } from 'xterm'
  8. import * as fit from 'xterm/lib/addons/fit/fit'
  9. import * as attach from 'xterm/lib/addons/attach/attach'
  10. export default {
  11. name: 'term',
  12. props: {
  13. res: (Object,String)
  14. },
  15. data () {
  16. return {
  17. term: null,
  18. terminalSocket: null
  19. }
  20. },
  21. methods: {
  22. runRealTerminal () {
  23. console.log('webSocket is finished')
  24. },
  25. errorRealTerminal () {
  26. console.log('error')
  27. },
  28. closeRealTerminal () {
  29. console.log('close')
  30. }
  31. },
  32. beforeMount (){
  33. Terminal.applyAddon(fit)
  34. Terminal.applyAddon(attach)
  35. },
  36. mounted () {
  37. let that = this;
  38. let terminalContainer = document.getElementById('terminal')
  39. that.term = new Terminal({
  40. fontSize: 14,
  41. fontFamily: 'Consolas, "Courier New", monospace',
  42. bellStyle: 'sound',
  43. cursorBlink: true,
  44. })
  45. that.term.open(terminalContainer)
  46. that.term.focus();
  47. that.term.fit();
  48. // setTimeout(() => {
  49. // that.term.fit();
  50. // }, 100);
  51. fetch('/devopsApi/createPodAttachSession/5d134c4f7cb0322f6049c785/devops-api-1-0-d5dc659cc-jd7d5').then(res =>{
  52. res.json().then(result => {
  53. that.terminalSocket = new SockJS('/devopsApi/api/sockjs?' + result.sessionId);
  54. that.terminalSocket.onopen = function() {
  55. const startData = {Op: 'bind', SessionID: result.sessionId};
  56. that.terminalSocket.send(JSON.stringify(startData));
  57. };
  58. that.terminalSocket.onmessage = function(e) {
  59. const frame = JSON.parse(e.data);
  60. if (frame.Op === 'stdout') {
  61. that.term.write(frame.Data);
  62. }
  63. };
  64. that.terminalSocket.onclose = function() {
  65. console.log('close');
  66. };
  67. that.term.on('resize', () => {
  68. that.terminalSocket.send(JSON.stringify({
  69. Op: 'resize',
  70. Cols: term.cols,
  71. Rows: term.rows,
  72. }));
  73. });
  74. that.term.on('data', (data) => {
  75. that.terminalSocket.send(JSON.stringify({
  76. Op: 'stdin',
  77. Data: data,
  78. Cols: term.cols,
  79. Rows: term.rows,
  80. }));
  81. });
  82. });
  83. });
  84. },
  85. beforeDestroy () {
  86. this.terminalSocket.close()
  87. this.term.destroy()
  88. }
  89. }
  90. </script>
  91. <style lang="less" scoped>
  92. </style>