我已经在laravel 5.2中安装了node.js,socket.io,predis,ioredis
当我跑步时
node socket.js
在gitbash中,没有返回任何内容。
在localhost:3000,首先加载一段时间然后localhost没有发送任何数据错误显示。(应该显示完成)
socket.js文件:http://laravel.io/bin/OeGxv
路线档案:http://laravel.io/bin/d9PvY
package.json:http://laravel.io/bin/Kk5mB
答案 0 :(得分:0)
我认为不能帮助你,但我成功地将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 \App\Events\EventName());
return "event fired";
});
Route::get('test', function () {
// this checks for the event
return view('test');
});
<强> TestController.php 强>
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Redis;
class TestController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
Redis::set('name', 'Taylor');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$user = Redis::get('name');
echo $user;
}
<强> test.blade.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 App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
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'];
}
}
我希望能帮到你!