我已经开始使用 Laravel 5.1 并且它非常棒,只是想按照此处的指南使用 NodeJs 作为服务器并使用 Redis 作为驱动程序来使用新的“广播事件”功能:http://blog.nedex.io/laravel-5-1-broadcasting-events-using-redis-driver-socket-io/ .当我触发一个实现 ShouldBroadcast 接口(interface)的事件时,我收到一个错误: “从服务器读取线路时出错。[tcp://127.0.0.1:4365]”
4365 - 是服务器运行的端口(监听那个端口)。您知道为什么会这样吗?
我也试过直接使用Redis:
$redis = Redis::connection();
$redis->publish('test-channel', 'msg');
得到相同的结果,“从服务器读取行时出错。[tcp://127.0.0.1:4365]”。
套接字.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();
redis.subscribe('test-channel', function(err, count) {
});
redis.on('message', function(channel, message) {
console.log('Message Recieved: ' + message);
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.payload);
});
http.listen(4365, function(){
console.log('Listening on Port 4365');
});
配置\数据库.php:
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 4365,
'database' => 0,
'timeout' => 100,
],
],
尝试更改 defulat 超时,将其设置为 0、-1 或 >10 还尝试在 php.ini 中禁用 Xdebug,问题仍然存在。
我对代码进行了深入调试,试图了解可能导致此问题的原因,但它在类中失败了:Predis\Connection\StreamConnection
public function read()
{
$socket = $this->getResource();
$chunk = fgets($socket);
if ($chunk === false || $chunk === '') {
$this->onConnectionError('Error while reading line from the server.');
}
...
为什么 block 是假的? 为什么 redis 客户端试图从服务器读取数据,据我了解,它应该“发布”数据到服务器,这意味着它应该写入(广播)而不是读取..
最佳答案
我觉得有什么误解Redis是并且确实。
Redis is an open source, BSD licensed, advanced key-value cache and store.
它通常用于进程间通信 (IPC),它有很好的 PUBLISH 和 SUBSCRIBE 命令。
因此对于 Laravel 和 Node,Redis 可以实现 Node 和 Laravel 的永久运行进程之间的通信。 Laravel 将其事件发布到 Redis,Node 订阅 Redis 的特定 channel 。在这里,您可以通过套接字连接发出一条消息,很可能 channel 名称是相同的。
要使这一切正常工作,您必须执行以下操作 (very well explained in the docs):
- 在您的系统上安装 Redis。标准端口为 6379。
- 在composer.json中设置依赖:predis/predis ~1.0
- 在 config/database.php 中配置 Redis。
- 在 config/broadcasting.php 中将 Redis 设置为默认广播。
- 在 config/queue.php 中设置默认队列驱动程序。 (注意:如果我将 Redis 设置为队列驱动程序,事件发布对我不起作用。但是直接发布正在工作
Redis::publish('test-channel', json_encode(['foo' => 'bar'] ));
。可能是 Bug? - 现在您可以创建和触发实现
ShouldBroadcast
的事件。出于测试目的,您可以使用:Redis::publish('test-channel', json_encode(['foo' => 'bar']));
现在是时候切换到 Node 了:
您的 socket.js 代码看起来不错,假设 Redis 在端口 6379 上。如果 Node 在本地运行,您可以在浏览器中连接到您的 Node 服务器,如下所示:localhost:4365
。
这是我在前端和后端测试 Node 和 Socket.io 的代码:
套接字.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(3000, function(){
console.log('listening on *:3000');
});
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>
希望对您有所帮助:)
关于node.js - 使用Redis在Laravel 5.1中广播事件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30819872/