php - socket.io 在连接时导致 php redis 读取错误?
问题描述
我使用 php(laravel 5.8) 进行广播,我使用laravel-echo-server
,它的工作。但!!!最近我需要自己做点什么。我socket service
用 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 read error on connection。我不确定真正的原因。但我猜是关于我的套接字,因为我使用 laravel-echo-server 很棒。
我不确定我redis.psubscribe
的位置是否正确?也许这会导致长时间连接并导致连接时出现php读取错误?
断开连接时我应该移入redis.psubscribe
并取消订阅吗?on('connection')
我想知道redis.psubscribe
问题的主要原因是什么?谢谢你的帮助。
解决方案
推荐阅读
- python-3.x - 在后台定期运行 4 个函数而不冻结 GUI
- python - 了解使用编码/解码来去除变音符号
- c# - 如何在 Asp.Net MVC WebApi 中执行异步事件
- laravel - Laravel:如果从特征调用,则找不到类
- php - tcpdf 尝试使用 Heredoc 创建具有双 foreach 的表行时出错
- html - 猫头鹰轮播:第一张幻灯片不触发动作
- c++ - 没有相应弹出的 pragma pack(push) 导致堆栈粉碎
- python - 如何在 Python 字符串中包含转义字符
- mysql - MySQL - 不显示来自 ID 使用的任何行
- scala - 在 rdd 中使用 scala 过滤器功能时的问题