项目作者: kenote

项目描述 :
Send Protocol Buffers data with socket's helper.
高级语言: TypeScript
项目地址: git://github.com/kenote/socket-helper.git
创建时间: 2019-04-12T02:00:10Z
项目社区:https://github.com/kenote/socket-helper

开源协议:MIT License

下载


socket-helper

Send Protocol Buffers data with socket’s helper.

NPM Version
NPM Downloads
Gratipay

Installation

  1. $ npm install kenote-socket-helper
  2. #
  3. $ yarn add kenote-socket-helper

Usages

  1. import { Protobuffer, Socket, PB, PBSetting, ReflectionObject } from 'kenote-socket-helper'
  2. const client: Socket = new Socket('127.0.0.1:8000')
  3. const setting: PBSetting = {
  4. path : './pb',
  5. socket : 'socket.proto',
  6. gmPB : 'gm.proto',
  7. prefix : 'com.autorun',
  8. game : 'game'
  9. }
  10. async function send (code: number, req: string, res: string, body: any): Promise<void> {
  11. let protobuffer: Protobuffer = new Protobuffer(setting)
  12. let { createPBBuffer, makeData, decode, gameMessage } = protobuffer
  13. let data: Buffer = makeData(createPBBuffer(code, req, body))
  14. let messageQuery: ReflectionObject = <ReflectionObject> gameMessage(res)
  15. try {
  16. let buffer: Buffer = await client.send(data)
  17. let msgBase: PB.Message = <PB.Message> decode(buffer, messageQuery)
  18. let { msghead, msgbody } = msgBase
  19. console.log(msgbody, msghead)
  20. } catch (error) {
  21. console.error(error)
  22. }
  23. }
  24. send(2001, 'CS_GM_QUERY', 'SC_GM_QUERY', {})

License

this repo is released under the MIT License.