switching networks works, established server functions
This commit is contained in:
38
ui/_/code/bridge/bridge.js
Normal file
38
ui/_/code/bridge/bridge.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const IS_NODE =
|
||||
typeof process !== "undefined" &&
|
||||
process.versions?.node != null
|
||||
|
||||
async function bridgeSend(name, args) {
|
||||
// Example browser implementation: send function call to server
|
||||
const res = await global.Socket.send({
|
||||
name: name,
|
||||
args: args
|
||||
})
|
||||
|
||||
const json = await res.json()
|
||||
if (!res.ok) throw new Error(json.error)
|
||||
return json.result
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps an object of functions so that:
|
||||
* - Node calls the real function
|
||||
* - Browser calls bridgeSend
|
||||
*/
|
||||
export function createBridge(funcs) {
|
||||
return new Proxy(funcs, {
|
||||
get(target, prop) {
|
||||
const orig = target[prop]
|
||||
|
||||
if (typeof orig !== "function") return orig
|
||||
|
||||
return function (...args) {
|
||||
if (IS_NODE) {
|
||||
return orig(...args)
|
||||
} else {
|
||||
return bridgeSend(prop, args)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
11
ui/_/code/bridge/serverFunctions.js
Normal file
11
ui/_/code/bridge/serverFunctions.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import fs from "fs"
|
||||
import { createBridge } from "./bridge.js"
|
||||
|
||||
const handlers = {
|
||||
getProfile(one, two) {
|
||||
fs.writeFileSync("output.txt", `${one} ${two}`)
|
||||
return "written to disk"
|
||||
},
|
||||
}
|
||||
|
||||
export const { getProfile } = createBridge(handlers)
|
||||
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
Sam Russell
|
||||
Captured Sun
|
||||
1.16.26 - Moving nav event dispatch out of pushState, adding null feature to attr()
|
||||
1.5.26 - Switching verticalAlign and horizontalAlign names, adding borderVertical and Horizontal
|
||||
12.26.25 - State for arrays, nested objects. State for stacks (Shadow-only)
|
||||
12.17.25 - [Hyperia] - adding width, height functions. adding "e" to onClick. adding the non-window $$ funcs.
|
||||
@@ -27,7 +28,6 @@ let oldPushState = history.pushState;
|
||||
history.pushState = function pushState() {
|
||||
let ret = oldPushState.apply(this, arguments);
|
||||
window.dispatchEvent(new Event('pushstate'));
|
||||
window.dispatchEvent(new Event('navigate'));
|
||||
return ret;
|
||||
};
|
||||
|
||||
@@ -53,8 +53,8 @@ window.setQuery = function(key, value) {
|
||||
};
|
||||
|
||||
window.navigateTo = function(url) {
|
||||
window.dispatchEvent(new Event('navigate'));
|
||||
window.history.pushState({}, '', url);
|
||||
window.dispatchEvent(new Event('navigate'));
|
||||
}
|
||||
|
||||
window.setLocation = function(url) {
|
||||
@@ -1197,7 +1197,11 @@ HTMLElement.prototype.attr = function(attributes) {
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(attributes)) {
|
||||
this.setAttribute(key, value);
|
||||
if(value === null) {
|
||||
this.removeAttribute(key)
|
||||
} else {
|
||||
this.setAttribute(key, value);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
@@ -1,62 +1,61 @@
|
||||
class Connection {
|
||||
connectionTries = 0
|
||||
connectionTries = 0;
|
||||
ws;
|
||||
linkCreated;
|
||||
wsStatus;
|
||||
receiveCB;
|
||||
|
||||
constructor(receiveCB) {
|
||||
this.init()
|
||||
this.receiveCB = receiveCB
|
||||
this.receiveCB = receiveCB;
|
||||
}
|
||||
|
||||
init() {
|
||||
if(window.location.hostname.includes("local")) {
|
||||
this.ws = new WebSocket("ws://" + window.location.host)
|
||||
} else {
|
||||
this.ws = new WebSocket("wss://" + window.location.hostname + window.location.pathname)
|
||||
}
|
||||
this.ws.addEventListener('open', () => {
|
||||
this.connectionTries = 0
|
||||
console.log("Websocket connection established.");
|
||||
this.ws.addEventListener('message', this.receiveCB)
|
||||
|
||||
init = async () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const url = window.location.hostname.includes("local")
|
||||
? "ws://" + window.location.host
|
||||
: "wss://" + window.location.hostname + window.location.pathname;
|
||||
|
||||
this.ws = new WebSocket(url);
|
||||
|
||||
this.ws.addEventListener('open', () => {
|
||||
this.connectionTries = 0;
|
||||
console.log("WebSocket connection established.");
|
||||
this.ws.addEventListener('message', this.receiveCB);
|
||||
resolve(this.ws); // resolve when open
|
||||
});
|
||||
|
||||
this.ws.addEventListener('close', () => {
|
||||
console.log('WebSocket closed');
|
||||
this.checkOpen(); // attempt reconnection
|
||||
});
|
||||
|
||||
this.ws.addEventListener('error', (err) => {
|
||||
console.error('WebSocket error', err);
|
||||
reject(err); // reject if error occurs
|
||||
});
|
||||
});
|
||||
this.ws.addEventListener("close", () => {
|
||||
this.checkOpen();
|
||||
console.log('Websocket Closed')
|
||||
})
|
||||
}
|
||||
|
||||
async checkOpen() {
|
||||
|
||||
checkOpen = async () => {
|
||||
if (this.ws.readyState === WebSocket.OPEN) {
|
||||
return true
|
||||
return true;
|
||||
} else {
|
||||
await this.sleep(this.connectionTries < 20 ? 5000 : 60000)
|
||||
this.connectionTries++
|
||||
console.log('Reestablishing connection')
|
||||
this.init()
|
||||
await this.sleep(this.connectionTries < 20 ? 5000 : 60000);
|
||||
this.connectionTries++;
|
||||
console.log('Reestablishing connection');
|
||||
await this.init();
|
||||
}
|
||||
}
|
||||
|
||||
sleep = (time) => {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(resolve, time);
|
||||
});
|
||||
}
|
||||
|
||||
sleep = (time) => new Promise(resolve => setTimeout(resolve, time));
|
||||
|
||||
send = (msg) => {
|
||||
console.log("sending")
|
||||
if (this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(msg);
|
||||
}
|
||||
else if(this.connectionTries === 0) {
|
||||
setTimeout(() => {
|
||||
this.send(msg)
|
||||
}, 100)
|
||||
}
|
||||
else {
|
||||
console.error('No websocket connection: Cannot send message');
|
||||
} else if (this.connectionTries === 0) {
|
||||
setTimeout(() => this.send(msg), 100);
|
||||
} else {
|
||||
console.error('No WebSocket connection: Cannot send message');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Connection
|
||||
export default Connection;
|
||||
|
||||
@@ -10,6 +10,10 @@ export default class Socket {
|
||||
this.connection = new Connection(this.receive);
|
||||
}
|
||||
|
||||
async init() {
|
||||
await this.connection.init()
|
||||
}
|
||||
|
||||
isOpen() {
|
||||
if(this.connection.checkOpen()) {
|
||||
return true;
|
||||
|
||||
1
ui/_/code/ws/shim/fs.js
Normal file
1
ui/_/code/ws/shim/fs.js
Normal file
@@ -0,0 +1 @@
|
||||
export default {}
|
||||
Reference in New Issue
Block a user