Toggle navigation
Toggle navigation
This project
Loading...
Sign in
김가영
/
Probability Death
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
unknown
2018-12-06 14:13:04 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
34c864609e99882dea4521f7b9c6891a14851975
34c86460
1 parent
0924359f
request several apis in socket connection
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
13 deletions
lib/request_api.js
lib/socketio.js
lib/request_api.js
0 → 100644
View file @
34c8646
const
secret_key
=
require
(
'../keys/api_option'
).
key
;
const
requesting
=
require
(
'request'
);
module
.
exports
=
(
io
)
=>
{
let
info
=
{}
setInterval
(()
=>
{
requesting
.
get
({
// api를 요청할 주소 -- 시크릿키,위도,경도 입력
url
:
`https://api2.sktelecom.com/weather/current/minutely?appKey=
${
secret_key
}
&lat=37.239795&lon=127.083240`
,
json
:
true
},
//api에게 응답 받았을때 실행되는 callback func
function
(
err
,
api_res
,
api_body
)
{
if
(
err
)
throw
err
;
// api의 대답이 있을경우 실행
if
(
api_res
)
{
console
.
log
(
api_body
);
io
.
emit
(
"new_info_in"
,
info
)
// api 호출 정보 클라이언트에게 전송
}
});
},
60
*
1000
);
//1분마다 호출
}
\ No newline at end of file
lib/socketio.js
View file @
34c8646
const
db
=
require
(
'./db.js'
)
;
const
secret_key
=
require
(
'../keys/api_option'
).
key
;
const
requesting
=
require
(
'request'
)
;
const
db
=
require
(
'./db.js'
);
const
secret_key
=
require
(
'../keys/api_option'
).
key
;
const
requesting
=
require
(
'request'
)
;
const
lat
=
"37.239795"
;
const
lon
=
"127.083240"
;
module
.
exports
=
(
server
,
app
)
=>
{
const
io
=
require
(
'socket.io'
,
)(
server
,
{
transports
:
[
'websocket'
]
});
const
API_CALL
=
setInterval
(()
=>
{
let
info
=
{}
const
CALL
=
(
when
,
what
)
=>
{
requesting
.
get
({
// api를 요청할 주소 -- 시크릿키,위도,경도 입력
url
:
`https://api2.sktelecom.com/weather/current/minutely?appKey=
${
secret_key
}
&lat=37.239795&lon=127.083240`
,
json
:
true
url
:
`https://api2.sktelecom.com/weather/
${
when
}
/
${
what
}
?appKey=
${
secret_key
}
&lat=
${
lat
}
&lon=
${
lon
}
`
,
json
:
true
},
//api에게 응답 받았을때 실행되는 callback func
function
(
err
,
api_res
,
api_body
)
{
...
...
@@ -20,18 +22,29 @@ module.exports = (server, app) => {
// api의 대답이 있을경우 실행
if
(
api_res
)
{
console
.
log
(
api_body
);
return
response
.
send
(
"ok"
);
}
});
}
,
60
*
1000
);
//1분마다 호출
}
io
.
on
(
'connection'
,
(
socket
)
=>
{
//웹 페이지 연결시 루프 동작
let
API_CALL
;
socket
.
on
(
"connection"
,
()
=>
{
API_CALL
=
setInterval
(()
=>
{
CALL
(
"current"
,
"minutely"
);
//현재날씨 (분별)
CALL
(
"index"
,
"wct"
);
//체감온도
CALL
(
"index"
,
"heat"
);
//열지수
CALL
(
"index"
,
"th"
);
//불쾌지수
CALL
(
"index"
,
"uv"
);
//자외선지수
},
60
*
1000
);
//1분마다 호출
});
socket
.
on
(
'disconnecting'
,
(
reason
)
=>
{
clearInterval
(
API_CALL
);
//연결 종료시 해제
})
socket
.
on
(
"connection"
,
(
roomnum
)
=>
{
API_CALL
();
})
})
...
...
Please
register
or
login
to post a comment