0
我正在運行webpack-dev-server on localhost:8000 for my app and express + socket.io on the port 3000 for my api。我代理請求在webpack.config.js這樣給socket.io:session.id更改和socket.io不匹配
devServer: {
proxy: {
'/socket.io': {
target: 'http://localhost:3000',
ws: true
}
}
}
然而,不僅做到了會話ID的快遞和socket.io不匹配,會話ID在表達的改變每一個請求:
服務器:
let app = require('express')();
let session = require('express-session')({
secret: 'panopticon',
resave: true,
saveUninitialized: true
});
let server = require('http').createServer(app);
let io = require('socket.io')(server);
//session middleware
app.use(session);
io.use(require('express-socket.io-session')(session, {
autoSave: true
}));
let i=0;
app.get('/socket.io', (req, res) => {
console.log(i++, req.session.id);
//0 'ShgnU91kCZzC7xHP9B57ZtsCbwi3XjdB'
//1 'qLsYYpRZXpyoUrcKzF6K7uoAIKtE9oCh'
res.send();
});
io.on('connection', socket => {
console.log(socket.handshake.session.id);
//MRUYZMVstMh6ssNrq9LP-Z4vTaT5SZcs
});
客戶:
//connect to socket
let socket = io();
//make two requests to /socket.io
fetch('socket.io').then(() => fetch('socket.io'));
嗨,我想我有類似的問題。你會如此友善地將你的代碼發佈到git上嗎? – Freddie
@Freddie加入了Gist回答 – Thiatt