golang jsonrpc

前端之家收集整理的这篇文章主要介绍了golang jsonrpc前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main

import (
    "github.com/streadway/amqp"
    "log"
    "net"
    "net/rpc"
    "net/rpc/jsonrpc"
    "os"
    "time"
)

type MPMsg struct {
}

func (mpMsg *MPMsg) Handle(args *map[string]interface{},reply *map[string]interface{}) error {
    *reply = map[string]interface{}{}
    return nil 
}

func main() {
    lf,_ := os.OpenFile("/var/log/"+time.Now().Format("2006-01-02T15:04:05")+".log",os.O_APPEND|os.O_CREATE|os.O_RDWR,0666)
    defer lf.Close()
    log.SetOutput(lf)

    mpMsg := new(MPMsg)
    rpc.Register(mpMsg)

    addr,_ := net.ResolveTCPAddr("tcp",":135")
    ln,e := net.ListenTCP("tcp",addr)
    if e != nil {
        panic(e)
    }   
    for {
        conn,e := ln.Accept()
        if e != nil {
            continue
        }
        go jsonrpc.ServeConn(conn)
    }   
}



    // RPC calls.
    rpcClient,e := jsonrpc.Dial("tcp","2d1c07f4-8d2d-11e5-bf81-54ee755d5aba:135")
    if e != nil {
        log.Println("Error dail rpc server:",e)
        http.Error(w,e.Error(),http.StatusInternalServerError)
        return
    }   
    var reply map[string]interface{}
    e = rpcClient.Call("MPMsg.Handle",msg,&reply)
    if e != nil {
        log.Println("Error call rpc method:",http.StatusInternalServerError)
        return
    } 
原文链接:https://www.f2er.com/go/190087.html

猜你在找的Go相关文章