mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-04 20:41:56 +08:00
88 lines
2.7 KiB
Markdown
88 lines
2.7 KiB
Markdown
## Bindings
|
|
|
|
Bindings work in a similar way to v2, by providing a means to bind struct
|
|
methods to the frontend. These can be called in the frontend using the binding
|
|
wrappers generated by the `wails3 generate bindings` command:
|
|
|
|
```javascript
|
|
// @ts-check
|
|
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
|
|
// This file is automatically generated. DO NOT EDIT
|
|
|
|
import { main } from "./models";
|
|
|
|
window.go = window.go || {};
|
|
window.go.main = {
|
|
GreetService: {
|
|
/**
|
|
* GreetService.Greet
|
|
* Greet greets a person
|
|
* @param name {string}
|
|
* @returns {Promise<string>}
|
|
**/
|
|
Greet: function (name) {
|
|
wails.CallByID(1411160069, ...Array.prototype.slice.call(arguments, 0));
|
|
},
|
|
|
|
/**
|
|
* GreetService.GreetPerson
|
|
* GreetPerson greets a person
|
|
* @param person {main.Person}
|
|
* @returns {Promise<string>}
|
|
**/
|
|
GreetPerson: function (person) {
|
|
wails.CallByID(4021313248, ...Array.prototype.slice.call(arguments, 0));
|
|
},
|
|
},
|
|
};
|
|
```
|
|
|
|
Bound methods are obfuscated by default, and are identified using uint32 IDs,
|
|
calculated using the
|
|
[FNV hashing algorithm](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function).
|
|
This is to prevent the method name from being exposed in production builds. In
|
|
debug mode, the method IDs are logged along with the calculated ID of the method
|
|
to aid in debugging. If you wish to add an extra layer of obfuscation, you can
|
|
use the `BindAliases` option. This allows you to specify a map of alias IDs to
|
|
method IDs. When the frontend calls a method using an ID, the method ID will be
|
|
looked up in the alias map first for a match. If it does not find it, it assumes
|
|
it's a standard method ID and tries to find the method in the usual way.
|
|
|
|
Example:
|
|
|
|
```go
|
|
app := application.New(application.Options{
|
|
Bind: []any{
|
|
&GreetService{},
|
|
},
|
|
BindAliases: map[uint32]uint32{
|
|
1: 1411160069,
|
|
2: 4021313248,
|
|
},
|
|
Assets: application.AssetOptions{
|
|
Handler: application.AssetFileServerFS(assets),
|
|
},
|
|
Mac: application.MacOptions{
|
|
ApplicationShouldTerminateAfterLastWindowClosed: true,
|
|
},
|
|
})
|
|
```
|
|
|
|
We can now call using this alias in the frontend: `wails.Call(1, "world!")`.
|
|
|
|
### Insecure calls
|
|
|
|
If you don't mind your calls being available in plain text in your binary and
|
|
have no intention of using [garble](https://github.com/burrowers/garble), then
|
|
you can use the insecure `wails.CallByName()` method. This method takes the
|
|
fully qualified name of the method to call and the arguments to pass to it.
|
|
Example:
|
|
|
|
```go
|
|
wails.CallByName("main.GreetService.Greet", "world!")
|
|
```
|
|
|
|
!!! danger
|
|
|
|
This is only provided as a convenience method for development. It is not recommended to use this in production.
|