ํค์ด,
์๋ต ํค๋ Access-Control-Allow-Origin
์ ๊ฑฐํ๋ ๋ฐฉ๋ฒ
{'origins' : 'asfdasd.com'}
์ด ์๋ํ์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
POST /socket.io/?EIO=3&transport=polling&t=1481690658797-5&sid=Dp18NNt_bWPkB4rGAAAP HTTP/1.1
์ถ์ฒ: https://evilhost.net
HTTP/1.1 400 ์๋ชป๋ ์์ฒญ
์ฝํ
์ธ ์ ํ: application/json
์ก์ธ์ค ์ ์ด ํ์ฉ ์๊ฒฉ ์ฆ๋ช
: true
์ก์ธ์ค ์ ์ด ํ์ฉ ์ถ์ฒ: https://evilhost.net
๋ ์ง: 2017๋
2์ 3์ผ ๊ธ์์ผ 12:27:42 GMT
์ฐ๊ฒฐ: ์ฐ๊ฒฐ ์ ์ง
์ ์ก ์ธ์ฝ๋ฉ: ์ฒญํฌ
๊ฐ์ ์ค๋ฅ
@Kenzku @thEpisode ์ด๋ค ๋ฒ์ ์ socket.io๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๊น?
2.x
์์ ์์ ๋์ด์ผ ํฉ๋๋ค(https://github.com/socketio/engine.io/pull/452 ํฌํจ).
์๋ ํ์ธ์ @darrachequesne ๋ต๋ณ ๊ฐ์ฌํฉ๋๋ค. ์ ๋ Crawler Site(https://www.crawlersite.com)์ ์์ฑ์์ด๋ฉฐ Socket.io๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ๋ก ๋ณด๋ด๊ณ ๋ชจ๋ ์ธํ๋ผ๋ Microsoft Azure์ ์์ผ๋ฉฐ ๋ชจ๋ ํ ์คํธ ๋ฐ ์ฒซ ๋ฒ์งธ ๋ฒ ํ ํ ์คํฐ๋ Azure๋ฅผ ์ฌ์ฉํ๋ฉฐ ์ฐ๋ฆฌ๋ ๋ฌธ์ ๋ ์์ง๋ง ์๋ก์ด ๋ฒ ํ ํ ์คํฐ๋ ๋ค๋ฅธ ํด๋ผ์ฐ๋ ์๋น์ค ๊ณต๊ธ์๋ฅผ ์ฌ์ฉํ๊ณ ๋ฒ๊ทธ๋ฅผ๋ณด๊ณ ํ์ต๋๋ค. ๋ฌธ์ ๋ CORS์ ๋๋ค. Google์์ ๋ชจ๋ ์ง๋ฌธ์ ์ฝ์ต๋๋ค!! ๊ฒ์ ์๋ฃจ์ ์์ Socket.io๊ฐ 2.0(์๋ฒ์์ ์ ํํ 2.0.1)์ผ๋ก ์ ๋ฐ์ดํธ๋์์์ โโํ์ธํ๊ณ ํด๋ผ์ด์ธํธ์ ์๋ฒ๋ฅผ ์ ๋ฐ์ดํธํ์ง๋ง ๋ฌธ์ ๊ฐ ์ง์๋๋ฉฐ CORS ๋ฌธ์ ๋ ๋ณด๊ณ ํ Express๋ฅผ ์ฌ์ฉํ์ฌ ํด๊ฒฐํ์ต๋๋ค.
Google์ ๋ชจ๋ ์๋ฃจ์ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ ๋งค์ฐ ๋นํฉ์ค๋ฝ์ต๋๋ค. GitHub ๋ฌธ์ ๋๋ StackOverflow์ ์ด๊ฒ์ ๊ฒ์ํ์ง ์์ ์ ์ ๋ํด ์ ๊ฐ์ค๋ฝ๊ฒ ์๊ฐํ์ง๋ง ๊ฐ๋ฅํ ๋ชจ๋ ์ธ๋ถ ์ ๋ณด๋ฅผ ์ ๊ณตํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ณ ์ถ์ต๋๋ค. ํ ์คํธํ ์ผ๋ถ URL:
๋ฐฑ ์๋ URI: http://crawlerbackend.azurewebsites.net
Express CORS ํ
์คํธ: https://testcors.000webhostapp.com (์ด ํธ์คํ
์ ํ
์คํฐ๊ฐ ์ฌ์ฉํจ)
Socket.io CORS ํ
์คํธ: https://testcrawlersite.000webhostapp.com (์ฝ์ ์ฐธ์กฐ)
ํ
์คํธํ ๊ธฐ๋ณธ ์ฌ์ดํธ: http://crawler-test.azurewebsites.net (์ ์์ ์ฌํญ์ด ์๋ ๊ฒฝ์ฐ์๋ ์์๋จ)
Socket.io ํ์ผ: http://crawlerbackend.azurewebsites.net/socket.io.js
app.js์ ์ผ๋ถ:
`
var ๊ฒฝ๋ก = ์๊ตฌ('๊ฒฝ๋ก');
var bodyParser = require('๋ณธ์ฒด ํ์');
var cors = require('cors');
var ํํ = ์๊ตฌ('ํํ');
var ์ฑ = ์ต์คํ๋ ์ค();
// use body parser so we can get info from POST and/or URL parameters
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use(bodyParser.json()); // support json encoded bodies
app.use(cors({ origin: '*' }));
// Settings for CORS
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.header('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.header('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', false);
// Pass to next layer of middleware
next();
});
var server = app.listen(cross.NormalizePort(process.env.PORT || 3500));
var io = require('socket.io').listen(server, {
log: false,
agent: false,
origins: '*:*',
transports: ['websocket', 'htmlfile', 'xhr-polling', 'jsonp-polling', 'polling']
});
`
๋ฌธ์ ํด๊ฒฐ!! @Kenzku๋ ์ด ๋ฌธ์๋ฅผ ๊ฒํ ํ์ญ์์ค: https://github.com/socketio/socket.io/blob/master/docs/API.md#serveroriginsvalue ๋ฐ ์ฝ๋: https://github.com/socketio/socket.io /blob/master/lib/index.js#L67 ๋ฐ Express๋ฅผ ์ฌ์ฉํ๋์ง ํ์ธํฉ๋๋ค(์ด ์์ ๋ ๋งค์ฐ ์ ์ฉํฉ๋๋ค): https://github.com/socketio/socket.io#in -conjunction-with-express
์์ฒญ๋! ์ด์ ๋ฌธ์ ๋ฅผ ์ข ๋ฃํ ์ ์์ต๋๋ค.
@theEpisode ๋๋ ์ด๊ฒ์ด ์ค๋๋ ์ค๋ ๋๋ผ๋ ๊ฒ์ ์๊ณ ์์ง๋ง ๊ทธ ๋ง์ง๋ง ๋งํฌ๊ฐ ๋๋ฅผ ์ํด app.listen(...)
์์ง๋ง socket.io
๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ index.js
http.listen(...)
๋ฅผ ํธ์ถํ๊ณ ๋ค์์ ๋ฃ์ง ์๋๋ก ํด์ผ ํฉ๋๋ค. app
๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฉด ์ด CORS ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค. ๊ทธ๋ฐ ์์ ์ผ!
@frewinchristopher ์์์ ์ธ๊ธํ ๊ฒ๊ณผ ๋์ผํ ํจํด์ ๋ฐ๋ฅด๊ณ ์์ต๋๋ค. ๋ก์ปฌ์์๋ ์ ๋๋ก ์๋ํ์ง๋ง ์๊ฒฉ์์๋ ๋ฐ์ํฉ๋๋ค(์ด์ : CORS ์์ฒญ์ด ์ฑ๊ณตํ์ง ๋ชปํ์ต๋๋ค). ๋ด๊ฐ ์ด๋ ์๋ชปํ์ด..?
@gowthamyaal ์ฝ๋๋ฅผ ๋ณด์ฌ์ฃผ์๊ฒ ์ต๋๊น?
๊ฑฑ์ ํด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ํ์ง๋ง AWS๋ฅผ ์ฌ์ฉํ๋ฉด์ ํ๋ ฅ์ IP ๋ฐ ๋ก์ปฌ IP์ ์ถฉ๋์ด ๋ฐ์ํ์ฌ CORS ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค. ๋์ค์ ์์ผ ์์ IP๋ฅผ 0.0.0.0์ผ๋ก ๋ณ๊ฒฝํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค.
์ด์ ์๊ฒฉ์์ ๊ฒฝ์ ์กฐ๊ฑด ๋ฌธ์ ์ ์ง๋ฉดํ์ฌ webpack-dev-config๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. io connect๋ฅผ ์ฌ์ฉํ๊ณ ๋์ผํ ํ์ผ์์ ์ฝ๋๋ฅผ ๋ด๋ณด๋ด๋ ค๊ณ ํ์ง๋ง ์ ์๋์ง ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.
์นํฉ ๊ฐ๋ฐ ๊ตฌ์ฑ
ํด๋ผ์ด์ธํธ ์์ผ ์ฝ๋
;)
์ฃผ์์ด ๋ฌ๋ฆฐ ์ค์ด ์ ์๊ฒ ํจ๊ณผ์ ์ด์์ต๋๋ค. ... ๋๊ตฌ๋ ์ง ์ด๊ฒ์ผ๋ก ๋๋ฅผ ๋์ธ ์ ์์ต๋๋ค. ... ๋ฏธ๋ฆฌ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
๋๋ ๋ด๊ฐ ์ฐพ์ ์ ์๋ ๋ชจ๋ ์๋ฃจ์ ์ ๋ฐ๋์ง๋ง ์์ง ์๋ํ๋ ์๋ต์ ๋ณด๋ด์ง ๋ชปํ์ต๋๋ค. ๋๊ตฐ๊ฐ๊ฐ ๋ด ์ค ์ ์๋ค๋ฉด ๋๋ ๋งค์ฐ ๊ฐ์ฌ ํ ๊ฒ์ ๋๋ค.
`const express = require('express')
const http = ์๊ตฌ('http')
const socketIO = ์๊ตฌ('socket.io')
const cors = ์๊ตฌ('cors')
// ํธ์คํธ ํฌํธ
์์ ํฌํธ = 3001
const ์ฑ = ์ต์คํ๋ ์ค()
app.use(cors())
// ์๋ฒ ์ธ์คํด์ค
const ์๋ฒ = http.createServer(์ฑ)
// ์๋ฒ ์ธ์คํด์ค๋ฅผ ์ฌ์ฉํ์ฌ ์์ผ ์์ฑ
const io = socketIO(์๋ฒ)
io.origins("http://localhost:3000")
io.on('์ฐ๊ฒฐ', ์์ผ => {
console.log("์ฌ์ฉ์ ์ฐ๊ฒฐ")
socket.on('disconnect', () => {
console.log("User Disconnected")
})
socket.on('send message', (message) => {
io.sockets.emit('send message', message)
})
})
server.listen(ํฌํธ, () => console.log( Listening on port ${port}
))`
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
@theEpisode ๋๋ ์ด๊ฒ์ด ์ค๋๋ ์ค๋ ๋๋ผ๋ ๊ฒ์ ์๊ณ ์์ง๋ง ๊ทธ ๋ง์ง๋ง ๋งํฌ๊ฐ ๋๋ฅผ ์ํด
app.listen(...)
์์ง๋งsocket.io
๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐindex.js
http.listen(...)
๋ฅผ ํธ์ถํ๊ณ ๋ค์์ ๋ฃ์ง ์๋๋ก ํด์ผ ํฉ๋๋ค.app
๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฉด ์ด CORS ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค. ๊ทธ๋ฐ ์์ ์ผ!