Giter Club home page Giter Club logo

node-raknet-native's Introduction

node-raknet-native

NPM version Build Status Discord Try it on gitpod

Native RakNet bindings for Node.js

Install

npm install raknet-native

Prebuilds are provided for 64-bit Windows 10, Linux and macOS. If a prebuild does not work, please create an issue and set enviornment variable FORCE_BUILD to force a manual build.

Usage

class Client, Server

The Client and Server classes are JS wrappers around the internal RakClient and RakServer classes implemented in C++ in src/. See the ts/RakNet.js for usage.

Example

A simple generic RakNet example:

const { Client, Server, PacketPriority, PacketReliability } = require('raknet-native')
// The third paramater is for game type, you can specify 'minecraft' or leave it blank for generic RakNet
const client = new Client('127.0.0.1', 19130)
// hostname, port, serverOptions
const server = new Server('0.0.0.0', 19130, { maxConnections: 3 })
server.listen()
client.connect()
client.on('encapsulated', (buffer) => {
  console.assert(buffer.toString() == '\xA0 Hello world')
})

server.on('openConnection', (client) => {
  client.send(Buffer.from('\xA0 Hello world'), PacketPriority.HIGH_PRIORITY, PacketReliability.UNRELIABLE, 0)
})

For example, for Minecraft Bedrock, use:

const { Client, Server, PacketPriority } = require('raknet-native')
const client = new Client('127.0.0.1', 19130, { protocolVersion: 10 })
const server = new Server('0.0.0.0', 19130, { protocolVersion: 10, maxConnections: 3, message: Buffer.from('MCPE;Steve;2 7;0.11.0;0;20')  })

For more usage examples see tests/.

Cloning

If cloning from git, you must clone the repository recursively.

git clone --recursive https://github.com/extremeheat/node-raknet-native.git

Dependencies

For the most part, pre-builds are provided so you don't need to worry about having the correct build tools installed. But if a pre-build doesn't work or isn't avaliable, you may need to install the following deps:

Mac OS

You need to install xcode utilities first (this is not the whole Xcode!):

xcode-select --install

Other platforms

A valid CMake installation is required to build the library along with C++ compiler tools. CMake must be avaliable from the command line path.

Exported API

See index.d.ts for full API docs, a copy of the definitions are listed below:

export declare class Client extends EventEmitter {
    constructor(hostname: string, port: number, options?: ClientOptions)
    /**
     * Send a RakNet PING request to the server
     */
    ping(): void
    /**
     * Start a connection request with the server using host and port passed in constructor
     */
    connect(): Promise<void>
    /**
     * Recieve a PING event from the server, the extra field with the additional pong data
     */
    on(event: 'pong', params: ({ extra: Buffer }) => void)
    /**
     * The client has connected
     */
    on(event: 'connect', params: (data: { address: string, guid: string }) => void)
    /**
     * The client has been disconnected
     */
    on(event: 'disconnect', params: (data: { address: string, guid: string, reason: MessageID }) => void)
    /**
     * Recieve an actual user packet.
     */
    on(event: 'encapsulated', params: (data: { buffer: Buffer, address: string, guid: string }) => void)
    /**
     * Send a message to the server.
     * @param message The message you want to send to the server
     * @param priority The priority, which dicates if message should be sent now or queued
     * @param reliability Options to ensure a packet arrives to the recipient
     * @param orderingChannel The RakNet ordering channel, used only for ReliableOrdered packets
     */
    send(message: Buffer, priority: PacketPriority, reliability: PacketReliability, orderingChannel: number, broadcast?: boolean): number
    /**
     * Closes the connection. This is a *blocking* call.
     */
    close(): void
}

export declare class ServerClient {
    close(): void
    send(message: Buffer, priority: PacketPriority, reliability: PacketReliability, orderingChannel: number, broadcast?: boolean): number
}

