Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow protocol extensions by exposing server.Notify and client.Handler #53

Open
qvalentin opened this issue Nov 3, 2024 · 0 comments
Open

Comments

@qvalentin
Copy link

Thanks for this awesome package.

One thing I'm currently missing is the ability to use protocol extensions.

  1. One can already call custom server methods with the Request(ctx context.Context, method string, params interface{}) (result interface{}, err error) function. But custom Notifications are not supported by the server interface.

  2. Also the client implementation (NewClient) does not allow adding a custom fallback handler but will just use jsonrpc2.MethodNotFoundHandler.

My current workaround is:

  1. Store the jsonrpc2.Conn when creating the server and call Notifiy directly on it.
  2. A custom NewClient method, that includes a custom handler:
// CustomNewClient returns the context in which Client is embedded, jsonrpc2.Conn, and the Server.
func (yamllsConnector Connector) CustomNewClient(ctx context.Context, client protocol.Client, stream jsonrpc2.Stream, logger *zap.Logger) (context.Context, jsonrpc2.Conn, protocol.Server) {
	ctx = protocol.WithClient(ctx, client)

	conn := jsonrpc2.NewConn(stream)
	conn.Go(ctx,
		protocol.Handlers(
			protocol.ClientHandler(client, yamllsConnector.CustomHandler),
		),
	)
	server := protocol.ServerDispatcher(conn, logger.Named("server"))

	return ctx, conn, server
}

func (yamllsConnector Connector) CustomHandler(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
	switch req.Method() {
	case "custom/schema/request":
		return reply(ctx, nil, nil)
	}

	return jsonrpc2.MethodNotFoundHandler(ctx, reply, req)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant