-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:persistent client connection with node greeting using jsonrpc.
- Loading branch information
Showing
6 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |