Socket.io: Nodejs Socket.io Express๊ฐ€ HTTP API ํ˜ธ์ถœ ํ›„ ํ•ธ๋“œ์…ฐ์ดํฌ ์‹คํŒจ

์— ๋งŒ๋“  2017๋…„ 11์›” 19์ผ  ยท  2์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: socketio/socket.io

์›ํ•˜๋Š” ์ž‘์—…:

  • [x] ๋ฒ„๊ทธ ๋ณด๊ณ 

ํ˜„์žฌ ํ–‰๋™

๋‚ด๊ฐ€ Nodejs, Express ์„œ๋ฒ„์™€ MongoDB๋ฅผ ํ•จ๊ป˜ SocketIo๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๋‚˜๋Š” ๋ฌธ์„œ๋ฅผ ๋”ฐ๋ž๋‹ค. ๊ทธ๊ฒƒ์€ ๋ฌธ์ œ ์—†์ด ์„œ๋กœ์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์—ฐ๊ฒฐํ•  ๋•Œ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. Http ์š”์ฒญ์„ ๋งŒ๋“ค ๋•Œ ์ƒˆ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์—†๊ณ  ์ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

socket.io.js:7370 WebSocket ์—ฐ๊ฒฐ
'ws:// localhost:28232/socket.io/?userId=userAmr&EIO=3&transport=websocket&sid=wNTTgrUD-PSeNaIcAAAF '
์‹คํŒจ: WebSocket ํ•ธ๋“œ์…ฐ์ดํฌ ์ค‘ ์˜ค๋ฅ˜: ์˜ˆ๊ธฐ์น˜ ์•Š์€ ์‘๋‹ต ์ฝ”๋“œ:
400

Http ์š”์ฒญ ์ „์— ์—ฐ๊ฒฐ๋œ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๋Š” ๋ฌธ์ œ ์—†์ด ๋ฉ”์‹œ์ง€๋ฅผ ๊ณ„์† ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์†Œ์ผ“ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋””๋ฒ„๊น…ํ•˜๊ณ  ํด๋ผ์ด์–ธํŠธ ์†Œ์ผ“ ์š”์ฒญ์ด ์—ฐ๊ฒฐ ๊ธฐ๋Šฅ์œผ๋กœ ์ด๋™ํ•œ ๋‹ค์Œ errorCode:1 ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด๊ฒƒ์€ ๋‚ด ์ฝ”๋“œ

/**
 * Create Express server.
 */
const app = express();


// API endpoint
app.get('/api/test',(req,res)=>{
    res.status(200).send({test:"test"});
});



/**
 * Init socket
 */
const server = require('http').Server(app);
const io = require('socket.io')(server);


io.on('connection', (socket) => {

        // emit message to group
        socket.on('emitMessage', (data, callback) => {
                io.emit('emitMessage', data);
        });
});

์ด๊ฒƒ์€ ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์— ๋Œ€ํ•œ ๋‚ด ์งˆ๋ฌธ์ž…๋‹ˆ๋‹ค. https://stackoverflow.com/q/47375814/6786941

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋ฌธ์ œ๋ฅผ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. express-status-monitor ํŒจํ‚ค์ง€๊ฐ€ ์ด ์ž˜๋ชป๋œ ๋™์ž‘์„ ํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๊ฒƒ์„ ์ œ๊ฑฐํ•˜๋ ค๊ณ ํ•˜๋ฉด ์™„๋ฒฝํ•˜๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค

 // comment these lines, as they making the issue
 // const expressStatusMonitor = require('express-status-monitor'); 
 // app.use(expressStatusMonitor());

์ตœ์ข… ์ฝ”๋“œ:

let app = require('express')();

// these two lines were making the problem, please comment them. if you want to reproduce the problem enable them again 
// const expressStatusMonitor = require('express-status-monitor');
// app.use(expressStatusMonitor());

let http = require('http').Server(app);
let io = require('socket.io')(http);
let port = process.env.PORT || 3000;

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
});



app.get('/api/v0/availabilities',(req,res)=>{
    res.status(200).send({test:"test"});
});


io.on('connection', (socket) => {

    // emit message to group
    socket.on('emitMessage', (data) => {
        io.emit('emitMessage', data);
    });
});

http.listen(port, function(){
    console.log('listening on *:' + port);
});

์ด๊ฒƒ์€ ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์— ๋Œ€ํ•œ ๋‚ด ์งˆ๋ฌธ์ž…๋‹ˆ๋‹ค. https://stackoverflow.com/q/47375814/6786941

์ด๊ฒƒ์€ ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์— ๋Œ€ํ•œ ๋‚ด ๋Œ€๋‹ต์ž…๋‹ˆ๋‹ค. https://stackoverflow.com/a/47411701/6786941

๋ชจ๋“  2 ๋Œ“๊ธ€

๋ฌธ์ œ๋ฅผ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. express-status-monitor ํŒจํ‚ค์ง€๊ฐ€ ์ด ์ž˜๋ชป๋œ ๋™์ž‘์„ ํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๊ฒƒ์„ ์ œ๊ฑฐํ•˜๋ ค๊ณ ํ•˜๋ฉด ์™„๋ฒฝํ•˜๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค

 // comment these lines, as they making the issue
 // const expressStatusMonitor = require('express-status-monitor'); 
 // app.use(expressStatusMonitor());

์ตœ์ข… ์ฝ”๋“œ:

let app = require('express')();

// these two lines were making the problem, please comment them. if you want to reproduce the problem enable them again 
// const expressStatusMonitor = require('express-status-monitor');
// app.use(expressStatusMonitor());

let http = require('http').Server(app);
let io = require('socket.io')(http);
let port = process.env.PORT || 3000;

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
});



app.get('/api/v0/availabilities',(req,res)=>{
    res.status(200).send({test:"test"});
});


io.on('connection', (socket) => {

    // emit message to group
    socket.on('emitMessage', (data) => {
        io.emit('emitMessage', data);
    });
});

http.listen(port, function(){
    console.log('listening on *:' + port);
});

์ด๊ฒƒ์€ ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์— ๋Œ€ํ•œ ๋‚ด ์งˆ๋ฌธ์ž…๋‹ˆ๋‹ค. https://stackoverflow.com/q/47375814/6786941

์ด๊ฒƒ์€ ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์— ๋Œ€ํ•œ ๋‚ด ๋Œ€๋‹ต์ž…๋‹ˆ๋‹ค. https://stackoverflow.com/a/47411701/6786941

@almgwary ์„ ์ƒ๋‹˜ ๋„ˆ๋ฌด ์ข‹์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋˜‘๊ฐ™์€ ๋ฌธ์ œ์— ์ง๋ฉดํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” expressStatusMonitor๋ฅผ ์ œ๊ฑฐํ–ˆ๊ณ  ๊ทธ๊ฒƒ์€ ๋งค๋ ฅ์ฒ˜๋Ÿผ ์ž‘๋™ํ–ˆ์Šต๋‹ˆ๋‹ค! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋งŽ์€ ๋‚ ์˜ ๊ฒ€์ƒ‰ ๋์— ๊ท€ํ•˜์˜ ์ œ์•ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งˆ์นจ๋‚ด ์ด๊ฒƒ์„ ํ•ด๊ฒฐํ•  ์ˆ˜์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ •๋ง ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