GithubHelp home page GithubHelp logo

watson / utp-native Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mafintosh/utp-native

0.0 3.0 0.0 149 KB

Native bindings for libutp

License: MIT License

C++ 53.10% Python 0.65% JavaScript 42.37% C 3.89%

utp-native's Introduction

utp-native

Native bindings for libutp. For more information about utp read BEP 29.

npm install utp-native

build status

Usage

var utp = require('utp-native')

var server = utp.createServer(function (socket) {
  socket.pipe(socket) // echo server
})

server.listen(10000, function () {
  var socket = utp.connect(10000)

  socket.write('hello world')
  socket.on('data', function (data) {
    console.log('echo: ' + data)
  })
})

API

There two APIs available. One that mimicks the net core module in Node as much as possible and another one that allows you to reuse the same udp socket for both the client and server. The last one is useful if you plan on using this in combination with NAT hole punching.

net-like API

server = utp.createServer([onconnection])

Create a new utp server instance.

server.listen([port], [address], [onlistening])

Listen for on port. If you don't provide a port or pass in 0 a free port will be used. Optionally you can provide an interface address as well, defaults to 0.0.0.0.

var addr = server.address()

Returns an address object, {port, address} that tell you which port / address this server is bound to.

server.on('listening')

Emitted when the server is listening

server.on('connection', connection)

Emitted when a client has connected to this server

server.on('error', err)

Emitted when a critical error happened

server.close()

Closes the server.

server.on('close')

Emitted when the server is fully closed. Note that this will only happen after all connections to the server are closed.

server.maxConnections

Set this property is you want to limit the max amount of connections you want to receive

server.connections

An array of all the connections the server has.

server.ref()

Opposite of unref.

server.unref()

Unreferences the server from the node event loop.

connection = utp.connect(port, [host])

Create a new client connection. host defaults to localhost. The client connection is a duplex stream that you can write / read from.

address = connection.address()

Similar to server.address.

connection.ref()

Similar to server.ref()

connection.unref()

Similar to server.unref()

connection.on('close')

Emitted when the connection is fully closed.

connection.on('error', err)

Emitted if an unexpected error happens.

connection.destroy()

Forcefully destroys the connection.

In addition to this the connection has all the classic stream methods such as .write etc.

Socket API

The socket api allows you to reuse the same underlying UDP socket to both connect to other clients on accept incoming connections. It also mimicks the node core dgram socket api.

socket = utp()

Create a new utp socket

socket.bind([port], [host], [onlistening])

Bind the socket.

socket.on('listening')

Emitted when the socket is bound.

socket.send(buf, offset, len, port, host, [callback])

Send a udp message.

socket.on('message', buffer, rinfo)

Listen for a udp message.

socket.close()

Close the socket.

address = socket.address()

Returns an address object, {port, address} that tell you which port / address this socket is bound to.

socket.on('close')

Emitted when the socket is fully closed.

socket.on('error')

Emitted if the socket experiences an error.

socket.listen([port], [host], [onlistening])

Start listening for incoming connections. Performs a bind as well.

socket.on('connection', connection)

Emitted after you start listening and a client connects to this socket. Connection is similar to the connection used in the net api.

connection = socket.connect(port, host)

Connect to another socket. Connection is similar to the connection used in the net api.

socket.unref()

Dereference the socket from the node event loop.

socket.ref()

Opposite of socket.unref()

License

MIT

utp-native's People

Contributors

daviddias avatar mafintosh avatar ralphtheninja avatar

Watchers

 avatar  avatar  avatar

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.