Skip to content

Instantly share code, notes, and snippets.

@maxekman
Last active December 23, 2015 10:19
Show Gist options
  • Save maxekman/6620567 to your computer and use it in GitHub Desktop.
Save maxekman/6620567 to your computer and use it in GitHub Desktop.
Example of how to use msgpack-rpc functionality in github.com/ugorji/go/codec
package client
import (
"fmt"
"net"
"net/rpc"
"github.com/ugorji/go/codec"
)
func main() {
var mh MsgpackHandle
conn, err := net.Dial("tcp", "localhost:5555")
if err != nil {
panic(err)
}
rpcCodec := MsgpackSpecRpc.ClientCodec(conn, &mh)
client := rpc.NewClientWithCodec(rpcCodec)
var reply string
err = client.Call("TestServer.Test", "", &reply)
if err != nil {
panic(err)
}
fmt.Println(reply)
// Output: reply
}
package server
import (
"fmt"
"net"
"net/rpc"
"github.com/ugorji/go/codec"
)
type TestServer struct{}
func (t *TestServer) Test(args *string, reply *string) error {
*reply = "reply"
return nil
}
func main() {
var mh codec.MsgpackHandle
testServer := new(TestServer)
rpc.Register(testServer)
listener, err := net.Listen("tcp", ":5555")
if err != nil {
panic(err)
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
panic(err)
}
rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, &mh)
go rpc.ServeCodec(rpcCodec)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment