Skip to content

Commit

Permalink
Re-register websocket handlers after session deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
sudharsan-selvaraj committed Mar 18, 2024
1 parent bf7010b commit 493b5b2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
24 changes: 21 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class DevicePlugin extends BasePlugin {
private pluginArgs: IPluginArgs = Object.assign({}, DefaultPluginArgs);
public static NODE_ID: string;
public static IS_HUB = false;
private static httpServer: any;
private static adbInstance: any;

constructor(pluginName: string, cliArgs: any) {
super(pluginName, cliArgs);
Expand Down Expand Up @@ -116,15 +118,24 @@ class DevicePlugin extends BasePlugin {
);
}

private static async registerWebSocketHandlers() {
log.info('Registering websocket handler for Streaming');
await DevicePlugin.httpServer.addWebSocketHandler(
'/android-stream/:udid',
getStreamingServer(DevicePlugin.adbInstance),
);
}

public static async updateServer(
expressApp: any,
httpServer: any,
cliArgs: ServerArgs,
): Promise<void> {
// Specify the destination path where you want to save the downloaded file
const adb = await ADB.createADB({});
// log.debug(expressApp)
httpServer.addWebSocketHandler('/android-stream/:udid', getStreamingServer(adb));
DevicePlugin.httpServer = httpServer;
DevicePlugin.adbInstance = adb;
await DevicePlugin.registerWebSocketHandlers();
// cliArgs are here is not pluginArgs yet as it contains the whole CLI argument for Appium! Different case for our plugin constructor
log.debug(`📱 Update server with CLI Args: ${JSON.stringify(cliArgs)}`);
externalModule = await loadExternalModules();
Expand Down Expand Up @@ -299,6 +310,11 @@ class DevicePlugin extends BasePlugin {
jwpReqCaps: any,
caps: ISessionCapability,
) {
console.log('********** Handlers *****************');
const activeHandlers1 = await DevicePlugin.httpServer.getWebSocketHandlers();
for (const pathname of _.keys(activeHandlers1)) {
console.log(pathname);
}
log.debug(`📱 pluginArgs: ${JSON.stringify(this.pluginArgs)}`);
log.debug(`Receiving session request at host: ${this.pluginArgs.bindHostOrIp}`);
const pendingSessionId = uuidv4();
Expand Down Expand Up @@ -572,7 +588,9 @@ class DevicePlugin extends BasePlugin {
async deleteSession(next: () => any, driver: any, sessionId: any) {
await unblockDeviceMatchingFilter({ session_id: sessionId });
log.info(`📱 Unblocking the device that is blocked for session ${sessionId}`);
return await next();
const res = await next();
await DevicePlugin.registerWebSocketHandlers();
return res;
}
}

Expand Down
21 changes: 13 additions & 8 deletions web/src/components/AndroidStream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import { useEffect, useState } from 'react';

const AndroidStream = () => {
const [imageSrc, setImageSrc] = useState('');
let ws: any;
const handleWebSocketMessage = (event: { data: any }) => {
const blob = event.data;
const url = URL.createObjectURL(blob);
setImageSrc(url);
};

const createWebSocketConnection = (wsUrl: string) => {
ws = new WebSocket(wsUrl);
ws.addEventListener('message', handleWebSocketMessage);
ws.addEventListener('close', () => createWebSocketConnection(wsUrl));
};

useEffect(() => {
const getWebSocketPort = () => {
Expand All @@ -19,15 +31,8 @@ const AndroidStream = () => {

const { host, udid, port } = getWebSocketPort() as any;
const wsUrl = `ws://${host}:${port}/android-stream/${udid}`;
const ws = new WebSocket(wsUrl);

const handleWebSocketMessage = (event: { data: any }) => {
const blob = event.data;
const url = URL.createObjectURL(blob);
setImageSrc(url);
};

ws.addEventListener('message', handleWebSocketMessage);
createWebSocketConnection(wsUrl);

return () => {
// Clean up the WebSocket connection when the component is unmounted
Expand Down

0 comments on commit 493b5b2

Please sign in to comment.