Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

kenote-socket-helper

1.1.1 • 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.

Install

npm i kenote-socket-helper

DownloadsWeekly Downloads

4

Version

1.1.1

License

MIT

Unpacked Size

19.8 kB

Total Files

11

Last publish

Collaborators

  • avatar