Skip to content

Instantly share code, notes, and snippets.

@vihoangson
Created June 27, 2019 11:30
Show Gist options
  • Select an option

  • Save vihoangson/c8d775c60dd6e29b46b183ca216763c6 to your computer and use it in GitHub Desktop.

Select an option

Save vihoangson/c8d775c60dd6e29b46b183ca216763c6 to your computer and use it in GitHub Desktop.
const WebSocket = require('ws');
const https = require('https');
const request = require('request');
const wss = new WebSocket.Server({ port: 8686 });
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
wss.on('connection', function connection(ws,req) {
//const ip = req.connection.remoteAddress;
ws.isAlive = true;
ws.send('{"id":1,"message":"'+req.connection.remoteAddress+'"}');
ws.on('message', function incoming(message) {
ws.send('{"id":1,"message":1111111}');
console.log(message);
//============ ============ ============ ============
request('https://jsonplaceholder.typicode.com/todos/1', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('do1')
ws.send('{"id":1,"message":"'+message+'"}');
}
})
//============ ============ ============ ============
});
console.log('do2')
ws.send('{"id":1,"message":333333}');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment