【问题标题】:Laravel 5.1 working with redisLaravel 5.1 使用 redis
【发布时间】:2015-09-26 22:46:06
【问题描述】:

这是我的 Routes.php

Route::get('hello1',function(){
    Redis::publish('test-channel', json_encode(['foo' => 'bar']));
});

这是我的 app.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();


app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
});


redis.subscribe('test-channel', function () {
    console.log('Redis: test-channel subscribed');
});

redis.on('message', function(channel, message) {
    console.log('Redis: Message on ' + channel + ' received!');
    console.log(message);
    message = JSON.parse(message);
    io.emit(channel, message.payload)
});

io.on('connection', function(socket){
    console.log('a user connected');
    socket.on('disconnect', function(){
        console.log('user disconnected');
    });
});


http.listen(6379, function(){
    console.log('listening on *:6379');
});

我的 index.html

<!doctype html>
<html>
<head>
    <title>Socket.IO</title>
</head>
<body>
<ul id="messages">
    <li>Hardcoded Message</li>
</ul>

<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
    var socket = io();
    socket.on("test-channel", function(message) {
        console.log(message);
        $('#messages').append($('<li>').text(message));
    });
</script>
</body>
</html>

现在无论何时运行 php artisan redis:subscribe 或 hello1 route 我都会收到类似的错误,

AbstractConnection.php 第 146 行中的 ConnectionException:出错时 从服务器读取线。 [tcp://localhost:6379]

【问题讨论】:

    标签: php node.js redis laravel-5


    【解决方案1】:

    Redis 默认侦听端口 6379,并且您已将 http 服务器设置为侦听同一端口。这不起作用,您在尝试运行节点应用程序时不会出错吗?如果你没有收到错误,redis 很可能根本没有运行或正在侦听另一个端口。

    如果您的 redis 实例正在侦听另一个端口,laravel 将尝试连接到端口 6379 上的 redis 并将访问节点服务器,因此,由于没有 redis 但节点 http 服务器正在侦听,因此无法连接到 redis在这个港口。

    如果您的 redis 实例正在侦听另一个端口,并且您在 laravel 配置中对此进行了更改,则必须通过传递端口来更改将 ioredis 连接到 redis 的方式:

    例如

    var redis = new Redis(6380) 
    

    有关将 ioredis 连接到 redis 的更多详细信息,请参阅github page

    作为结论,请确保

    1. Redis 正在运行
    2. 您的 http 服务器正在侦听 redis 以外的另一个端口
    3. 您在 config/database.php 和 config/broadcast.php (laravel) 中的 redis 连接设置以及 server.js 文件中的节点连接设置正确

    注意不是http服务器正在连接redis,ioredis是。

    【讨论】:

    • 谢谢迈克尔先生。安装redis服务器并通过dos模式启动一切正常。但是每当我试图通过节点 js 启动 redis 服务器时,我得到的错误是 events.js:85 throw er; // 未处理的“错误”事件 ^ 错误:Redis 连接到 127.0.0.1:6379 失败 - 在 RedisClient.on_error 读取 ECONNRESET 我不明白为什么
    • 不客气! Redis 需要在从节点启动连接之前运行。 Node 不会使用您拥有的代码启动它。这里有一个关于正确设置 redis 的很好的解释
    • 但我担心我现在如何在真实的实时服务器上使用这些对我来说很容易,因为我在 Windows 上进入 CLI 模式并为 redis 服务器打开端口:) 无论如何谢谢你迈克尔感谢您,先生,您为我节省了很多时间 1000
    猜你喜欢
    • 2015-08-29
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 2015-09-10
    • 2015-12-25
    • 2016-03-10
    • 2016-07-13
    相关资源
    最近更新 更多