Created
December 23, 2022 19:53
-
-
Save manniru/43c8861988c28b64f0a4e7f3f5a3efd7 to your computer and use it in GitHub Desktop.
Socket Client Server with Heart Beat
This file contains hidden or 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
const net = require("net"); | |
const client = net.createConnection({ port: 8000 }, () => { | |
console.log("Connected to server"); | |
}); | |
client.on("data", (data) => { | |
console.log(`Received: ${data.toString()}`); | |
}); | |
client.on("end", () => { | |
console.log("Disconnected from server"); | |
}); |
This file contains hidden or 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
{ | |
"name": "refetch-interval", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": {}, | |
"scripts": { | |
"start": "node index.js" | |
}, | |
"devDependencies": { | |
"@types/node": "^17.0.21" | |
} | |
} |
This file contains hidden or 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
const net = require("net"); | |
const server = net.createServer((socket) => { | |
console.log("Client connected"); | |
// Send a message every 5 seconds | |
setInterval(() => { | |
socket.write("Heartbeat"); | |
}, 5000); | |
socket.on("end", () => { | |
console.log("Client disconnected"); | |
}); | |
}); | |
server.listen(8000, () => { | |
console.log("Server listening on port 8000"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment