package
1.9.8
Repository: https://github.com/lockp111/toolkit.git
Documentation: pkg.go.dev

# README

ws-jsonrpc2 examples

server

func rpcInit() {
    var (
		router = gin.New()
		rpcSrv = rpc.NewServer()
	)

	router.Use(cors.New(cors.Config{
		AllowMethods: []string{"OPTIONS", "POST", "GET"},
		AllowHeaders: []string{"Origin", "X-Requested-With",
			"Content-Type", "Accept"},
		AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			return true
		},
	}))

    router.GET("/rpc", func(c *gin.Context) {
        serveRPC(c, rpcSrv)
    })

	rpcSrv.RegisterName("User", &User{})
	rpcSrv.OnMissingMethod(backendHandler)
}

func serveRPC(c *gin.Context, rpcSrv ...*rpc.Server) {
	upgrader := websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}

	ws, err := upgrader.Upgrade(c.Writer, c.Request, c.Header)
	if err != nil {
		log.WithError(err).Warn("Jsonrpc upgrade failed.")
		return
	}

	rpc.ServeRPCwithInit(c.Request, ws, func(conn *rpc.Conn) {
		// init connect handler
	}, rpcSrv...)
}

type User struct{}

func (u *User) Login(conn *rpc.Conn, req *pbu.LoginReq, rsp *pbu.LoginRsp) error {
    // login handler
	return nil
}

func backendHandler (conn *rpc.Conn, method string, args json.RawMessage) (rsp interface{},err error) {
    // if missing method then run this handler
    return
}

client body

{
    "id": 1,
    "jsonrpc": "2.0",
    "method": "User.Login",
    "params": {}
}

# Functions

DebugLog open debuglog.
NewConn ...
NewNotifier ...
NewServer returns a new Server.
NewServerCodec returns a new ServerCodec using JSON-RPC on conn.
Register publishes the receiver's methods in the DefaultServer.
RegisterName is like Register but uses the provided name for the type instead of the receiver's concrete type.
ServeCodec is like ServeConn but uses the specified codec to decode requests and encode responses.
ServeRPC ...
ServeRPCwithInit ...

# Constants

No description provided by the author
Defaults used by HandleHTTP.

# Variables

DefaultServer is the default instance of *Server.

# Structs

Args for Call.
Conn ...
No description provided by the author
Notifier ...
ReadWriteCloser ...
Request is a header written before every RPC call.
Response is a header written before every RPC return.
Server represents an RPC Server.

# Interfaces

A ServerCodec implements reading of RPC requests and writing of RPC responses for the server side of an RPC conn.

# Type aliases

ConnCloseHandler ...
No description provided by the author
conn, method, params.
No description provided by the author
No description provided by the author