1.0.1 • Published 3 years ago

ws-rmi v1.0.1

Weekly downloads
4
License
MPL-2.0
Repository
github
Last release
3 years ago

ws-rmi

A Remote Method Invocation implementation written in TypeScript utilising the WebSocket protocol.

Installation

npm i ws-rmi

Basic Usage

Types

export interface Server {
    add(x: number, y: number): Promise<number>;
}

Client

import {Server} from "./Types";
import {RMIManager} from "ws-rmi";

const ws = new WebSocket("ws://localhost:8080");

ws.addEventListener("open", async () => {
	const rmi = new RMIManager(ws);
	const remote = rmi.getRemote<Server>();

	try {
		console.log(`1+1 is ${await remote.add(1, 1)}`);
	} catch (e) {
		console.log("Server side error occurred.\n", e);
	}
	
	ws.close();
});

Server

import { WebSocketServer } from "ws";
import {Server} from "./Types";
import {RMIManager} from "ws-rmi";

class ServerImpl implements Server {
	async add(x: number, y: number): Promise<number> {
		return x + y;
	}
}

const wss = new WebSocketServer({ port: 8080 });

wss.on("listening", () => {
	console.log("Server listening on port 8080...");

	wss.on("connection", ws => {
		console.log("Client connected! Establishing RMI...");

		const rmi = new RMIManager(ws as unknown as WebSocket);
		rmi.exposeFunctions(new ServerImpl());

		console.log("Done!");
	});
});
1.0.1

3 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

1.0.0

7 years ago