google protobuf-js : 如何有效地解析我的消息
google protobuf-js : How to parse efficiently my messages
我正在开发 Angular5+ 前端,它使用 google-protobuf JS 和 WebSocket 与后端通信。
我的 .proto
文件中目前有 2 个对象:
- 一个请求对象。
- 一个通知对象。
然后我制作了一个处理程序服务,它将获取通过 WebSocket 发送的消息,但后来我遇到了一个大问题:我找不到有效解析/反序列化我的对象的方法:
this._socketService.getSocket().onmessage = ((message: Message) => {
const uiArray = new Uint8Array(message.data);
this.parseMessage(uiArray);
});
parseMessage(uiArray: Uint8Array) {
let response = null;
// DOES NOT WORK
// response = reqRep.Response.deserializeBinary(uiArray) || notif.BackendStatusNotification.deserializeBinary(uiArray);
// <==== This is where I need to find a good way to deserialize efficiently my objects
// TEMPORARY
if (uiArray.byteLength === 56) {
response = reqRep.Response.deserializeBinary(uiArray)
} else {
response = notif.BackendStatusNotification.deserializeBinary(uiArray);
}
// Notify different Observables which object has changed based on their type
switch (response && response.hasSupplement() && response.getSupplement().array[0]) {
case 'type.googleapis.com/req.BackendStatusResponse':
this._responseSubject.next(response);
break;
case 'type.googleapis.com/notif.BackendStatusNotification':
this._notificationSubject.next(response);
break;
default:
console.log('DOESN\'T WORK');
}
}
我尝试使用代码中所示的 ||
来始终能够反序列化我的响应,但它不起作用:如果第一个失败,我会收到运行时错误。
我有一些见解,但也许有人可以帮助我:
- 要么我必须做
try
catch
来处理每个案例(这显然是最糟糕的解决方案)。
- 我尝试反序列化的方式有问题,这是一个愚蠢的错误。我虽然也许
我可以使用
google-protobuf
中的泛型 Message.deserialize()
,但这是行不通的,因为每个对象都应该实现自己的反序列化方法。
- 或者最后一个,我应该创建一个
.proto
文件,在其中设置一个基础对象,该对象将嵌套我的应用程序的所有不同对象。这样我就可以反序列化单一类型的消息,该消息将能够反序列化嵌套对象。 (对我来说这是最好的解决方案,但对后端来说成本很高)
最后,我做了我在问题中提到的最后一个选项,即将我所有的不同对象封装在一个通用对象中。
这样,我只有一种方法来反序列化我的对象,然后分派对嵌套已知对象的处理。它完美运行。
我正在开发 Angular5+ 前端,它使用 google-protobuf JS 和 WebSocket 与后端通信。
我的 .proto
文件中目前有 2 个对象:
- 一个请求对象。
- 一个通知对象。
然后我制作了一个处理程序服务,它将获取通过 WebSocket 发送的消息,但后来我遇到了一个大问题:我找不到有效解析/反序列化我的对象的方法:
this._socketService.getSocket().onmessage = ((message: Message) => {
const uiArray = new Uint8Array(message.data);
this.parseMessage(uiArray);
});
parseMessage(uiArray: Uint8Array) {
let response = null;
// DOES NOT WORK
// response = reqRep.Response.deserializeBinary(uiArray) || notif.BackendStatusNotification.deserializeBinary(uiArray);
// <==== This is where I need to find a good way to deserialize efficiently my objects
// TEMPORARY
if (uiArray.byteLength === 56) {
response = reqRep.Response.deserializeBinary(uiArray)
} else {
response = notif.BackendStatusNotification.deserializeBinary(uiArray);
}
// Notify different Observables which object has changed based on their type
switch (response && response.hasSupplement() && response.getSupplement().array[0]) {
case 'type.googleapis.com/req.BackendStatusResponse':
this._responseSubject.next(response);
break;
case 'type.googleapis.com/notif.BackendStatusNotification':
this._notificationSubject.next(response);
break;
default:
console.log('DOESN\'T WORK');
}
}
我尝试使用代码中所示的 ||
来始终能够反序列化我的响应,但它不起作用:如果第一个失败,我会收到运行时错误。
我有一些见解,但也许有人可以帮助我:
- 要么我必须做
try
catch
来处理每个案例(这显然是最糟糕的解决方案)。 - 我尝试反序列化的方式有问题,这是一个愚蠢的错误。我虽然也许
我可以使用
google-protobuf
中的泛型Message.deserialize()
,但这是行不通的,因为每个对象都应该实现自己的反序列化方法。 - 或者最后一个,我应该创建一个
.proto
文件,在其中设置一个基础对象,该对象将嵌套我的应用程序的所有不同对象。这样我就可以反序列化单一类型的消息,该消息将能够反序列化嵌套对象。 (对我来说这是最好的解决方案,但对后端来说成本很高)
最后,我做了我在问题中提到的最后一个选项,即将我所有的不同对象封装在一个通用对象中。
这样,我只有一种方法来反序列化我的对象,然后分派对嵌套已知对象的处理。它完美运行。