Last active
November 15, 2025 08:53
-
-
Save Kinetic27/ad4f6bcc99bd521a2a1e923187fa7266 to your computer and use it in GitHub Desktop.
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 WebSocket = require('ws'); | |
| const fs = require('fs'); | |
| const sampleData = JSON.parse(fs.readFileSync('sample-data.json', 'utf8')); | |
| let currentIndex = 0; | |
| const wss = new WebSocket.Server({ port: 8080 }); | |
| console.log('run in 8080'); | |
| wss.on('connection', ws => { | |
| console.log('client connected'); | |
| const interval = setInterval(() => { | |
| if (ws.readyState === WebSocket.OPEN) { | |
| ws.send(JSON.stringify(sampleData[currentIndex])); | |
| currentIndex = (currentIndex + 1) % sampleData.length; | |
| } else { | |
| clearInterval(interval); | |
| } | |
| }, 500); | |
| ws.on('close', () => { | |
| console.log('bye'); | |
| clearInterval(interval); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment