socket.io导致连接时php redis读取错误?
我使用php(Laravel5.8)进行广播,我使用socket.io导致连接时php redis读取错误?,php,node.js,redis,socket.io,Php,Node.js,Redis,Socket.io,我使用php(Laravel5.8)进行广播,我使用LaravelEcho服务器,这是我的工作。但是最近我需要自己做点什么。我用ndoejs编写了一个套接字服务。这是我的密码 const jwt = require('jsonwebtoken'); const server = require('http').Server(); const io = require('socket.io')(server); ... // ==== The question is here =======
LaravelEcho服务器
,这是我的工作。但是最近我需要自己做点什么。我用ndoejs编写了一个套接字服务。这是我的密码
const jwt = require('jsonwebtoken');
const server = require('http').Server();
const io = require('socket.io')(server);
...
// ==== The question is here =====================
const Redis = require('ioredis');
const redis = new Redis({
port: REDIS_PORT,
host: REDIS_HOST,
password: REDIS_PASSWORD
});
redis.psubscribe('myTestChannel.*');
redis.on('pmessage', function(pattern, channel, message) {
console.log(channel, message);
const object_message = JSON.parse(message);
io.sockets.emit(channel, object_message.data);
});
// ==== The question is here =====================
io.sockets.use(function (socket, next) {
if (socket.handshake.query && socket.handshake.query.token){
// auth
} else {
next(new Error('Authentication error'));
}
}).on('connection', function(socket) {
console.log('==========connection============');
console.log('Socket Connect with ID: ' + socket.id);
socket.on('join', (data) => {
... do something
});
socket.on('disconnect', () => {
... do something
})
});
server.listen(LISTEN_PORT, function () {
console.log(`Start listen ${LISTEN_PORT} port`);
});
这是可行的,但运行了很长时间后,我的php收到一条错误消息,phpredis读取连接错误。我不确定真正的原因。但我猜是关于我的套接字,因为我使用的laravel echo服务器很棒
我不确定我的redis.psubscribe的位置是否正确?可能这会导致长时间连接,并导致连接上的php读取错误
我应该将redis.psubscribe
移动到on('connection')
并在断开连接时取消订阅
我想知道redis.psubscribe是问题的主要原因吗?谢谢你的帮助