export declare class Server {
    constructor(hostname: string, port: number, options: ServerOptions)
    /**
     * The list of connections tracked by RakNet at the moment. The string key is the GUID.
     */
    connections: Map<string, Server>
    /**
     * Start listening on the specified host and port
     */
    listen(): Promise<void>
    /**
     * Send a message to the client.
     * @param address The address of the client you want to send to
     * @param port The port of the client you want to send to
     * @param message The message you want to send to the client
     * @param priority The priority, which dicates if message should be sent now or queued
     * @param reliability Options to ensure a packet arrives to the recipient
     * @param orderingChannel The RakNet ordering channel, used only for ReliableOrdered packets
     * @param broadcast Send to all clients? If true, send to all clients except `address` and `port`.
     */
    send(address: string, port: number, message: Buffer, priority: PacketPriority, reliability: PacketReliability, orderingChannel: number, broadcast?: boolean): number
    /**
     * Sets additional data to be sent along with RakNet's unconnected pong packet
     */
    setOfflineMessage(buffer: Buffer | ArrayBuffer)
    /**
     * Recieve an actual user packet.
     * `address` is the address of the connected user, `guid` is a UUID. You can map this to a `connection` above.
     */
    on(event: 'encapsulated', params: (data: { buffer: Buffer, address: string, guid: string }) => void)
    /**
     * Emited on a new connection, with a `ServerClient` paramater to make it easier to send messages to this user.
     */
    on(event: 'openConnection', params: (client: ServerClient) => void)
    /**
     * Emitted after a user closes a connection.
     */
    on(event: 'closeConnection', params: (client: ServerClient, reason: MessageID) => void)
    /**
     * Closes the connection. This is a *blocking* call.
     */
    close(): void
}

node-raknet-native's People

Contributors

b23r0 avatar extremeheat avatar gameparrot avatar oguzhanumutlu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-raknet-native's Issues

Fails to build when running on Ubuntu-20.04-aarch64

npm ERR! /home/ubuntu/Test-App/node_modules/raknet-native/raknet/Source/FileList.cpp:24:10: fatal error: sys/io.h: No such file or directory
npm ERR!    24 | #include <sys/io.h>
npm ERR!       |          ^~~~~~~~~~
npm ERR! compilation terminated.
npm ERR! make[2]: *** [raknet/Lib/LibStatic/CMakeFiles/RakNetLibStatic.dir/build.make:336: raknet/Lib/LibStatic/CMakeFiles/RakNetLibStatic.dir/__/__/Source/FileList.cpp.o] Error 1
npm ERR! make[1]: *** [CMakeFiles/Makefile2:159: raknet/Lib/LibStatic/CMakeFiles/RakNetLibStatic.dir/all] Error 2
npm ERR! make: *** [Makefile:130: all] Error 2
npm ERR! ERR! OMG Process terminated: 2

Ubuntu-20.04-aarch64 doesn't include sys/io.h so errors during the build as it doesn't account for the diff architecture within Raknet

Prebuids for arm (android)

i am trying to use bedrock protocol in my android application, and it would be incredibly convenient if the module did not require compilation (the client would not need to download compilers weighing gigabytes + often there is not even the opportunity to install them). Is there an option for the appearance of prebuilds for different processors, for example arm?

raknet-native library not found

Hi Im trying to run a script on ARMv7 Processor is this supported with raknet and the bedrock-protocol?

[raknet] raknet-native library not found, defaulting to jsp-raknet. Correct the "raknetBackend" option to avoid this error. Error: Could not locate the bindings file. Tried:
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/build/node-raknet.node
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/build/Debug/node-raknet.node
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/build/Release/node-raknet.node
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/out/Debug/node-raknet.node
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/Debug/node-raknet.node
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/out/Release/node-raknet.node
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/Release/node-raknet.node
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/build/default/node-raknet.node
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/compiled/18.15.0/linux/arm/node-raknet.node
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/addon-build/release/install-root/node-raknet.node
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/addon-build/debug/install-root/node-raknet.node
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/addon-build/default/install-root/node-raknet.node
→ /home/ubuntu/sambashare/Discord Bot AnarchyXI/node_modules/raknet-native/lib/binding/node-v108-linux-arm/node-raknet.node

Installation on Linux failed

Hey, having problems running raknet-native on a Rapberry Pi running Linux today:

