0
저는 WebSocket과 함께 google-protobuf JS를 사용하는 Angular5 + 프런트 엔드에서 백엔드와 통신하고 있습니다.google protobuf-js : 내 메시지를 효율적으로 구문 분석하는 방법
- 요청 객체 :
나는 현재 나의
.proto
파일에서 2 개체가. - 알림 개체.
나는 다음 메시지가 웹 소켓을 통해 전송 얻을 것이다 핸들러 서비스를 만들었습니다,하지만 나는 큰 문제를 가지고 :
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
을해야한다. - 내가 역 직렬화하려는 방식으로 잘못한 점이 있는데 그것은 바보 같은 실수입니다. 나는 어쩌면 일반
Message.deserialize()
을google-protobuf
에서 사용할 수 있지만 각 개체가 자체 deserialize 메서드를 구현해야하므로이 방법이 없습니다. - 마지막으로, 내 응용 프로그램에 대해 내 다른 개체를 모두 중첩 할 기본 개체를 설정하는
.proto
파일을 만들어야합니다. 이 방법으로 중첩 된 객체를 역 직렬화 할 수있는 단일 유형의 메시지를 deserialize 할 수 있습니다. (이게 가장 좋은 해결책이지만 백엔드는 꽤 비싸다.)
그리고 백엔드에도 적응하기가 꽤 빠릅니다. –