Skip to content

Commit

Permalink
register websocket when appium throws unexpectedError (#1059)
Browse files Browse the repository at this point in the history
  • Loading branch information
saikrishna321 authored Mar 20, 2024
1 parent dc242e2 commit 1de3570
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import * as https from 'https';
import fs from 'fs';
// import { runAndroidAdbServer } from './device-managers/AdbServer';
import { getStreamingServer } from './streaming-server';
import { sleep } from 'asyncbox';
import { sleep, waitForCondition } from 'asyncbox';

const commandsQueueGuard = new AsyncLock();
const DEVICE_MANAGER_LOCK_NAME = 'DeviceManager';
Expand Down Expand Up @@ -117,6 +117,13 @@ class DevicePlugin extends BasePlugin {
deviceFilter,
)} onUnexpectedShutdown from server`,
);
await waitForCondition(async () => {
console.log('Waiting for websocket handlers to be removed..');
return !(await DevicePlugin.httpServer.getWebSocketHandlers()).hasOwnProperty(
'/android-stream/:udid',
);
});
await DevicePlugin.registerWebSocketHandlers();
}

private static async registerWebSocketHandlers() {
Expand Down Expand Up @@ -249,6 +256,7 @@ class DevicePlugin extends BasePlugin {
try {
const selectedDevice = client.getDevice(device.id);
await selectedDevice.uninstall('com.device.farm');
await sleep(800);
await selectedDevice.install(apk);
console.log(`Installed ${apk} on device ${device.id}`);
} catch (installError: any) {
Expand Down
2 changes: 1 addition & 1 deletion src/streaming-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function getStreamingServer(adbInstance: ADB) {
await proxySocket.start();
log.info(`Successfully created proxy streaming session for device ${udid}`);
SocketMap.set(streamingKey, proxySocket);
proxySocket.addListener(ws);
await proxySocket.addListener(ws);
} catch (err) {
console.log('Unable to connect to remote streaming socket');
console.log(err);
Expand Down
5 changes: 4 additions & 1 deletion web/src/components/AndroidStream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ const AndroidStream = () => {
const createWebSocketConnection = (wsUrl: string) => {
ws = new WebSocket(wsUrl);
ws.addEventListener('message', handleWebSocketMessage);
ws.addEventListener('close', () => createWebSocketConnection(wsUrl));
ws.addEventListener('close', () => {
console.log('WebSocket connection closed. Reconnecting...');
createWebSocketConnection(wsUrl)
});
};

useEffect(() => {
Expand Down

0 comments on commit 1de3570

Please sign in to comment.