TypeScript implementation of the Hazel-Networking C# net library.
Note: This is alpha-quality software which is most likely not suitable for production applications. It hasn't really been tested and the probability of it exploding catastrophically is 0.9. It's also very likely to receive breaking changes at any given time.
NodeJS 14+ (HazelJS is esm only)
npm i @cyyynthia/hazeljs
yarn add @cyyynthia/hazeljs
pnpm i @cyyynthia/hazeljs
The docs only shows basic usage, and aren't super detailed. I'll eventually add better docs once the lib is more stable and in a production-ready shape. For now, autocomplete is your best friend:tm:
Here's a basic example:
import { Server } from '@cyyynthia/hazeljs'
const server = new Server() // To create an IPv6 server: new Server(true)
server.on('connection', (connection, hello) => {
console.log(`New connection from ${connection.remote.address}:${connection.remote.port}`)
console.log(`Hello payload:`, hello)
connection.on('message', (msg) => {
console.log('Message received', msg)
// Here we just echo it back, but you'd normally process the message and eventually
// reply with a more appropriated response.
connection.sendNormal(msg)
})
connection.on('close', () => {
console.log(`Connection with ${connection.remote.address}:${connection.remote.port} closed.`)
})
})
server.listen(1337, '127.0.0.1')
import { Client } from '@cyyynthia/hazeljs'
const client = new Client() // To create an IPv6 client: new Client('127.0.0.1', 1337, true)
client.on('connected', () => {
console.log('Connected!')
client.sendNormal(...)
})
client.on('message', (msg) => {
console.log('Message received', msg)
})
client.connect()