Code Monkey home page Code Monkey logo

node-peek's Introduction

node-peek

NodeJs wrapper that peeks bytes from a given socket file descriptor

Installation

Install with npm:

$ npm install socket-peek

Examples

Read bytes of the next pending datagram using FIONREAD. As it takes a pointer as a parameter, it's straightforward using a Buffer as a parameter.

var fs = require('fs');
var express = require('express');
var https = require('https');
var peek = require('bindings')('peek.node').peek;

var key = fs.readFileSync('./peek-example-key.pem');
var cert = fs.readFileSync('./peek-example-cert.pem');
var https_options = {
    key: key,
    cert: cert
};
var PORT = 443;
var HOST = 'localhost';
app = express();

server = https.createServer(https_options, function(req, res) {
    res.writeHead(200);
    res.end("Hello");
}).listen(PORT, HOST);

function toHexString(byteArray) {
  return byteArray.map(function(byte) {
    return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  }).join(' ')
}

server.on('connection', function(connection) {
    var buffer = new Buffer(1000);
    var ret = peek(connection._handle.fd, 1000, buffer);
    console.log('New connection, fd: ' + connection._handle.fd + ', ret: ' + ret);
    if (ret > 0) {
        console.log('Returned bytes: ' + buffer.slice(0,ret).toString('hex'));
    }

});
console.log('HTTPS Server listening on %s:%s', HOST, PORT);

node-peek's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.