nativemessaging

package module
v0.0.0-...-1cef683 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2022 License: MIT Imports: 7 Imported by: 0

README

go-native-messaging

forthebadge forthebadge

GitHub license

Built with ❤︎ by LegendaryB

Native Messaging module powered by Go.


📝 Requirements

  • Read and understand this.

🚀 How to use it?

Receiving a byte array

package main

import (
    "io"
    "github.com/LegendaryB/go-native-messaging"
)

func main() {
    host := nativemessaging.NewNativeMessagingHost(nil)

    for {
        b, err := host.ReadBytes()
	
        if err != nil {
	    if err == io.EOF {
		break
	    }
	}
	
	// just echo the received message back to the extension
	host.Write(msg)
    }
}

Note: If you need to write a plain byte array by yourself you can use the following method:
func (host *NativeMessagingHost) WriteBytes(bytes []byte) error

Receiving a complex object

package main

import (
    "io"
    "github.com/LegendaryB/go-native-messaging"
)

type Message struct {
    Value string `json:"value"`
}

func main() {
    host := nativemessaging.NewNativeMessagingHost(nil)
    msg := &Message{}

    for {
        if err := host.Read(msg); err != nil {
	    if err == io.EOF {
		break
	    }
	}

	// just echo the received message back to the extension
	host.Write(msg)
    }
}

Sending a byte array

See last line in section Receiving a byte array.

Sending a complex object

See last line in section Receiving a complex object.

⚙️ Debugging

In the samples above we were passing a nil to the NewNativeMessagingHost method. If you need to receive the error messages you can pass in a io.Writer instance. If the parameter is nil the error messages are written to os.stderr.

Redirecting error messages to a file

package main

import (
    "io"
    "os"
    "github.com/LegendaryB/go-native-messaging"
)

func main() {
    f, _ := os.Create("stderr.txt")
    host := nativemessaging.NewNativeMessagingHost(f)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NativeMessagingHost

type NativeMessagingHost struct {
	// contains filtered or unexported fields
}

func NewNativeMessagingHost

func NewNativeMessagingHost(stderr io.Writer) *NativeMessagingHost

func (*NativeMessagingHost) Read

func (host *NativeMessagingHost) Read(v interface{}) error

func (*NativeMessagingHost) ReadBytes

func (host *NativeMessagingHost) ReadBytes() ([]byte, error)

func (*NativeMessagingHost) Write

func (host *NativeMessagingHost) Write(v interface{}) error

func (*NativeMessagingHost) WriteBytes

func (host *NativeMessagingHost) WriteBytes(bytes []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL