티스토리 뷰
문제
프로젝트에서 Typescript를 사용하고 있는데, 지원하는 모듈이 protobuf.js이다.
또, protobuffer 자체가 상속을 지원하지 않기 때문에
통합 타입의 상위 객체를 가지지 못하는 문제가 있었음
해결법
프로토버퍼 필드 예약 방식과 typescript의 타입 참조의 duck Type interface를 사용함
코드
아래 코드는 Protobuf preCode로 //interface가 명기된 message의 경우 typescript에서 I를 붙여서 사용하면 해당 필드를 사용한 객체의 경우 타입 캐스팅이 가능함.
syntax = "proto3";
package com.nhnent.casino.protocol;
// preReserved Message
// string messageName = 1;
// repeated string actions = 2;
// int32 resultCode = 3;
// interface message
message Message {
string messageName = 1;
}
// interface message
message Result {
int32 resultCode = 3;
}
message JoinPlayer
{
string messageName = 1;
repeated string actions = 2;
int32 resultCode = 3;
string id = 11;
int64 monney = 12;
}
message BuyInPlayer
{
string messageName = 1;
int32 resultCode = 3;
int64 buyInMoney = 11;
bool autoBuyIn = 12;
}
message SeatPlayer
{
string messageName = 1;
repeated string actions = 2;
int32 resultCode = 3;
string id = 11;
}
message RoomOption
{
string messageName = 1;
int32 option = 11;
}
message TableInfo
{
string messageName = 1;
int64 potMoney = 11;
}
생성된 코드
import * as $protobuf from "protobufjs";
/** Namespace com. */
export namespace com {
/** Namespace nhnent. */
namespace nhnent {
/** Namespace casino. */
namespace casino {
/** Namespace protocol. */
namespace protocol {
/** Properties of a Message. */
interface IMessage {
/** Message messageName */
messageName?: (string|null);
}
/** Represents a Message. */
class Message implements IMessage {
/**
* Constructs a new Message.
* @param [properties] Properties to set
*/
constructor(properties?: com.nhnent.casino.protocol.IMessage);
/** Message messageName. */
public messageName: string;
/**
* Creates a new Message instance using the specified properties.
* @param [properties] Properties to set
* @returns Message instance
*/
public static create(properties?: com.nhnent.casino.protocol.IMessage): com.nhnent.casino.protocol.Message;
/**
* Encodes the specified Message message. Does not implicitly {@link com.nhnent.casino.protocol.Message.verify|verify} messages.
* @param message Message message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: com.nhnent.casino.protocol.IMessage, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes a Message message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns Message
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): com.nhnent.casino.protocol.Message;
}
/** Properties of an Action. */
interface IAction {
/** Action actions */
actions?: (string[]|null);
}
/** Represents an Action. */
class Action implements IAction {
/**
* Constructs a new Action.
* @param [properties] Properties to set
*/
constructor(properties?: com.nhnent.casino.protocol.IAction);
/** Action actions. */
public actions: string[];
/**
* Creates a new Action instance using the specified properties.
* @param [properties] Properties to set
* @returns Action instance
*/
public static create(properties?: com.nhnent.casino.protocol.IAction): com.nhnent.casino.protocol.Action;
/**
* Encodes the specified Action message. Does not implicitly {@link com.nhnent.casino.protocol.Action.verify|verify} messages.
* @param message Action message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: com.nhnent.casino.protocol.IAction, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes an Action message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns Action
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): com.nhnent.casino.protocol.Action;
}
/** Properties of a Result. */
interface IResult {
/** Result resultCode */
resultCode?: (number|null);
}
/** Represents a Result. */
class Result implements IResult {
/**
* Constructs a new Result.
* @param [properties] Properties to set
*/
constructor(properties?: com.nhnent.casino.protocol.IResult);
/** Result resultCode. */
public resultCode: number;
/**
* Creates a new Result instance using the specified properties.
* @param [properties] Properties to set
* @returns Result instance
*/
public static create(properties?: com.nhnent.casino.protocol.IResult): com.nhnent.casino.protocol.Result;
/**
* Encodes the specified Result message. Does not implicitly {@link com.nhnent.casino.protocol.Result.verify|verify} messages.
* @param message Result message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: com.nhnent.casino.protocol.IResult, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes a Result message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns Result
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): com.nhnent.casino.protocol.Result;
}
/** Properties of a JoinPlayer. */
interface IJoinPlayer {
/** JoinPlayer messageName */
messageName?: (string|null);
/** JoinPlayer actions */
actions?: (string[]|null);
/** JoinPlayer resultCode */
resultCode?: (number|null);
/** JoinPlayer id */
id?: (string|null);
/** JoinPlayer monney */
monney?: (number|Long|null);
}
/** Represents a JoinPlayer. */
class JoinPlayer implements IJoinPlayer {
/**
* Constructs a new JoinPlayer.
* @param [properties] Properties to set
*/
constructor(properties?: com.nhnent.casino.protocol.IJoinPlayer);
/** JoinPlayer messageName. */
public messageName: string;
/** JoinPlayer actions. */
public actions: string[];
/** JoinPlayer resultCode. */
public resultCode: number;
/** JoinPlayer id. */
public id: string;
/** JoinPlayer monney. */
public monney: (number|Long);
/**
* Creates a new JoinPlayer instance using the specified properties.
* @param [properties] Properties to set
* @returns JoinPlayer instance
*/
public static create(properties?: com.nhnent.casino.protocol.IJoinPlayer): com.nhnent.casino.protocol.JoinPlayer;
/**
* Encodes the specified JoinPlayer message. Does not implicitly {@link com.nhnent.casino.protocol.JoinPlayer.verify|verify} messages.
* @param message JoinPlayer message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: com.nhnent.casino.protocol.IJoinPlayer, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes a JoinPlayer message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns JoinPlayer
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): com.nhnent.casino.protocol.JoinPlayer;
}
/** Properties of a BuyInPlayer. */
interface IBuyInPlayer {
/** BuyInPlayer messageName */
messageName?: (string|null);
/** BuyInPlayer resultCode */
resultCode?: (number|null);
/** BuyInPlayer buyInMoney */
buyInMoney?: (number|Long|null);
/** BuyInPlayer autoBuyIn */
autoBuyIn?: (boolean|null);
}
/** Represents a BuyInPlayer. */
class BuyInPlayer implements IBuyInPlayer {
/**
* Constructs a new BuyInPlayer.
* @param [properties] Properties to set
*/
constructor(properties?: com.nhnent.casino.protocol.IBuyInPlayer);
/** BuyInPlayer messageName. */
public messageName: string;
/** BuyInPlayer resultCode. */
public resultCode: number;
/** BuyInPlayer buyInMoney. */
public buyInMoney: (number|Long);
/** BuyInPlayer autoBuyIn. */
public autoBuyIn: boolean;
/**
* Creates a new BuyInPlayer instance using the specified properties.
* @param [properties] Properties to set
* @returns BuyInPlayer instance
*/
public static create(properties?: com.nhnent.casino.protocol.IBuyInPlayer): com.nhnent.casino.protocol.BuyInPlayer;
/**
* Encodes the specified BuyInPlayer message. Does not implicitly {@link com.nhnent.casino.protocol.BuyInPlayer.verify|verify} messages.
* @param message BuyInPlayer message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: com.nhnent.casino.protocol.IBuyInPlayer, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes a BuyInPlayer message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns BuyInPlayer
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): com.nhnent.casino.protocol.BuyInPlayer;
}
/** Properties of a SeatPlayer. */
interface ISeatPlayer {
/** SeatPlayer messageName */
messageName?: (string|null);
/** SeatPlayer actions */
actions?: (string[]|null);
/** SeatPlayer resultCode */
resultCode?: (number|null);
/** SeatPlayer id */
id?: (string|null);
}
/** Represents a SeatPlayer. */
class SeatPlayer implements ISeatPlayer {
/**
* Constructs a new SeatPlayer.
* @param [properties] Properties to set
*/
constructor(properties?: com.nhnent.casino.protocol.ISeatPlayer);
/** SeatPlayer messageName. */
public messageName: string;
/** SeatPlayer actions. */
public actions: string[];
/** SeatPlayer resultCode. */
public resultCode: number;
/** SeatPlayer id. */
public id: string;
/**
* Creates a new SeatPlayer instance using the specified properties.
* @param [properties] Properties to set
* @returns SeatPlayer instance
*/
public static create(properties?: com.nhnent.casino.protocol.ISeatPlayer): com.nhnent.casino.protocol.SeatPlayer;
/**
* Encodes the specified SeatPlayer message. Does not implicitly {@link com.nhnent.casino.protocol.SeatPlayer.verify|verify} messages.
* @param message SeatPlayer message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: com.nhnent.casino.protocol.ISeatPlayer, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes a SeatPlayer message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns SeatPlayer
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): com.nhnent.casino.protocol.SeatPlayer;
}
/** Properties of a RoomOption. */
interface IRoomOption {
/** RoomOption messageName */
messageName?: (string|null);
/** RoomOption option */
option?: (number|null);
}
/** Represents a RoomOption. */
class RoomOption implements IRoomOption {
/**
* Constructs a new RoomOption.
* @param [properties] Properties to set
*/
constructor(properties?: com.nhnent.casino.protocol.IRoomOption);
/** RoomOption messageName. */
public messageName: string;
/** RoomOption option. */
public option: number;
/**
* Creates a new RoomOption instance using the specified properties.
* @param [properties] Properties to set
* @returns RoomOption instance
*/
public static create(properties?: com.nhnent.casino.protocol.IRoomOption): com.nhnent.casino.protocol.RoomOption;
/**
* Encodes the specified RoomOption message. Does not implicitly {@link com.nhnent.casino.protocol.RoomOption.verify|verify} messages.
* @param message RoomOption message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: com.nhnent.casino.protocol.IRoomOption, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes a RoomOption message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns RoomOption
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): com.nhnent.casino.protocol.RoomOption;
}
/** Properties of a TableInfo. */
interface ITableInfo {
/** TableInfo messageName */
messageName?: (string|null);
/** TableInfo potMoney */
potMoney?: (number|Long|null);
}
/** Represents a TableInfo. */
class TableInfo implements ITableInfo {
/**
* Constructs a new TableInfo.
* @param [properties] Properties to set
*/
constructor(properties?: com.nhnent.casino.protocol.ITableInfo);
/** TableInfo messageName. */
public messageName: string;
/** TableInfo potMoney. */
public potMoney: (number|Long);
/**
* Creates a new TableInfo instance using the specified properties.
* @param [properties] Properties to set
* @returns TableInfo instance
*/
public static create(properties?: com.nhnent.casino.protocol.ITableInfo): com.nhnent.casino.protocol.TableInfo;
/**
* Encodes the specified TableInfo message. Does not implicitly {@link com.nhnent.casino.protocol.TableInfo.verify|verify} messages.
* @param message TableInfo message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: com.nhnent.casino.protocol.ITableInfo, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes a TableInfo message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns TableInfo
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): com.nhnent.casino.protocol.TableInfo;
}
}
}
}
}
사용방법
function onJoinPlayer(message: Packet.IMessage) {
let joinPlayer = message as Packet.IJoinPlayer;
}
onJoinPlayer(new Packet.JoinPlayer);
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- delaycall
- setTimeout
- 국내상장미국지수ETF투자계획
- 주식투자주식채권포트폴리오레이달리오사계절포트폴리오
- javascript object finding
- await
- 환율스프레드거래#원달러
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함