wsrpc

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

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

Go to latest
Published: Jul 19, 2023 License: MIT Imports: 4 Imported by: 0

README

wsrpc

a tiny websocket rpc framework.

I like it because of its tiny and exquisite.

Uasage

define service
type Args struct {
	A, B int
}

type Arith struct{}

func (a *Arith) Mul(args *Args, reply *int) error {
	*reply = args.A * args.B
	return nil
}

func (a *Arith) Add(args *Args, reply *int) error {
	*reply = args.A + args.B
	return nil
}
start a server
server := NewServer(":8972", "/ws")
server.Register(&Arith{})
go server.Serve()
defer server.Close()
...
start a client
client, err := NewClient("http://localhost:8972", "ws://localhost:8972/ws")
if err != {
    panic(err)
}
defer client.Close()

var reply int
err = client.Call("Arith.Mul", &Args{2, 3}, &reply)
...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WSRPCClient

type WSRPCClient struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

WSRPCClient is a websocket based rpc client.

func NewClient

func NewClient(origin, wsAddr string) (*WSRPCClient, error)

NewClient returns a new WSRPCClient.

func (*WSRPCClient) Call

func (c *WSRPCClient) Call(serviceMethod string, args interface{}, reply interface{}) error

Call invokes the named function, waits for it to complete, and returns its error status.

func (*WSRPCClient) Close

func (c *WSRPCClient) Close() error

Close closes the client connection.

func (*WSRPCClient) Go

func (c *WSRPCClient) Go(serviceMethod string, args interface{}, reply interface{}) *rpc.Call

Go invokes the function asynchronously.

type WSRPCServer

type WSRPCServer struct {
	RPCCodecFunc rpc.ServerCodec
	// contains filtered or unexported fields
}

WSRPCServer is a websocket based rpc server.

func NewServer

func NewServer(addr, wsPath string) *WSRPCServer

NewServer returns a new WSRPCServer.

func (*WSRPCServer) Close

func (s *WSRPCServer) Close() error

Close closes the server.

func (*WSRPCServer) Mux

func (s *WSRPCServer) Mux() *http.ServeMux

Mux returns the http.ServeMux. You can use this mux to config more routers.

func (*WSRPCServer) Register

func (s *WSRPCServer) Register(service interface{})

Register register RPC services.

func (*WSRPCServer) Serve

func (s *WSRPCServer) Serve() error

Serve serves the server. It starts a http server and handle ws requests at path "/ws".

func (*WSRPCServer) ServeHTTP

func (s *WSRPCServer) ServeHTTP(w http.ResponseWriter, req *http.Request)

Jump to

Keyboard shortcuts

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