Created
June 27, 2019 11:30
-
-
Save vihoangson/c8d775c60dd6e29b46b183ca216763c6 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 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