tauri-apps / tauri-invoke-http

A custom invoke system for Tauri that leverages a localhost server
Apache License 2.0
48 stars 5 forks source link

I couldn't get it to work. #6

Closed marcelodelta closed 8 months ago

marcelodelta commented 1 year ago

Cool idea. I couldn't get it to work.

Could you help?

Error: Couldn't connect to server

Can't shake hands

Thanks for listening

Tauri config

{
    "$schema": "../node_modules/@tauri-apps/cli/schema.json",
    "build": {
        "beforeBuildCommand": "npm run build",
        "beforeDevCommand": "npm run dev",
        "devPath": "http://localhost:3000",
        "distDir": "../.output/public",
        "withGlobalTauri": true
    },
    "package": {
        "productName": "royal",
        "version": "0.1.0"
    },
    "tauri": {
        "allowlist": {
            "all": true
        },
        "bundle": {
            "active": true,
            "category": "DeveloperTool",
            "copyright": "",
            "deb": {
                "depends": []
            },
            "externalBin": [],
            "icon": [
                "icons/32x32.png",
                "icons/128x128.png",
                "icons/128x128@2x.png",
                "icons/icon.icns",
                "icons/icon.ico"
            ],
            "identifier": "royal.custom.bundleid",
            "longDescription": "",
            "macOS": {
                "entitlements": null,
                "exceptionDomain": "",
                "frameworks": [],
                "providerShortName": null,
                "signingIdentity": null
            },
            "resources": [],
            "shortDescription": "",
            "targets": "all",
            "windows": {
                "certificateThumbprint": null,
                "digestAlgorithm": "sha256",
                "timestampUrl": ""
            }
        },
        "security": {
            "csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'"
        },
        "updater": {
            "active": false
        },
        "windows": [
            {
                "fullscreen": false,
                "height": 600,
                "resizable": true,
                "title": "Royal",
                "width": 800
            }
        ]
    }
}

Cargo

[package]
name = "app"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
default-run = "app"
edition = "2021"
rust-version = "1.62.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
tauri-build = { version = "1.0.4", features = [] }

[dependencies]
serde_json = "1.0"
tauri-invoke-http = { git = "https://github.com/tauri-apps/tauri-invoke-http", branch = "dev" }
serde = { version = "1.0.140", features = ["derive"] }
tauri = { version = "1.0.5", features = ["api-all"] }

[features]
# by default Tauri runs in production mode
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL
default = [ "custom-protocol" ]
# this feature is used used for production builds where `devPath` points to the filesystem
# DO NOT remove this
custom-protocol = [ "tauri/custom-protocol" ]

Main

#![cfg_attr(
    all(not(debug_assertions), target_os = "windows"),
    windows_subsystem = "windows"
)]

#[tauri::command]
fn my_command(args: u64) -> Result<String, ()> {
    println!("executed command with args {:?}", args);
    Ok("executed".into())
}

fn main() {
    let http = tauri_invoke_http::Invoke::new(["tauri://localhost", "http://localhost:8080"]);

    tauri::Builder::default()
        .invoke_system(http.initialization_script(), http.responder())
        .setup(move |app| {
            http.start(app.handle());
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![my_command])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

javascript

<script setup>
import { invoke } from '@tauri-apps/api/tauri'

invoke('my_command', { args: 5 }).then(data => {
  console.log(data)
}).catch(error => {
  console.log(error)
})
</script>
lucasfernog commented 1 year ago

Can you share a repo we can use to reproduce? Also which OS are you running?

FabianLars commented 8 months ago

Closing due to inactivity. If someone faces a similar issue, please open a new issue,