@hivescale/connect-uniapp
v0.0.1
Published
HiveScale application-scoped network client for uni-app
Readme
HiveScale uni-app Connect SDK
@hivescale/connect-uniapp is an application-scoped adapter for uni-app. It
does not install a system VPN or change DNS from JavaScript.
Runtime behavior
- H5 delegates HTTP/HTTPS and MagicDNS to
@hivescale/connect. - uni-app App delegates HTTP/HTTPS, TCP, and UDP to a native plugin through a small JSON/event protocol.
- H5 cannot expose raw TCP or UDP because browser JavaScript has no raw socket
API.
dialTcpanddialUdpreturnUNSUPPORTED_RUNTIMEon H5.
Install
npm install @hivescale/connect-uniappFor H5, also install the web SDK:
npm install @hivescale/connectH5
import { createClient } from '@hivescale/connect-uniapp';
const client = createClient({
runtime: 'h5',
authKey: shortLivedAuthKey,
controlURL: 'https://hs-ctl.hivescale.net',
});
await client.connect();
const response = await client.fetch('http://fileserver:8080/health');
console.log(await response.text());
await client.close();App native plugin
The native plugin is obtained with the normal uni-app plugin mechanism and is
adapted with createUniAppNativeBridge:
import { createClient, createUniAppNativeBridge } from '@hivescale/connect-uniapp';
const plugin = uni.requireNativePlugin('HiveScaleConnect');
const client = createClient({
runtime: 'app',
nativeBridge: createUniAppNativeBridge(plugin),
authKey: shortLivedAuthKey,
hostname: 'my-uniapp-device',
});
await client.connect();
const response = await client.fetch('http://fileserver:8080/health');
const tcp = await client.dialTcp('dbserver', 5432);
tcp.on('data', (data) => console.log(data));
await tcp.write(new TextEncoder().encode('ping'));
const udp = await client.dialUdp('metrics', 8125);
udp.on('message', (data) => console.log(data));
await udp.send(new TextEncoder().encode('metric'));
await client.close();The plugin must implement connect, fetch, dialTcp, writeTcp,
closeTcp, dialUdp, sendUdp, closeUdp, and close. It must also expose a
subscribe(callback) method and optionally unsubscribe(). Each operation
uses the uni-app callback form (arguments, successCallback, failureCallback);
the callback result is either the operation result or { ok: false, code,
message }. It must call the event callback with tcpData, tcpClosed,
tcpError, udpMessage, udpClosed, and udpError events. Payloads are
base64 encoded in dataBase64; connection IDs are returned as { id: string }.
For Android App plugins, the reusable implementation is in
libs/mobile-connect/android/src/main/kotlin/net/hivescale/connect/android/:
HiveScaleMobileConnectManager owns the Go userspace node and
HiveScaleUniAppNativeBridge maps the JavaScript method names to it. The
libs/uniapp-connect/native/android contains the DCloud UniModule wrapper;
copy it into the native plugin project, add the DCloud plugin SDK and generated
hivescale-mobile-connect.aar, then register HiveScaleConnectModule under
the HiveScaleConnect plugin name. The wrapper does not request VpnService.
AuthKeys should be short-lived and issued by a trusted service. Do not embed an administrator key in a uni-app bundle.
