我已经在laravel 5.2 中安装了node.js、socket.io、predis、ioredis
当我运行时
节点套接字.js
在gitbash中,不会返回任何内容。
在localhost:3000,首先加载一段时间,然后显示localhost未发送任何数据错误。(应显示已完成)
socket.js文件:http://laravel.io/bin/OeGxv
路由文件:http://laravel.io/bin/d9PvY
package.json:http://laravel.io/bin/Kk5mB
我认为这对你没有帮助,但我成功地将redis与Laravel 5.1一起使用,这就是代码。
composer.json
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"pusher/pusher-php-server": "^2.2",
"predis/predis": "^1.1"
routes.php
Route::get('/setredis',[
'as'=>'set.redis',
'uses'=>'TestController@index'
]);
Route::get('/getredis',[
'as'=>'get.redis',
'uses'=>'TestController@create'
]);
Route::get('fire', function () {
// this fires the event
event(new AppEventsEventName());
return "event fired";
});
Route::get('test', function () {
// this checks for the event
return view('test');
});
TestController.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
use AppHttpControllersController;
use Redis;
class TestController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
Redis::set('name', 'Taylor');
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
$user = Redis::get('name');
echo $user;
}
testblade.php
@extends('master')
@section('content')
<p id="power">0</p>
@stop
@section('footer')
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>
var socket = io('http://testlaravel5.com:3000');
socket.on("test-channel", function(message){
console.log(message);
// increase the power everytime we load test route
$('#power').text(parseInt($('#power').text()) + parseInt(message.data.power));
});
</script>
@stop
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.data);
});
http.listen(3000, function(){
console.log('Listening on Port 3000');
});
EventName.php
<?php
namespace AppEvents;
use AppEventsEvent;
use IlluminateQueueSerializesModels;
use IlluminateContractsBroadcastingShouldBroadcast;
class EventName extends Event implements ShouldBroadcast
{
use SerializesModels;
public $data;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
$this->data = array(
'power'=> '10'
);
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['test-channel'];
}
}
我希望能帮助你