Error log:
`/home/pi/Desktop/raknet/node_modules/bindings/bindings.js:135
throw err;
^

Error: Could not locate the bindings file. Tried:
→ /home/pi/Desktop/raknet/node_modules/raknet-native/build/node-raknet.node
→ /home/pi/Desktop/raknet/node_modules/raknet-native/build/Debug/node-raknet.node
→ /home/pi/Desktop/raknet/node_modules/raknet-native/build/Release/node-raknet.node
→ /home/pi/Desktop/raknet/node_modules/raknet-native/out/Debug/node-raknet.node
→ /home/pi/Desktop/raknet/node_modules/raknet-native/Debug/node-raknet.node
→ /home/pi/Desktop/raknet/node_modules/raknet-native/out/Release/node-raknet.node
→ /home/pi/Desktop/raknet/node_modules/raknet-native/Release/node-raknet.node
→ /home/pi/Desktop/raknet/node_modules/raknet-native/build/default/node-raknet.node
→ /home/pi/Desktop/raknet/node_modules/raknet-native/compiled/18.2.0/linux/arm/node-raknet.node
→ /home/pi/Desktop/raknet/node_modules/raknet-native/addon-build/release/install-root/node-raknet.node
→ /home/pi/Desktop/raknet/node_modules/raknet-native/addon-build/debug/install-root/node-raknet.node
→ /home/pi/Desktop/raknet/node_modules/raknet-native/addon-build/default/install-root/node-raknet.node
→ /home/pi/Desktop/raknet/node_modules/raknet-native/lib/binding/node-v108-linux-arm/node-raknet.node
at bindings (/home/pi/Desktop/raknet/node_modules/bindings/bindings.js:126:9)
at Object. (/home/pi/Desktop/raknet/node_modules/raknet-native/binding.js:30:33)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Module._load (node:internal/modules/cjs/loader:827:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object. (/home/pi/Desktop/raknet/node_modules/raknet-native/index.js:1:18)
at Module._compile (node:internal/modules/cjs/loader:1105:14) {
tries: [
'/home/pi/Desktop/raknet/node_modules/raknet-native/build/node-raknet.node',
'/home/pi/Desktop/raknet/node_modules/raknet-native/build/Debug/node-raknet.node',
'/home/pi/Desktop/raknet/node_modules/raknet-native/build/Release/node-raknet.node',
'/home/pi/Desktop/raknet/node_modules/raknet-native/out/Debug/node-raknet.node',
'/home/pi/Desktop/raknet/node_modules/raknet-native/Debug/node-raknet.node',
'/home/pi/Desktop/raknet/node_modules/raknet-native/out/Release/node-raknet.node',
'/home/pi/Desktop/raknet/node_modules/raknet-native/Release/node-raknet.node',
'/home/pi/Desktop/raknet/node_modules/raknet-native/build/default/node-raknet.node',
'/home/pi/Desktop/raknet/node_modules/raknet-native/compiled/18.2.0/linux/arm/node-raknet.node',
'/home/pi/Desktop/raknet/node_modules/raknet-native/addon-build/release/install-root/node-raknet.node',
'/home/pi/Desktop/raknet/node_modules/raknet-native/addon-build/debug/install-root/node-raknet.node',
'/home/pi/Desktop/raknet/node_modules/raknet-native/addon-build/default/install-root/node-raknet.node',
'/home/pi/Desktop/raknet/node_modules/raknet-native/lib/binding/node-v108-linux-arm/node-raknet.node'
]
}

Node.js v18.2.0
`
Also ran this on Node V12.0.0 (on another machine) and no luck
Works great on a Windows 10 device though!

Found #4 but was not sure if that related to this. Is there any short-term fixes for this yet?
Thanks, Larry.

Recommended to replace the native library

In actual use, I found that the raknet implementation of the fb-raknet (c++) version has problems such as a large number of thread leaks.

top -p [pid] -H

1649411856(1)

Since this native code has not been maintained for many years, I suggest to consider replacing it with a new raknet I implemented in rust, which is still compatible with minecraft.

https://github.com/b23r0/rust-raknet

It has proven stable in production environments.

Linux Errors

I am trying to install this on ubuntu aka raspberry pi 4 and it doesn't want to install what do I do

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.