Socket.io: Nodejs Socket.io Expressは、HTTPAPIを呼び出した後にハンドシェイクに失敗しました

作成日 2017年11月19日  ·  2コメント  ·  ソース: socketio/socket.io

あなたはしたい:

  • [x]バグを報告する

現在の動作

私はマニュアルを参照して、その後、NodejsSocketIo、ExpressサーバのMongoDBを使用しています。 複数のクライアントを接続する場合、問題なく相互にメッセージを送信できます。 Httpリクエストを行ったとき、新しいクライアントに接続できず、このエラーが発生します。

socket.io.js:7370WebSocket接続から
'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 評価