Skip to content

rsenn/qjs-net

This branch is 2159 commits ahead of khanhas/minnet-quickjs:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Feb 27, 2025
e4539ed · Feb 27, 2025
Feb 7, 2025
Jun 25, 2024
Feb 27, 2025
Feb 27, 2025
Feb 24, 2025
Feb 27, 2025
Feb 7, 2025
May 21, 2023
Sep 4, 2021
Jul 19, 2019
Mar 19, 2023
Oct 11, 2024
May 22, 2023
Jul 19, 2019
Jul 7, 2023
Mar 17, 2023
Apr 22, 2023
May 24, 2023
Jan 28, 2024
Jun 7, 2021
Jul 27, 2022
Mar 26, 2023
Mar 26, 2023
Dec 31, 2024
Feb 18, 2024
Feb 7, 2025
Jun 7, 2024

Repository files navigation

qjs-net

This package aims to provide simple, minimal and essential networking infrastructures for QuickJS. (Derived from minnet-quickjs)

Currently, it can:

  • Creating WebSocket and HTTP server
  • Creating WebSocket or HTTP client
  • fetch

Usage

Requirements:

  • clang or gcc
  • libwebsockets

To use qjs-net in your QuickJS project, run following commands:

cd your_project_dir
git clone https://github.com/rsenn/qjs-net
cd qjs-net
git submodule update --init
cd build
cmake ..

Alternatively use either premake or ninja:

premake5 gmake

or

ninja -C build

These alternative build methods can't invoke the compilation of libwebsockets, therefore you have to build and install it manually before you can compile qjs-net:

cd qjs-net
. build-libwebsockets.sh

TYPE=Release builddir=libwebsockets/build \
  build_libwebsockets 
  
make -C libwebsockets/build install

You may add the variable OPENSSL_PREFIX (e.g. OPENSSL_PREFIX=/opt/libressl-3.5.1) in front of that command when building against a custom SSL library build.

cd your_project_dir git clone https://github.com/rsenn/qjs-net cd qjs-net/build cmake ..


In your JS script:
```javascript
import * as net from 'net.so';

net.server(options): Create a WebSocket server and listen to host:port.

options: an object with following properties:

  • port: number, optional, default = 7981
  • host: string, optional, default = "localhost"
  • onConnect: function, optional
    Calls when a client connects to server. Returns client's MinnetWebsocket instance in parameter. Syntax:
onConnect: (client_socket) => {
    print("A client connected")
}
  • onClose(why): function, optional
    Call when client disconnects from server. Returns disconnect reason in parameter. Syntax:
onClose: (why) => {
    print("Client disconnected. Reason: ", why)
}
  • onMessage: function, optional
    Call when client sends a message to server. Returns client's MinnetWebsocket instance and received message in parameters. Syntax:
onMessage: (client_socket, message) => {
    print("Received: ", message)
}
  • onPong: function, optional
    Call when client sends a pong. Returns client's MinnetWebsocket instance and received ArrayBuffer data in parameters. Syntax:
onPong: (client_socket, data) => {
    print("Pongged: ", data)
}

net.client(options): Create a WebSocket client and connect to a server.

options: an object with following properties:

  • port: number, optional, default = 7981
  • host: string, optional, default = "localhost"
  • onConnect: function, optional
    Calls when client connects to server succesfully. Returns server's MinnetWebsocket instance in parameter. Syntax:
onConnect: (server_socket) => {
    print("Connected to server")
}
  • onClose(why): function, optional
    Call when client disconnects from server. Returns disconnect reason in parameter. Syntax:
onClose: (why) => {
    print("Disconnected from server. Reason: ", why)
}
  • onMessage: function, optional
    Call when server sends message to client. Returns server's MinnetWebsocket instance and received message in parameters. Syntax:
onMessage: (server_socket, message) => {
    print("Received: ", message)
}
  • onPong: function, optional
    Call when server sends a pong. Returns server's MinnetWebsocket instance and received ArrayBuffer data in parameters. Syntax:
onPong: (server_socket, data) => {
    print("Pongged: ", data)
}

MinnetWebsocket instance

contains socket to a server or client. You can use these methods to communicate:

  • .send(message): message can be string or ArrayBuffer
  • .ping(data): data must be ArrayBuffer
  • .pong(data): data must be ArrayBuffer

fetch(url): Get resources from url

url: a string to download resources from.
Returns MinnetResponse object that you can use these
Methods:

  • .text(): Get body text as string
  • .json(): Get body text, parse as JSON and returns parsed object.
  • .arrayBuffer(): Get body as an ArrayBuffer

Properties:

  • .ok: boolean, Read-only
    Whether the response was successful
  • .url: string, Read-only
    URL of the response
  • .status: number, Read-only
    Status code of the response
  • .type: string, Read-only
    Type of the response

Check out example.mjs

About

Minimal networking library for QuickJS

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 63.3%
  • JavaScript 14.1%
  • CMake 13.1%
  • Makefile 8.8%
  • Other 0.7%