nativemessaging

package module
v0.0.0-...-ccb6071 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: MIT Imports: 6 Imported by: 23

README

nativemessaging

Native messaging host library for go application

Usage

Go host application

package main

import (
	"io"
	"os"

	"github.com/qrtz/nativemessaging"
)

func main() {
	decoder := nativemessaging.NewNativeJSONDecoder(os.Stdin)
	encoder := nativemessaging.NewNativeJSONEncoder(os.Stdout)

	for {
		var rsp response
		var msg message
		err := decoder.Decode(&msg)

		if err != nil {
			if err == io.EOF {
				// exit
				return
			}
			rsp.Text = err.Error()
		} else {
			if msg.Text == "ping" {
				rsp.Text = "pong"
				rsp.Success = true
			} else {
				// Echo the message back to the client
				rsp.Text = msg.Text
			}
		}

		if err := encoder.Encode(rsp); err != nil {
			// Log the error and exit
			return
		}
	}
}

type message struct {
	Text string `json:"text"`
}

type response struct {
	Text    string `json:"text"`
	Success bool   `json:"success"`
}

Javascript client

    chrome.runtime.sendNativeMessage('com.github.qrtz.nativemessaginghost', {text:'ping'}, (response) => {
        console.log('Native messaging host response ', response);
    })

More info:

https://developer.chrome.com/extensions/nativeMessaging
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrInvalidMessageSize unable to read message size
	ErrInvalidMessageSize = errors.New("Invalid message size")
	// ErrByteOrderNotSet byter order is set on the first read
	ErrByteOrderNotSet = errors.New("Byte order not set")
)
View Source
var NativeEndian = binary.LittleEndian

NativeEndian system native byte order

Functions

func Decode

func Decode(r io.Reader, v interface{}, order binary.ByteOrder) error

Decode parses the incoming JSON-encoded data and stores the result in the value pointed to by v. The leading first 4 bytes of the data is interpreted as a 32-bit unsigned integer encoded in the specified byte order

func Encode

func Encode(w io.Writer, v interface{}, order binary.ByteOrder) (int, error)

Encode writes to io.Writer the json encoded data of the given value The data is preceded with a 32-bit unsigned integer data length in the specified byte order

func Read

func Read(r io.Reader, order binary.ByteOrder) ([]byte, error)

Read reads a message from the given io.Reader interpreting the leading first 4 bytes as a 32-bit unsigned integer encoded in the specified byte order

func Write

func Write(w io.Writer, message io.Reader, order binary.ByteOrder) (i int, err error)

Write writes to io.Writer the data read from the given io.Reader The data is preceded with a 32-bit unsigned integer data length in the specified byte order

Types

type JSONDecoder

type JSONDecoder interface {
	Decode(interface{}) error
}

JSONDecoder reads and decodes JSON values from an input stream.

func NewJSONDecoder

func NewJSONDecoder(r io.Reader, bo binary.ByteOrder) JSONDecoder

NewJSONDecoder returns a new jsonDecoder that reads from the given io.Reader interpreting the first 4 bytes as 32-bit data length in the specified byte order

func NewNativeJSONDecoder

func NewNativeJSONDecoder(r io.Reader) JSONDecoder

NewNativeJSONDecoder returns a new jsonDecoder that reads from the given io.Reader interpreting the first 4 bytes as 32-bit data length in native byte order

type JSONEncoder

type JSONEncoder interface {
	Encode(interface{}) error
}

JSONEncoder writes JSON values to an output stream.

func NewJSONEncoder

func NewJSONEncoder(w io.Writer, bo binary.ByteOrder) JSONEncoder

NewJSONEncoder returns a new jsonEncoder that write to the given io.Writer The data is preceded with 32-bit data length in the specified byte order

func NewNativeJSONEncoder

func NewNativeJSONEncoder(w io.Writer) JSONEncoder

NewNativeJSONEncoder returns a new jsonEncoder that writes to the given io.Writer The data is preceded with 32-bit data length in native byte order

type Reader

type Reader interface {
	// Read reads bytes from an io.Reader.
	// It returns the bytes read and an error from the read operation.
	// Read may return io.EOF when the underlying stream reach the end or is closed
	Read() ([]byte, error)
}

Reader is an interface that wraps the Read method.

func NewNativeReader

func NewNativeReader(r io.Reader) Reader

NewNativeReader returns a new reader that reads from the given io.Reader interpreting the first 4 bytes as 32-bit data length in native byte order

func NewReader

func NewReader(r io.Reader, bo binary.ByteOrder) Reader

NewReader returns a new reader that reads from the given io.Reader interpreting the first 4 bytes as 32-bit data length in the specified byte order

type Writer

type Writer interface {
	// Write writes bytes from the given io.Reader to the underlying io.Writer
	// It returns the number of bytes from io.Reader and any error from the write operation.
	Write(io.Reader) (int, error)
}

Writer is an interface that wraps the Write method.

func NewNativeWriter

func NewNativeWriter(w io.Writer) Writer

NewNativeWriter returns a new writer that writes to the given io.Writer The data is preceded with 32-bit data length in native byte order

func NewWriter

func NewWriter(w io.Writer, bo binary.ByteOrder) Writer

NewWriter returns a new writer that writes to w The data is preceded with 32-bit data length in the specified byte order

Jump to

Keyboard shortcuts

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