Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 1 | <template> |
| 2 | <div id="terminal" ref="panel"></div> |
| 3 | </template> |
| 4 | |
| 5 | <script> |
| 6 | import { AttachAddon } from 'xterm-addon-attach'; |
| 7 | import { FitAddon } from 'xterm-addon-fit'; |
| 8 | import { Terminal } from 'xterm'; |
| 9 | |
| 10 | export default { |
| 11 | name: 'SerialOverLanConsole', |
| 12 | mounted() { |
| 13 | this.openTerminal(); |
| 14 | }, |
| 15 | methods: { |
| 16 | openTerminal() { |
| 17 | const token = this.$store.getters['authentication/token']; |
| 18 | |
| 19 | const ws = new WebSocket(`wss://${window.location.host}/console0`, [ |
| 20 | token |
| 21 | ]); |
| 22 | |
| 23 | // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons. |
| 24 | |
| 25 | const term = new Terminal({ |
| 26 | fontSize: 15, |
| 27 | fontFamily: |
| 28 | 'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace' |
| 29 | }); |
| 30 | |
| 31 | const attachAddon = new AttachAddon(ws); |
| 32 | term.loadAddon(attachAddon); |
| 33 | |
| 34 | const fitAddon = new FitAddon(); |
| 35 | term.loadAddon(fitAddon); |
| 36 | |
| 37 | const SOL_THEME = { |
| 38 | background: '#19273c', |
| 39 | cursor: 'rgba(83, 146, 255, .5)', |
| 40 | scrollbar: 'rgba(83, 146, 255, .5)' |
| 41 | }; |
| 42 | term.setOption('theme', SOL_THEME); |
| 43 | |
| 44 | term.open(this.$refs.panel); |
| 45 | fitAddon.fit(); |
| 46 | |
| 47 | try { |
| 48 | ws.onopen = function() { |
| 49 | console.log('websocket console0/ opened'); |
| 50 | }; |
| 51 | ws.onclose = function(event) { |
| 52 | console.log( |
| 53 | 'websocket console0/ closed. code: ' + |
| 54 | event.code + |
| 55 | ' reason: ' + |
| 56 | event.reason |
| 57 | ); |
| 58 | }; |
| 59 | } catch (error) { |
| 60 | console.log(error); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | }; |
| 65 | </script> |
| 66 | |
| 67 | <style scoped> |
| 68 | @import '~xterm/css/xterm.css'; |
| 69 | |
| 70 | #terminal { |
| 71 | height: 25em; |
| 72 | overflow: hidden; |
| 73 | } |
| 74 | </style> |