UNPKG

2.63 kBJavaScriptView Raw
1/**
2 * Code for managing and reacting to changes in state for the
3 * Guacamole client and tunnel
4 */
5
6/**
7 * Used to map Guac Tunnel and Client status to meaningful value
8 * Index in array corresponds with status code
9 * I.E Guac tunnel returns status of 1 which indicated TUNNEL_STATE[1] or 'CONNECTED'
10 */
11const CONNECTING_STATUS = ['IDLE', 'CONNECTING', 'WAITING']
12const TUNNEL_STATE = ['CONNECTING', 'CONNECTED', 'CLOSED']
13const CLIENT_STATE = ['IDLE', 'CONNECTING', 'WAITING', 'CONNECTED', 'DISCONNECTING', 'DISCONNECTED']
14
15import Vue from 'vue'
16import GuacErrors from './GuacErrors'
17
18export default {
19 methods: {
20 /**
21 * Adds error handlers for the tunnel and client
22 *
23 * These error handlers will be called when ever an error is detected
24 * and will save the error back to the main client object
25 */
26 initErrorHandlers () {
27 this.tunnel.onerror = this.client.onerror = (error) => {
28 this.disconnect()
29 const guacError = GuacErrors.ERRORS[error.code]
30 this.error = guacError ? guacError : {reconnect: false, message: `An unknown error occurred. Code ${error.code}`}
31 }
32 },
33
34 /**
35 * Adds state change handlers for the tunnel and client
36 *
37 * These state change handlers will keep options synchronized
38 * with the state of the Guacamole tunnel and client
39 */
40 initStateChangeHandlers () {
41 this.tunnel.onstatechange = (state) => {
42 Vue.set(this.options, 'tunnelState', TUNNEL_STATE[state])
43 }
44
45 // Update connection state as client state changes
46 this.client.onstatechange = (clientState) => {
47 Vue.set(this.options, 'clientState', CLIENT_STATE[clientState])
48 this.$emit('clientStateChanged', CLIENT_STATE[clientState])
49 }
50 }
51 },
52 computed: {
53
54 /**
55 * Creates simplified overall status for Guacamole connection
56 *
57 * Combines the status for the tunnel and client in to one
58 * status suitable for dispalying to end user
59 */
60 status () {
61 const options = this.options
62 if (options.tunnelState === 'Not Connected' || options.clientState === 'Not Connected') return 'Not Connected'
63
64 if (options.tunnelState === 'CLOSED') return 'Disconnected'
65 if (options.tunnelState === 'CONNECTING') return 'Connecting'
66
67 if (CONNECTING_STATUS.includes(options.clientState)) return 'Connecting'
68 if (options.clientState === 'CONNECTED') return 'Connected'
69 if (options.clientState === 'DISCONNECTING') return 'Disconnecting'
70 if (options.clientState === 'DISCONNECTED') return 'Disconnected'
71
72 return options.clientState
73 }
74 }
75}