a43d183b4f
* Compiles but runtime time fails * wip * Beginning of clean-up - creation of config to keep things together * Removed unused module * Removed hardcoded constants * Easier way of sending binary messages * WIP cleanup before machine switch * Upgrade wasm-bindgen to 0.2.83 * Fixed compilation warnings for wasm client * all clients compiling without warnings * disabling topology refresh in wasm * Added a config option to disable loop cover traffic stream * config changes * Make webassembly work in a web worker - `wasm-timer` modified to work in web worker - add worker target to webpack - add client to call from HTML - update README to build WASM for bundling (this does not build ES modules) * Restored topology refreshing * correctly polling items in the wasm delay_queue * Allow client to read up to 8 messages at once from gateway connection (#1669) * Allow client to read up to 8 messages at once from gateway connection * Importing tokio::select in wasm32 target * Updated changelog * missing imports * Introduced disable_main_poisson_packet_distribution to force real_traffic_stream to disable poisson sending (#1664) * Introduced disable_main_poisson_packet_distribution to force real_traffic_stream to disable poisson sending * Updated changelog * Adjusting default settings * Introduced a client-configurable option to force it to use extended packet size * local adjustments * Removed warning associated with receiving extended packets * Minimal v2-required changes * Updated changelog * explicitly allowing clippy drop_non_drop Co-authored-by: Mark Sinclair <mmsinclair@gmail.com>
131 lines
4.3 KiB
JavaScript
131 lines
4.3 KiB
JavaScript
// Copyright 2020-2022 Nym Technologies SA
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
importScripts('nym_client_wasm.js');
|
|
|
|
console.log('Initializing worker');
|
|
|
|
// wasm_bindgen creates a global variable (with the exports attached) that is in scope after `importScripts`
|
|
const { default_debug, get_gateway, NymClient, set_panic_hook, Config } = wasm_bindgen;
|
|
|
|
class ClientWrapper {
|
|
constructor(config, onMessageHandler) {
|
|
this.rustClient = new NymClient(config);
|
|
this.rustClient.set_on_message(onMessageHandler);
|
|
this.rustClient.set_on_gateway_connect(this.onConnect);
|
|
}
|
|
|
|
selfAddress = () => {
|
|
return this.rustClient.self_address();
|
|
};
|
|
|
|
onConnect = () => {
|
|
console.log('Established (and authenticated) gateway connection!');
|
|
};
|
|
|
|
start = async () => {
|
|
// this is current limitation of wasm in rust - for async methods you can't take self by reference...
|
|
// I'm trying to figure out if I can somehow hack my way around it, but for time being you have to re-assign
|
|
// the object (it's the same one)
|
|
this.rustClient = await this.rustClient.start();
|
|
};
|
|
|
|
sendMessage = async (recipient, message) => {
|
|
this.rustClient = await this.rustClient.send_message(recipient, message);
|
|
};
|
|
|
|
sendBinaryMessage = async (recipient, message) => {
|
|
this.rustClient = await this.rustClient.send_binary_message(recipient, message);
|
|
};
|
|
}
|
|
|
|
let client = null;
|
|
|
|
async function main() {
|
|
// load WASM package
|
|
await wasm_bindgen('nym_client_wasm_bg.wasm');
|
|
|
|
console.log('Loaded WASM');
|
|
|
|
// sets up better stack traces in case of in-rust panics
|
|
set_panic_hook();
|
|
|
|
console.error("the current mainnet is not compatible with v2! - either use the pre-merge branch or explicitly set the client to use one of V2 QA networks")
|
|
return
|
|
|
|
// validator server we will use to get topology from
|
|
// MAINNET (V1):
|
|
const validator = 'https://validator.nymtech.net/api'; //"http://localhost:8081";
|
|
const preferredGateway = 'E3mvZTHQCdBvhfr178Swx9g4QG3kkRUun7YnToLMcMbM';
|
|
// QA (V2):
|
|
// const validator = 'https://qa-validator-api.nymtech.net/api'; //"http://localhost:8081";
|
|
// const preferredGateway = 'CgQrYP8etksSBf4nALNqp93SHPpgFwEUyTsjBNNLj5WM';
|
|
|
|
const gatewayEndpoint = await get_gateway(validator, preferredGateway);
|
|
|
|
// only really useful if you want to adjust some settings like traffic rate
|
|
// (if not needed you can just pass a null)
|
|
const debug = default_debug();
|
|
|
|
debug.disable_main_poisson_packet_distribution = true;
|
|
debug.disable_loop_cover_traffic_stream = true;
|
|
debug.use_extended_packet_size = true;
|
|
// debug.average_packet_delay_ms = BigInt(10);
|
|
// debug.average_ack_delay_ms = BigInt(10);
|
|
// debug.ack_wait_addition_ms = BigInt(3000);
|
|
// debug.ack_wait_multiplier = 10;
|
|
|
|
debug.topology_refresh_rate_ms = BigInt(60000)
|
|
|
|
const config = new Config('my-awesome-wasm-client', validator, gatewayEndpoint, debug);
|
|
|
|
const onMessageHandler = (message) => {
|
|
self.postMessage({
|
|
kind: 'ReceiveMessage',
|
|
args: {
|
|
message,
|
|
},
|
|
});
|
|
};
|
|
|
|
console.log('Instantiating WASM client...');
|
|
client = new ClientWrapper(config, onMessageHandler);
|
|
console.log('Web worker creating WASM client...');
|
|
await client.start();
|
|
console.log('WASM client running!');
|
|
|
|
const selfAddress = client.rustClient.self_address();
|
|
console.log(`Client address is ${selfAddress}`);
|
|
self.postMessage({
|
|
kind: 'Ready',
|
|
args: {
|
|
selfAddress,
|
|
},
|
|
});
|
|
|
|
// Set callback to handle messages passed to the worker.
|
|
self.onmessage = async event => {
|
|
if (event.data && event.data.kind) {
|
|
switch (event.data.kind) {
|
|
case 'SendMessage': {
|
|
const { message, recipient } = event.data.args;
|
|
await client.sendMessage(message, recipient);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
// Let's get started!
|
|
main(); |