本文整理汇总了Golang中net/rpc/jsonrpc.NewServerCodec函数的典型用法代码示例。如果您正苦于以下问题:Golang NewServerCodec函数的具体用法?Golang NewServerCodec怎么用?Golang NewServerCodec使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewServerCodec函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
bind_addr := flag.String("bind_ip", "127.0.0.1", "bind ip address")
http_port := flag.Int("http_port", 9999, "listen http port")
rpc_port := flag.Int("rpc_port", 9998, "listen rpc port")
flag.Parse()
go func() {
addr, _ := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", *bind_addr, *rpc_port))
listener, _ := net.ListenTCP("tcp", addr)
rpcservice := new(RPCService)
rpc.Register(rpcservice)
rpc.HandleHTTP()
for {
conn, _ := listener.Accept()
go rpc.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}()
web.Get("/api/topics/([a-zA-Z0-9_\\-]+)/subscribers/([a-zA-Z0-9_\\-]+)/messages", APIGetTopicMessages)
web.Post("/api/topics/([a-zA-Z0-9_\\-]+)/subscribers/([a-zA-Z0-9_\\-]+)/messages", APIPostTopicMessage)
web.Get("/api/topics/([a-zA-Z0-9_\\-]+)", APIGetTopic)
web.Post("/api/topics/([a-zA-Z0-9_\\-]+)", APIUpdateTopic)
//web.Get("/api/topics", APIGetTopics)
web.Get("/api/subscribers/([a-zA-Z0-9_\\-]+)", APIGetSubscriber)
web.Post("/api/subscribers/([a-zA-Z0-9_\\-]+)", APIUpdateSubscriber)
//web.Get("/api/topics/(.+)/subscribers/(.+)", APIGetTopicSubscriber)
//web.Get("/api/topics/(.+)/subscribers", APIGetTopicSubscribers)
web.Run(fmt.Sprintf("%s:%d", *bind_addr, *http_port))
}
示例2: TestWebSocket_Deliver
func TestWebSocket_Deliver(t *testing.T) {
mux := http.NewServeMux()
h := Handler{Tx: &smpp.Transceiver{Addr: ":0"}}
<-h.Register(mux)
s := httptest.NewServer(mux)
defer s.Close()
u := strings.Replace(s.URL, "http:", "ws:", 1)
ws, err := websocket.Dial(u+"/v1/ws/jsonrpc/events", "", "http://localhost")
if err != nil {
t.Fatal(err)
}
defer ws.Close()
h.pool.Broadcast(&DeliveryReceipt{
Src: "bart",
Dst: "lisa",
Text: "delivery receipt here",
})
srv := rpc.NewServer()
NewSM(h.Tx, srv)
go func() {
deliverErr <- srv.ServeRequest(jsonrpc.NewServerCodec(ws))
}()
select {
case err = <-deliverErr:
if err != nil {
t.Fatal(err)
}
case <-time.After(2 * time.Second):
t.Fatal("timeout waiting for delivery receipt")
}
}
示例3: StartRpc
func StartRpc() {
if !g.Config().Rpc.Enabled {
return
}
addr := g.Config().Rpc.Listen
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
log.Fatalf("net.ResolveTCPAddr fail: %s", err)
}
listener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Fatalf("listen %s fail: %s", addr, err)
} else {
log.Println("rpc listening", addr)
}
server := rpc.NewServer()
server.Register(new(Transfer))
for {
conn, err := listener.Accept()
if err != nil {
log.Println("listener.Accept occur error:", err)
continue
}
// go rpc.ServeConn(conn)
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
示例4: InitJsonRpcServer
func InitJsonRpcServer(rcvr interface{}) {
server := rpc.NewServer()
server.Register(rcvr)
listener, e := net.Listen("tcp", ":7498")
if e != nil {
log.Fatal("listen error:", e)
}
defer listener.Close()
RpcHandler := func(w http.ResponseWriter, r *http.Request) {
serverCodec := jsonrpc.NewServerCodec(&HttpConn{in: r.Body, out: w})
w.Header().Set("Content-type", "application/json")
w.WriteHeader(200)
err := server.ServeRequest(serverCodec)
if err != nil {
log.Printf("Error while serving JSON request: %v", err)
http.Error(w, "Error while serving JSON request, details have been logged.", 500)
return
}
}
r := mux.NewRouter()
r.HandleFunc("/rpc", RpcHandler)
go http.Serve(listener, r)
}
示例5: main
func main() {
var err error
clientApp := exec.Command("./client-example", "")
rwc := new(ReadWriteCloser)
rwc.WriteCloser, err = clientApp.StdinPipe()
if err != nil {
log.Fatal(err)
}
rwc.ReadCloser, err = clientApp.StdoutPipe()
if err != nil {
log.Fatal(err)
}
serv := rpc.NewServer()
codec := jsonrpc.NewServerCodec(rwc)
fmt.Println("Made RPC server")
m := new(Adder)
serv.Register(m)
fmt.Println("Registered adder service")
err = clientApp.Start()
if err != nil {
log.Fatal(err)
}
go serv.ServeCodec(codec)
clientApp.Wait()
fmt.Printf("Adder has been called %d times and is now: %d\n", no, *m)
}
示例6: main
func main() {
vtrade := new(VirtualTradingPlatform)
vtrade.tradeid = 0
server := rpc.NewServer()
server.Register(vtrade)
server.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
listener, e := net.Listen("tcp", ":1234")
if e != nil {
fmt.Println("listen error:", e)
return
}
fmt.Println("Server started")
for {
if conn, err := listener.Accept(); err != nil {
fmt.Println("accept error: " + err.Error())
log.Fatal("accept error: " + err.Error())
} else {
log.Printf("new connection established\n")
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
}
示例7: startRPC
func (d *daemon) startRPC() {
if options.DebugPort > 0 {
go func() {
if err := http.ListenAndServe(fmt.Sprintf(":%d", options.DebugPort), nil); err != nil {
glog.Errorf("Unable to bind to debug port %s. Is another instance running?", err)
return
}
}()
}
listener, err := net.Listen("tcp", options.Listen)
if err != nil {
glog.Fatalf("Unable to bind to port %s. Is another instance running?")
}
rpcutils.SetDialTimeout(options.RPCDialTimeout)
d.rpcServer.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
glog.V(0).Infof("Listening on %s", listener.Addr().String())
go func() {
for {
conn, err := listener.Accept()
if err != nil {
glog.Fatalf("Error accepting connections: %s", err)
}
go d.rpcServer.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}()
}
示例8: serverloop
func (d *Daemon) serverloop() error {
errorcount := 0
for {
if conn, err := d.l.Accept(); err != nil {
errorcount++
log4go.Error("Error accepting connection: %s", err)
if errorcount > 10 {
return log4go.Error("Too many errors, shutting server down")
}
} else {
go func() {
s := time.Now()
conn.SetDeadline(time.Time{})
codec := jsonrpc.NewServerCodec(conn)
defer func() {
codec.Close()
if r := recover(); r != nil {
log4go.Error("Recovered from panic: %v, stack: %s", r, string(debug.Stack()))
}
log4go.Debug("Serviced in %f milliseconds", time.Since(s).Seconds()*1000)
}()
for {
if err := d.server.ServeRequest(codec); err != nil {
log4go.Error("Error handling request: %v", err)
break
}
}
}()
}
}
}
示例9: StartServer
func StartServer(s *lb.Server) {
go func() {
serverStatus := &ServerStatus{s}
server := rpc.NewServer()
server.Register(serverStatus)
address := fmt.Sprintf("%s:%d",
s.Configuration.GeneralConfig.RPCHost,
s.Configuration.GeneralConfig.RPCPort,
)
server.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
listener, e := net.Listen("tcp", address)
if e != nil {
log.Fatal("listen error:", e)
}
for {
if conn, err := listener.Accept(); err != nil {
log.Fatal("accept error: " + err.Error())
} else {
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
}()
}
示例10: Run
func Run(clientName, clientUri string) (run func(), err error) {
service := NewUserService()
server := rpc.NewServer()
if err := server.RegisterName(clientName, service); err != nil {
return nil, err
}
run = func() {
l, err := net.Listen("tcp", clientUri)
if err != nil {
log.Fatal("net.Listen: ", err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Fatal("l.Accept: ", err)
}
defer conn.Close()
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
return run, err
}
示例11: NewRpcServer
// Create a new RPC server
func NewRpcServer(portNo uint16) (*rpc.Server, net.Listener) {
server := rpc.NewServer()
// Listens on a port
l, e := net.Listen("tcp", fmt.Sprintf(":%d", portNo))
if e != nil {
log.Fatal("listen error:", e)
}
log.Infof("RPC Server is listening on %s\n", l.Addr())
// run in background
go func() {
for {
conn, err := l.Accept()
if err != nil {
// if listener closed, just exit the groutine
if strings.Contains(err.Error(), "use of closed network connection") {
return
}
log.Fatal(err)
}
log.Infof("Server accepted connection to %s from %s\n", conn.LocalAddr(), conn.RemoteAddr())
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}()
return server, l
}
示例12: startServerJSONRPC
func startServerJSONRPC(port string) {
log.Println("RPC on", port)
srv := rpc.NewServer()
s := new(MessageType)
srv.Register(s)
srv.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
// Listen function creates servers,
// listening for incoming connections.
ln, err := net.Listen("tcp", port)
if err != nil {
panic(err)
}
defer ln.Close()
for {
// Listen for an incoming connection.
conn, err := ln.Accept()
if err != nil {
panic(err)
}
go srv.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
示例13: Start
/// 启动rpcServer,监听rpc服务器端口,由于Start内部调用阻塞的方法,应在go 语句中调用.
func (ms *RpcServer) Start() {
go func() {
seelog.Info("RpcServer start...")
hostAndPort := fmt.Sprintf("%v:%v", ms.host, ms.port)
servAddr, err := net.ResolveTCPAddr("tcp", hostAndPort)
if err != nil {
seelog.Criticalf("RpcServer failed to start with err<%v>", err.Error())
os.Exit(1)
}
listener, err := net.ListenTCP("tcp4", servAddr)
if err != nil {
seelog.Criticalf("RpcServer failed to start with err<%v>", err.Error())
os.Exit(1)
}
seelog.Debugf("Rpc Server listening: <%v>", servAddr.String())
defer listener.Close()
for {
conn, err := listener.Accept()
seelog.Debug("Rpc Server accept new connection")
if err != nil {
seelog.Critical(err.Error())
os.Exit(1)
}
go ms.rpcServer.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}()
}
示例14: StartRpc
func (c *ProducerServer) StartRpc() error {
log.Printf("Start to start rpc! \n")
server := rpc.NewServer()
err := c.RegistFunctions(server)
if err != nil {
log.Printf("consumer regist failed! \n")
return err
}
server.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
log.Printf("Start to listen consumer rpc!%s \n", fmt.Sprintf("%s:%d", c.Address, c.RpcPort))
l, e := net.Listen("tcp", fmt.Sprintf("%s:%d", c.Address, c.RpcPort))
if e != nil {
log.Printf("consumer listen failed err:%s! \n", e)
return e
}
for {
if c.IsShutDown {
break
}
conn, err := l.Accept()
if err != nil {
log.Printf("accept err:%s \n", err)
return err
}
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
return nil
}
示例15: Start
func Start() {
cfg := g.Config()
if !cfg.Rpc.Enabled {
log.Println("rpc.Start warning, not enable")
return
}
addr := cfg.Rpc.Listen
server := rpc.NewServer()
server.Register(new(Agent))
l, e := net.Listen("tcp", addr)
if e != nil {
log.Fatalln("rpc.Start error", e)
} else {
log.Println("rpc.Start ok, listening on", addr)
}
go func() {
for {
conn, err := l.Accept()
if err != nil {
if cfg.Debug {
log.Println("rpc accept fail:", err)
}
time.Sleep(time.Duration(100) * time.Millisecond)
continue
}
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}()
}