Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/APIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ module.exports = class APIManager {
description: 'Disconnect from the current instance, ending the WebSocket communication.',
});

// Register a function to restart the webrtc connection
this.registerFunction({
name: 'reconnect',
category: 'VM_communication',
fn: () => {
this.instance.reconnect();
},
description: 'Disconnect and recreate the webrtc connection.',
});

// Register a function to get all registered functions
this.registerFunction({
name: 'getRegisteredFunctions',
Expand Down
15 changes: 14 additions & 1 deletion src/DeviceRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,19 @@ module.exports = class DeviceRenderer {
}
}

/**
* Reconnect the instance. Disconnect the current instance, and then
* attempt to reconnect.
*
* When reconnecting, it will attempt to add the previous keyboard and mouse
* callbacks. (including keyboard mapping if applicable)
*/
reconnect() {
this.disconnect();
this.onWebRTCReady();
this.store.reconnect();
}

/**
* Send event to the instance through the Websocket connection.
*
Expand Down Expand Up @@ -572,7 +585,7 @@ module.exports = class DeviceRenderer {
this.onConnectionStateChange = () => {
log.debug('ConnectionState changed:', this.peerConnection.iceConnectionState);
if (this.peerConnection.iceConnectionState === 'disconnected') {
this.onWebRTCReady();
this.reconnect();
}
};
this.addListener(this.peerConnection, 'connectionstatechange', this.onConnectionStateChange);
Expand Down
12 changes: 11 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,17 @@ const createStore = (instance, reducer) => {
return unsubscribe;
};

instance.store = {state: initialState, dispatch, subscribe, getters};
/**
* This function is used when the connection to the device is lost and restored.
* It will re-emit the current state to all listeners.
*/
const reconnect = () => {
listeners.forEach(({cb}) => {
cb({...instance.store.state});
});
};

instance.store = {state: initialState, dispatch, subscribe, reconnect, getters};
};

const reducer = (state, action) => {
Expand Down