Skip to content

Instantly share code, notes, and snippets.

@mbonell
Last active January 5, 2018 23:09
Show Gist options
  • Save mbonell/5f0674de16c883c932c519225639077c to your computer and use it in GitHub Desktop.
Save mbonell/5f0674de16c883c932c519225639077c to your computer and use it in GitHub Desktop.
RPC client implementation for https://github.com/mbonell/simple-rpc-server
package main
import (
"log"
"net/http"
"net/rpc"
"github.com/mbonell/simple-rpc-server/models"
)
type Launcher struct {
Client *rpc.Client
}
func (l *Launcher) SendRequest(rq *models.Request) models.Response {
var rp models.Response
err := l.Client.Call("Service.Execute", rq, &rp)
if err != nil {
log.Fatal("Service error: ", err)
}
return rp
}
func main() {
var response models.Response
client, err := rpc.DialHTTP("tcp", "127.0.0.1:8767")
if err != nil {
log.Fatal("Dialing:", err)
}
l := &Launcher{Client: client}
// Use "FunctionWithExpectedError" to get an error from the call otherwise
// you will get a valid result
request := &models.Request{
Function: "FunctionWithExpectedError",
}
response = l.SendRequest(request)
log.Println(response)
if response.Code == http.StatusOK {
log.Println(string(response.Msg))
} else {
log.Println("Error from server call: " + response.Error.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment