1.0.4 • Published 1 year ago

xornor-websocket v1.0.4

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

xornor-websocket v1.0.4

The xornor-websocket library exported as Node.js modules.

Installation

Using npm:

$ npm i xornor-websocket

In Node.js:

const WebSocketServer = require('xornor-websocket');
const fs = require("fs");
// data which send on particular channel
let data = { data: "value" };

//channel name
let channel = 2;

// connnection details
let connection = {
  cert: fs.readFileSync("cert.pem"),
  key: fs.readFileSync("key.pem"),
  passphrase: "this is a websocket key",
};

// create instance of a class
const wsserver = new WebSocketServer(connection);

//call onConnection function for create a connection
wsserver.onConnection(data, channel);

// start a websocket server on particular port
wsserver.start(3002, () => {
  console.log("WebSocket server started on port 3002");
});

In client side:

const channel = localStorage.getItem("channel");
// get channel from local storage
// if you have any other storage you can get from that storage

// create a websocket instance and pass path of websocket
const socket = new WebSocket("wss://localhost:8443/" + channel);

// send obj with json stringify
socket.send(JSON.stringify(obj));

socket.onopen = () => {
  console.log(`Connected to channel: ${channel}`);
};

socket.onerror = (error) => {
  console.error("WebSocket error:", error);
};

socket.onclose = (event) => {
  console.log(`WebSocket connection closed with code ${event.code}`);
};

socket.onmessage = (event) => {
  console.log(`Received message: ${event.data}`);
};
1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago