kenote-socket-helper
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

socket-helper

Send Protocol Buffers data with socket's helper.

NPM Version NPM Downloads Gratipay

Installation

$ npm install kenote-socket-helper
#
$ yarn add kenote-socket-helper

Usages

import { Protobuffer, Socket, PB, PBSetting, ReflectionObject } from 'kenote-socket-helper'

const client: Socket = new Socket('127.0.0.1:8000')
const setting: PBSetting = {
  path     : './pb',
  socket   : 'socket.proto',
  gmPB     : 'gm.proto',
  prefix   : 'com.autorun',
  game     : 'game'
}

async function send (code: number, req: string, res: string, body: any): Promise<void> {
  let protobuffer: Protobuffer = new Protobuffer(setting)
  let { createPBBuffer, makeData, decode, gameMessage } = protobuffer
  let data: Buffer = makeData(createPBBuffer(code, req, body))
  let messageQuery: ReflectionObject = <ReflectionObject> gameMessage(res)
  try {
    let buffer: Buffer = await client.send(data)
    let msgBase: PB.Message = <PB.Message> decode(buffer, messageQuery)
    let { msghead, msgbody } = msgBase
    console.log(msgbody, msghead)
  } catch (error) {
    console.error(error)
  }
}

send(2001, 'CS_GM_QUERY', 'SC_GM_QUERY', {})

License

this repo is released under the MIT License.

Dependents (0)

Package Sidebar

Install

npm i kenote-socket-helper

Weekly Downloads

0

Version

1.1.2

License

MIT

Unpacked Size

34 kB

Total Files

15

Last publish

Collaborators

  • thondery