k-rpc-socket

WebJar for k-rpc-socket

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

k-rpc-socket
Last Version

Last Version

1.8.0
Release Date

Release Date

Type

Type

jar
Description

Description

k-rpc-socket
WebJar for k-rpc-socket
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/mafintosh/k-rpc-socket

Download k-rpc-socket

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/k-rpc-socket/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>k-rpc-socket</artifactId>
    <version>1.8.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/k-rpc-socket/
implementation 'org.webjars.npm:k-rpc-socket:1.8.0'
// https://jarcasting.com/artifacts/org.webjars.npm/k-rpc-socket/
implementation ("org.webjars.npm:k-rpc-socket:1.8.0")
'org.webjars.npm:k-rpc-socket:jar:1.8.0'
<dependency org="org.webjars.npm" name="k-rpc-socket" rev="1.8.0">
  <artifact name="k-rpc-socket" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='k-rpc-socket', version='1.8.0')
)
libraryDependencies += "org.webjars.npm" % "k-rpc-socket" % "1.8.0"
[org.webjars.npm/k-rpc-socket "1.8.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.webjars.npm : bencode jar [2.0.0,3)
org.webjars.npm : buffer-equals jar [1.0.4,2)
org.webjars.npm : safe-buffer jar [5.1.1,6)

Project Modules

There are no modules declared in this project.

k-rpc-socket

Low level implementation of the k-rpc network layer that the BitTorrent DHT uses. Mostly extracted from the bittorrent-dht module on npm into its own repo.

npm install k-rpc-socket

build status

Usage

var rpc = require('k-rpc-socket')

var socket = rpc()

socket.on('query', function (query, peer) {
  socket.response(peer, query, {echo: query.a})
})

socket.bind(10000, function () {
  var anotherSocket = rpc()
  anotherSocket.query({host: '127.0.0.1', port: 10000}, {q: 'echo', a: {hello: 'world'}}, function (err, response) {
    console.log(response.r) // prints {echo: {hello: Buffer('world')}}
  })
})

API

var socket = rpc([options])

Create a new k-rpc-socket. Options include:

{
  timeout: queryTimeout, // defaults to 2s
  socket: optionalUdpSocket,
  isIP: optionalBooleanFunction
}

socket.send(peer, message, [callback])

Send a raw message. The callback is called when the message has been flushed from the socket.

var id = socket.query(peer, query, [callback])

Send a query message. The callback is called with (err, response, peer, request). You should set the method name you are trying to call as {q: 'method_name'} and query data as {a: someQueryData}.

The query method returns a query id. You can use this id to cancel the query using the .cancel method.

socket.cancel(id)

Cancel a query. Will call the corresponding query's callback with an error indicating that it was cancelled.

socket.response(peer, query, response, [callback])

Send a response to a query. The callback is called when the message has been flushed from the socket.

socket.error(peer, query, error, [callback])

Send an error reploy to a query. The callback is called when the message has been flushed from the socket.

socket.inflight

Integer representing the number of concurrent queries that are currently pending.

socket.destroy()

Destroys and unbinds the socket

socket.bind([port], [address], [callback])

Call this to bind to a specific port. If port is not specified or is 0, the operating system will attempt to bind to a random port. If address is not specified, the operating system will attempt to listen on all addresses.

If you don't call this a random free port will be chosen.

socket.on('query', query, peer)

When a query is received a query event is emitted with the query data and a peer object representing the querying peer.

socket.on('warning', error)

Emitted when a non fatal error has occured. It is safe to ignore this.

socket.on('error', error)

Emitted when a fatal error has occured.

License

MIT

Versions

Version
1.8.0