我使用的是Laravel 5.4,VirtualBox和Homestead。我希望每次创建新用户时都能发送广播,但是当我转到我的测试时,它不会更新power
,但我的节点实例会记录此
Message Recieved: {"event":"App\\Events\\EventName","data":{"data":{"power":"10"},"socket":null},"socket":null}
我假设它意味着它有效。我做错了什么?
api.php
Route::get('/', function() {
// this doesn't do anything other than to
// tell you to go to /fire
return "go to /fire";
});
Route::get('fire', function () {
// this fires the event
event(new App\Events\EventName());
return "event fired";
});
Route::get('test', function () {
// this checks for the event
return view('test');
});
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();
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.data);
});
http.listen(3000, function(){
console.log('Listening on Port 3000');
});
事件名称
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class EventName implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $data;
public function __construct()
{
$this->data = array(
'power'=> '10'
);
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
/* return new PrivateChannel('test-channel');*/
return ['test-channel'];
}
}
master.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FiveOne Socket.io</title>
</head>
<body>
@yield('content')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
@yield('footer')
</body>
</html>
test.blade.php
@extends('layouts.master')
@section('content')
<p id="power">0</p>
@stop
@section('footer')
<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
<script>
//var socket = io('http://localhost:3000');
//var socket = io('http://192.168.10.10:3000');
var socket = io(window.location.origin + ':3000');
socket.on("test-channel:App\\Events\\EventName", function(message){
// increase the power everytime we load test route
$('#power').text(parseInt($('#power').text()) + parseInt(message.data.power));
});
</script>
@stop
.ENV
BROADCAST_DRIVER=redis
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=192.168.10.10
REDIS_PASSWORD=null
REDIS_PORT=6379
redis.conf
bind 127.0.0.1 192.168.10.10
的package.json
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.16.2",
"bootstrap-sass": "^3.3.7",
"cross-env": "^5.0.1",
"ioredis": "^3.1.4",
"jquery": "^3.1.1",
"laravel-mix": "^1.0",
"lodash": "^4.17.4",
"socket.io": "^2.0.3",
"socket.io-client": "^2.0.3",
"vue": "^2.1.10"
},
"dependencies": {
"express": "^4.15.4",
"ioredis": "^3.1.4",
"socket.io": "^2.0.3"
}
}
编辑:我只是尝试记录套接字连接并返回false。我认为这是宅基地的一个问题,但我不确定
<script>
var socket = io('http://192.168.10.10:3000');
console.log(socket.connected);
//alert(window.location.origin + ':3000');
socket.on("test-channel:App\\Events\\EventName", function(message){
// increase the power everytime we load test route
$('#power').text(parseInt($('#power').text()) + parseInt(message.data.power));
});
</script>