[]websocket 中 protobuf 数据的发送与接收的具体方法是怎样的?

package test;
enum MSG_HEADER {
LOGIN_GAME = 0x00000001;
JOIN_GAME = 0x00000002;
EXIT_GAME = 0x00000003;
}
message LoginGame {
required string account = 1; // 账号
required string passwd = 2; // 密码
}
第一次使用protobuf与服务端通讯,有点疑惑。以往使用json格式的数据通讯时,可以将协议名称写入json里一并发送到服务端;当使用protobuf的时候如何将协议名称和协议内容一起发送到服务端呢?我的做法是这样子的:

private onAssetsLoaded(data:string):void{
var pack:any = this.ProtoBuf.loadProto(data).build("test");
var MSG_HEADER:any = pack.MSG_HEADER;
var byte:Laya.Byte = this.socket.output;
byte.writeInt32(MSG_HEADER.LoginGame);//和服务端约定好,前四个字节存放协议的名称
var login:any = new pack.LoginGame;
login.set("account","");
login.set("passwd","");
var buffer:any = login.encode().toBuffer();
byte.writeArrayBuffer(buffer);
this.socket.socket.flush();
}
请问我的做法对吗?一般来说TS项目如何正确的使用protobuf发送与解析websocket通讯数据?

已邀请:

Victor

赞同来自: Laya_XS

已经解决

Laya_XS

赞同来自:

我看你写的发送protobuf数据没问题,服务端收不到数据吗?客户端接收数据的方法是监听event.message事件,在事件触发后,你读取socket实例.output的数据即可!

要回复问题请先

商务合作
商务合作