亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過 http 實現谷歌的協議緩沖區

如何通過 http 實現谷歌的協議緩沖區

Go
陪伴而非守候 2023-05-08 18:10:16
我正在使用谷歌的協議緩沖區將數據從客戶端發送到服務器??蛻舳撕头掌鞫际怯?Golang 編寫的。我認為它使用純 tcp 將數據從發送client到server。示例客戶端代碼:func getFakeTransaction() *proto.Transaction {    transaction := new(proto.Transaction)    transaction.ClientId = "client_1"    transaction.ClientName = "Europa"    items := new(proto.Items)    items.ItemId = 1    items.ItemName = "Space suite"    items.ItemValue = 2000    transaction.Items = items    return transaction}func readDataFromExternalDatasource() *proto.Transaction {    return getFakeTransaction()}func sentDataToServer(data []byte) {    conn, err := net.Dial("tcp", "localhost:8080")    defer conn.Close()    if err != nil {        fmt.Fprintf(os.Stderr, "Error while dialing server: %s\n", err.Error())        return    }    sentBytes, err := conn.Write(data)    if err != nil {        fmt.Fprintf(os.Stderr, "Error sending bytes to serve: %s\n", err.Error())        return    }    fmt.Printf("Sent %d bytes\n", sentBytes)}func main() {    fmt.Println("Starting client..")    data := readDataFromExternalDatasource()    dataInByteArr, err := protoc.Marshal(data)    if err != nil {        fmt.Fprintf(os.Stderr, "Error while Marshal data: %s", err.Error())    }    for {        sentDataToServer(dataInByteArr)        time.Sleep(1000)    }}如何HTTP使用 Golang 中的協議緩沖區將數據從客戶端發送到服務器?
查看完整描述

1 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

我成功地實現了protobuff vai http。


學分:https://jacobmartins.com/2016/05/24/practical-golang-using-protobuffs/


示例_客戶端:


func getFakeTransaction() *proto.Transaction {

    transaction := new(proto.Transaction)

    transaction.ClientId = "client_1"

    transaction.ClientName = "Europa"


    items := new(proto.Items)

    items.ItemId = 1

    items.ItemName = "Space suite"

    items.ItemValue = 2000

    transaction.Items = items


    return transaction

}


func sendMessage(transaction *proto.Transaction) {

    message, err := protoc.Marshal(transaction)

    if err != nil {

        fmt.Fprintf(os.Stderr, "Error while marshaling message: %s", err.Error())

        os.Exit(1)

    }


    _, err = http.Post("http://localhost:8080", "", bytes.NewBuffer(message))

    if err != nil {

        fmt.Fprintf(os.Stderr, "Error while post request to server: %s", err.Error())

        os.Exit(1)

    }

    fmt.Printf("Sent %d bytes to server\n", len(message))

}


func main() {

    fmt.Println("Starting client..")

    transaction := getFakeTransaction()

    for {

        sendMessage(transaction)

        // time.Sleep(1 * time.Second)

    }

}

示例服務器:


func printMessage(t *proto.Transaction) {

    clientId := t.GetClientId()

    clientName := t.GetClientName()

    items := t.GetItems()

    fmt.Printf("ClientId: %s, ClientName: %s, Items: %s\n", clientId, clientName, items)

}


func main() {

    fmt.Println("Staring server..")

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

        message, err := ioutil.ReadAll(r.Body)

        if err != nil {

            fmt.Fprintf(os.Stderr, "Error while reading data from client: ", err.Error())

            return

        }

        transaction := new(proto.Transaction)

        // protoc.Unmarshal(message, &transaction)

        if err = transaction.XXX_Unmarshal(message); err != nil {

            fmt.Fprintf(os.Stderr, "Error while unmarshaling client message: %s", err.Error())

            return

        }

        printMessage(transaction)

    })

    http.ListenAndServe(":8080", nil)


}

樣本簡介:


syntax="proto3";


package proto;


enum Status {

    SUCCESS = 0;

    INPROGRESS = 1;

    FAILED = 2;

}


message Items {

    int32 itemId = 1;

    string itemName = 2;

    int32 itemValue = 3;

    Status status = 4;

}


message Transaction {

    string clientId = 1;

    string clientName = 2;

    Items items = 3;

}


查看完整回答
反對 回復 2023-05-08
  • 1 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號