Last active
January 5, 2018 23:09
-
-
Save mbonell/5f0674de16c883c932c519225639077c to your computer and use it in GitHub Desktop.
RPC client implementation for https://github.com/mbonell/simple-rpc-server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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