Skip to content

Commit

Permalink
feat:persistent client connection with node greeting using jsonrpc.
Browse files Browse the repository at this point in the history
  • Loading branch information
13x54n committed Jun 10, 2024
1 parent 308fe71 commit 009b27c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# ming
# ming
Ming is a distributed cloud platform for hosting JS frameworks.

### Getting Started:
I personally use Node.js on top of Bun.js for development though native components from Bun.js is not yet utilized so should work on Node.js too.

```bash
bun install
```
Binary file added bun.lockb
Binary file not shown.
42 changes: 42 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const net = require('net');

function connectToServer() {
const client = new net.Socket();

// Connect to the server
client.connect(3000, 'localhost', () => {
console.log('Connected to server');

// Send JSON-RPC request
const request = {
jsonrpc: '2.0',
method: 'greet',
params: ['John'],
id: 1
};
client.write(JSON.stringify(request));
});

// Handle incoming data
client.on('data', (data) => {
const response = JSON.parse(data.toString());
console.log('Server response:', response.result);
});

// Handle connection errors
client.on('error', (err) => {
console.error('Connection error:', err.message);
// Retry connection after 2 seconds
setTimeout(connectToServer, 2000);
});

// Handle server disconnection
client.on('end', () => {
console.log('Connection closed by server');
// Retry connection after 2 seconds
setTimeout(connectToServer, 2000);
});
}

// Initial connection attempt
connectToServer();
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "ming",
"version": "1.0.0",
"description": "",
"main": "client.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"net": "^1.0.2"
}
}
29 changes: 29 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const net = require("net");

// Create a TCP server
const server = net.createServer((socket) => {
console.log("Client connected");

// Handle incoming data
socket.on("data", (data) => {
const request = JSON.parse(data.toString());
if (request.method === "greet") {
const response = {
jsonrpc: "2.0",
id: request.id,
result: `Hello, ${request.params[0]}!`,
};
socket.write(JSON.stringify(response));
}
});

// Handle client disconnection
socket.on("end", () => {
console.log("Client disconnected");
});
});

// Start the TCP server on port 3000
server.listen(3000, () => {
console.log("JSON-RPC server is running on port 3000");
});

0 comments on commit 009b27c

Please sign in to comment.