I'm trying to send a get request to API usign Socket.IO
//socket
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
var apiPath='localhost:1242/api/v1/';
app.listen(8090);
io.on('connection', function (socket) {
socket.on('getBanners', function(data){
socket.get(apiPath+'1/1/1', function (resData) {
console.log(resData)
socket.emit('response', {data:resData})
})
});
});
My view
<script type="text/javascript">
$(document).ready(function(){
var socket = io('http://localhost:8090');
socket.on('response',function(data){
alert(data)
});
});
</script>
The idea is keep refreshing the data in case the api info is changed. But I don't know how make a GET request on SocketIo side
I found an solution In my socket I used request module
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
var apiPath='http://localhost:1242/api/v1/';
var request = require('request');
app.listen(8090);
io.on('connection', function (socket) {
socket.on('getBanners', function(data){
request.get(apiPath+'1/1/1',function (error, response, body){
socket.emit('result', {res:JSON.parse(body)})
})
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.