From a45e2bd3794da2ff66b0ead94704fc86d9c0d638 Mon Sep 17 00:00:00 2001
From: Alessandro Pignotti <a.pignotti@sssup.it>
Date: Tue, 1 Oct 2024 11:41:13 +0200
Subject: [PATCH] Use plain HTTP as fallback when WSS fails

---
 index.html | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/index.html b/index.html
index b80940d..500ef7f 100644
--- a/index.html
+++ b/index.html
@@ -345,7 +345,26 @@ __      __   _  __   ____  __
 		switch (device_type)
 		{
 			case "cloud":
-				blockDevice = await CheerpX.CloudDevice.create(image_url);
+				try
+				{
+					blockDevice = await CheerpX.CloudDevice.create(image_url);
+				}
+				catch(e)
+				{
+					// Report the failure and try again with plain HTTP
+					var wssProtocol = "wss:";
+					if(image_url.startsWith(wssProtocol))
+					{
+						// WebSocket protocol failed, try agin using plain HTTP
+						image_url = "https:" + image_url.substr(wssProtocol.length);
+						blockDevice = await CheerpX.CloudDevice.create(image_url);
+					}
+					else
+					{
+						// No other recovery option
+						throw e;
+					}
+				}
 				break;
 			case "bytes":
 				blockDevice = await CheerpX.HttpBytesDevice.create(image_url);