美文网首页
Go 1.8使用Protocol Buffer

Go 1.8使用Protocol Buffer

作者: EasyNetCN | 来源:发表于2022-06-02 09:01 被阅读0次

最近在写一个在线客服模块,秉着简单,可控,可维护,可扩展,满足需求的原则,找了一个开源项目:https://github.com/kone-net/go-chat

当然对于原始项目,肯定还是需要一些改动的,比如:鉴权,用户体系,文件类型消息的处理(存在阿里云OSS上)等等。

为了满足需求,修改了原始的基于protocol buffer的message,修改后在重新生成对应的go文件时候,遇到了一些问题,可以参考以下解决方法。

github上的protobuf(https://github.com/golang/protobuf)已经说明,需要使用:google.golang.org/protobuf

安装官方文档 https://grpc.io/docs/languages/go/quickstart/ 安装对应的插件模块,mac需要在.zshrc文件添加以下内容:

export PATH="$PATH:$(go env GOPATH)/bin"

以下是message proto文件:

syntax = "proto3";
package protocol;
option go_package = "/protocol";

message Message {
    string avatar = 1;       //头像
    string fromUsername = 2; // 发送消息用户的用户名
    string from = 3;         // 发送消息用户uuid
    string to = 4;           // 发送给对端用户的uuid
    string content = 5;      // 文本消息内容
    int32 contentType = 6;   // 消息内容类型:1.文字 2.普通文件 3.图片 4.音频 5.视频 6.语音聊天 7.视频聊天
    string type = 7;         // 消息传输类型:如果是心跳消息,该内容为heatbeat,在线视频或者音频为webrtc
    int32 messageType = 8;   // 消息类型,1.单聊 2.群聊
    string fileKey = 9;      // OSS文件Key
    string fileSuffix = 10;  // 文件后缀,如果通过二进制头不能解析文件后缀,使用该后缀
    bytes file = 11;         // 如果是图片,文件,视频等的二进制
}

执行以下命令生产对应的go文件:

protoc --go_out=. pkg/protocol/*.proto 

相关文章

网友评论

      本文标题:Go 1.8使用Protocol Buffer

      本文链接:https://www.haomeiwen.com/subject/vdzfmrtx.html