2016-07-29 2 views
0

Я уже установленных node.js, Socket.io, predis, ioredis в Laravel 5,2локальный не посылать данные в Laravel 5,2 (Socket.io)

Когда я бегу

node socket.js

в gitbash, ничего не возвращается.

на локальном хосте: 3000, первый он загружает на некоторое время, то локальный не посылала отображается любая ошибка данных (сделано должны быть отображены)

socket.js файл:. http://laravel.io/bin/OeGxv

маршруты файла: http://laravel.io/bin/d9PvY

package.json: http://laravel.io/bin/Kk5mB

ответ

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'=>'[email protected]' 
]); 

Route::get('/getredis',[ 
    'as'=>'get.redis', 
    'uses'=>'[email protected]' 
]); 

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']; 
    } 
} 

Я надеюсь помочь вам!

+0

введите код, но когда я перейду на localhost: 300, не отображается GET /. –

+0

также на localhost: 8000/fire, ошибка: Class 'App \ Events \ EventName' не найден –

Смежные вопросы