gpt4 book ai didi

node.js - "could not look up session by key: connect.sid"- session.socket.io

转载 作者:搜寻专家 更新时间:2023-10-31 22:48:41 26 4
gpt4 key购买 nike

我是 NodeJS 开发的新手。我正在使用 session.socket.io我的 express 应用程序中的插件,但是当我调试应用程序时出现此错误 “无法按键查找 session :connect.sid”,在 sessionSockets.on('connection', ...

这是整个 app.js:

var http = require('http'),
connect = require('express/node_modules/connect'),
express = require('express'),
app = express();

var cookieParser = express.cookieParser('Your secret key here'),
sessionStore = new connect.middleware.session.MemoryStore();

app.configure(function() {
app.set('port', process.env.PORT || 3000);
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(cookieParser);
app.use(express.session({
store: sessionStore
}));
app.use(app.router);

});

var server = http.createServer(app),
io = require('socket.io').listen(server);

var SessionSockets = require('session.socket.io'),
sessionSockets = new SessionSockets(io, sessionStore, cookieParser);

app.configure('development', function() {
app.use(express.errorHandler());
});

app.get('/', function(req, res) {
res.end("Welcome to the API server");
});

sessionSockets.on('connection', function(err, socket, session) {

console.log('-----------------------');
console.log(err.error);
console.log('-----------------------');

socket.emit('welcome', {
msg: 'You\'re now listening to the api.'

});

});

server.listen(app.get('port'), function() {
console.log("Express server listening on port " + app.get('port'));
});

客户端:

<!DOCTYPE html>

<head>
<meta charset="utf-8">
<title>Express+SocketIO testing</title>
</head>

<body>

<div id="responses">
<h2>API responses:</h2>
<ul></ul>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>

<script>

$(document).ready(function() {

var socket = io.connect('//localhost:3000');

// Welcome message
socket.on('welcome', function(data) {
var li = '<li>Server said: ' + data.msg + '</li>';
$('#responses ul').append(li);
});
});

</script>
</body>
</html>

包.json

{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app"
},
"dependencies": {
"express": "3.0.3",
"socket.io":"*",
"session.socket.io":"*"
}
}

欢迎任何帮助或提示。谢谢你。

最佳答案

这发生在我使用 Java socket.io 客户端时,因为 Java 无法访问 node.js session 。

所以,我决定最好不要使用 session.socket.io 插件。一个更通用的解决方案,可以适用于所有类型的客户端,是: 将 session 数据(或 session ID)发送到客户端,可能是通过将 Javascript 片段插入客户端代码;然后让客户端将 session ID 发送回服务器。

关于node.js - "could not look up session by key: connect.sid"- session.socket.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13849517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com