所以我尝试在 Redis
的帮助下广播 Laravel 5 事件。不,我不想使用像 Pusher
这样的服务,因为它不是免费的(即使免费限制对我来说已经足够了)而且我想控制广播服务器。
所以到目前为止我所做的是,我已经设置了一个 redis 服务器(监听端口 6379 -> 默认),我已经设置了以下事件:
class MyEventNameHere extends Event implements ShouldBroadcast
{
use SerializesModels;
public $data;
/**
* Create a new event instance.
*
* @return \App\Events\MyEventNameHere
*/
public function __construct()
{
$this->data = [
'power' => 10
];
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['pmessage'];
}
}
我注册了一条通往该事件的路线:
Route::get('test',function()
{
event(new App\Events\MyEventNameHere());
return "event fired";
});
我已经创建(更像是复制 :P) Node 套接字服务器:
var app = require('http').createServer(handler);
var io = require('socket.io')(app, {origins:'*:*'});
var Redis = require('ioredis');
var redis = new Redis();
app.listen(6379, function() {
console.log('Server is running!');
});
function handler(req, res) {
res.writeHead(200);
res.end('');
}
io.on('connection', function(socket) {
console.log(socket);
});
redis.psubscribe('*', function(err, count) {
});
redis.on('pmessage', function(subscribed, channel, message) {
console.log(message);
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
我创建了实际接收广播的 View (testview.blade.php):
@extends('layout')
@section('content')
<p id="power">0</p>
<script>
var socket = io('http://localhost:6379');
socket.on("pmessage:App\\Events\\MyEventNameHere", function(message) {
console.log(message);
$('#power').text(message.data);
});
console.log(socket.connected);
</script>
@endsection
我可以毫无问题地启动 redis 服务器。 我可以启动 Node socket.js 服务器,我得到响应“服务器正在运行”
当我点击事件的路线时,我的浏览器中会返回“事件触发”。
当我到达实际 View 的路线时
Route::get('test/view',function()
{
return view('testview');
});
我可以看到整个页面(布局被渲染),webconsole 没有显示任何错误。
但是,如果我触发事件, View 不会改变,这意味着广播没有收到对吗?
现在我为控制台添加了一个输出
console.log(socket.connected);
如果 client
连接到 socket.io
应该显示我吗?
嗯,输出显示 false
。我在这里做错了什么?
关于我的设置的更多信息:我在 php 内置服务器上运行整个项目,整个项目都在 Windows 上运行(如果有的话),我的防火墙没有阻止任何端口。
编辑 1:
我忘了说我的 Node 服务器也没有收到消息......它只说“服务器正在运行”,没有别的。
编辑 2:
我使用了另一个 socket.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');
});
这一次控制台接收到消息。 因此,如果 Node socket.io 收到消息,那么我的客户端出了什么问题?显然消息正在正确广播,唯一的问题是客户端没有收到它们......
最佳答案
我不能说到底出了什么问题,可能没有人不能说,因为你的问题是广泛的和环境相关的。使用 Wireshark Sniffer您可以轻松确定部分解决方案无法正常工作,然后尝试围绕实际问题找到解决方案。
如果您的问题是关于如何做到这一点,我建议不要在服务器端涉及 Node 并使用 .NET 或 Java 语言。
关于javascript - 带有 Redis 的 Laravel 广播无法工作/连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31263574/