unknown

api calling part developing

1 +module.exports = {
2 + key = ""
3 +}
...\ No newline at end of file ...\ No newline at end of file
1 -const passport_IO = require('./passport+socketio.js'), 1 +
2 - passportSocketIo = require("passport.socketio"), 2 + const db = require('../lib/db.js');
3 - db = require('../lib/db.js'); 3 + const requesting = require('request');
4 + const secret_key = require('./api_option').key;
4 5
5 module.exports = (server, app) => { 6 module.exports = (server, app) => {
6 7
7 const io = require('socket.io', )(server, { 8 const io = require('socket.io', )(server, {
8 transports: ['websocket'] 9 transports: ['websocket']
9 }); 10 });
10 - 11 + const API_CALL= setInterval(() => {
11 - 12 + requesting.get({
12 - io.on('connection', (socket) => { //네임스페이스 연결시 루프 동작 13 + // api를 요청할 주소 -- 시크릿키,위도,경도 입력
14 + url: `https://api2.sktelecom.com/weather/current/minutely?appKey=${secret_key}&lat=37.239795&lon=127.083240`,
15 + json:true
16 + },
17 + //api에게 응답 받았을때 실행되는 callback func
18 + function (err, api_res, api_body) {
19 + if (err) throw err;
20 + // api의 대답이 있을경우 실행
21 + if (api_res) {
22 + const sql = `INSERT INTO dataset (question,option_1,option_2,option_3,option_4,answer,user_id) VALUES (?,?,?,?,?,?,?)`;
23 + db.query(sql, [question, option[0], option[1], option[2], option[3],machine_body.answer,user_id], function (err, result) {
24 + if (err) throw err;
25 + console.log("dataset successfully inserted into DB")
26 + });
27 + return response.send( { answer: machine_body.answer } );
28 + }
29 + });
30 + }, 60 * 1000); //1분마다 호출
31 +
32 + io.on('connection', (socket) => { //웹 페이지 연결시 루프 동작
13 socket.on('disconnecting', (reason) => { 33 socket.on('disconnecting', (reason) => {
14 - 34 + clearInterval(API_CALL); //연결 종료시 해제
15 }) 35 })
16 - socket.on("connection", (roomnum) => { //방 접속시에 현재 방번호 room에 저장 36 + socket.on("connection", (roomnum) => {
37 + API_CALL();
17 }) 38 })
18 }) 39 })
19 40
......