0.0.1 • Published 8 years ago

unpack-easy v0.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
8 years ago

unpack-easy NPM version

粘连包两种解决方案

Installation

$ npm install unpack-easy

Example

var unpackEasy = require('unpack-easy');

    服务端

var net = require('net');

var unpackEasy = require('unpack-easy');


var client = net.createServer(function(c) { // 'connection' 监听器
  console.log('服务器已连接');
  c.on('end', function() {
    console.log('服务器已断开');
  });


  c.on('data', function(data) {
    //resolvePackageByEndTag(data, handlerFunc, [0xfd, 0xff]);
    //resolvePackageByLengthTag(data, handlerFunc, 1);

    //g.resolvePackageByLengthTag(data, handlerFunc, 1)
    unpackEasy.resolvePackageByEndTag.call(c, data,handlerFunc, [0x5]);    //通过结尾标识的方法必须通过这种方式
    unpackEasy.resolvePackageByLengthTag(data, handlerFunc, 1);    //通过包头加长度的方式不必要,前提是只有一个socket通道,所以建议都绑定一下作用域

  });

});
client.listen(1111, function() { // 'listening' 监听器
  console.log('服务器已绑定');
});

function handlerFunc(d) {
  console.log(d)
}

    客户端

var net = require('net');
var client = net.connect({port: 1111},
  function() { //'connect' 监听器

      client.write(new Buffer([1,1,1,1,1,1,1,1,0x5,1,1,1,1,1,1,1,1]));
      client.write(new Buffer([1,1,1,1,1,1,1,1,1,0x5,1,1,1,1,1,1,1]));
      //client.write(new Buffer([0x0, 0x1,08]));

  });

client.on('end', function() {
  console.log('客户端断开连接');
});

API

  • unpack-easy.resolvePackageByEndTag()
  • unpack-easy.resolvePackageByLengthTag()

Contributing

  • Fork this Repo first
  • Clone your Repo
  • Install dependencies by $ npm install
  • Checkout a feature branch
  • Feel free to add your features
  • Make sure your features are fully tested
  • Publish your local branch, Open a pull request
  • Enjoy hacking <3

ISC license

Copyright (c) 2016 baiguangan


0.0.1

8 years ago