Laravelを勉強しているものです。
以下のサイトを参考にWindowsにLaravel環境を構築しPusherを使って通知を行うアプリを作ろうとしています。
http://shingaki.me/web/laravel%E3%81%AEbroadcasting-event%E3%82%92pusher%E3%81%A7%E3%82%84%E3%82%8B/
しかし、実装後に動作確認を行ったところlocalhost:8000/helloにアクセスしたときにブラウザのコンソールにERR_CONNECTION_REFUSEDが表示されます
実施手順:
①php -S localhost:8000 -t publicで起動する
②Google Chromeからlocalhost:8000にアクセスする
③Google Chromeからlocalhost:8000/helloにアクセスする
コマンドプロンプトのコンソールです。
php -S localhost:8000 -t public
PHP 7.3.0 Development Server started at Thu Jan 10 10:05:22 2019
Listening on http://localhost:8000
Document root is (プロジェクトパス)\public
Press Ctrl-C to quit.
[Thu Jan 10 10:05:23 2019] [::1]:54300 [200]: /
[Thu Jan 10 10:05:23 2019] [::1]:54301 [200]: /favicon.ico
[Thu Jan 10 10:05:30 2019] [::1]:54316 [200]: /hello
以下ソースコードです。
welcome.blade.php
html
1<!DOCTYPE html> 2<head> 3 <title>Pusher Test</title> 4 <script src="https://js.pusher.com/4.1/pusher.min.js"></script> 5 <script> 6 7 // Enable pusher logging - don't include this in production 8 Pusher.logToConsole = true; 9 10 var pusher = new Pusher('(PUSHER_APP_KEYの値を設定)', { 11 cluster: 'ap3', 12 encrypted: true 13 }); 14 15 var channel = pusher.subscribe('my-channel'); 16 channel.bind('my-event', function(data) { 17 alert(data.message); 18 }); 19 </script> 20</head>
config/broadcasting.php
php
1<?php 2 3return [ 4 5 'default' => env('BROADCAST_DRIVER', 'null'), 6 7 'connections' => [ 8 9 'pusher' => [ 10 'driver' => 'pusher', 11 'key' => env('PUSHER_APP_KEY'), 12 'secret' => env('PUSHER_APP_SECRET'), 13 'app_id' => env('PUSHER_APP_ID'), 14 'options' => [ 15 'cluster' => env('PUSHER_APP_CLUSTER'), 16 'encrypted' => true, 17 ], 18 ], 19 20 'redis' => [ 21 'driver' => 'redis', 22 'connection' => 'default', 23 ], 24 25 'log' => [ 26 'driver' => 'log', 27 ], 28 29 'null' => [ 30 'driver' => 'null', 31 ], 32 33 ], 34 'pusher' => [ 35 'driver' => 'pusher', 36 'key' => env('PUSHER_APP_KEY'), 37 'secret' => env('PUSHER_APP_SECRET'), 38 'app_id' => env('PUSHER_APP_ID'), 39 'options' => [ 40 'cluster' => env('PUSHER_APP_CLUSTER'), 41 'encrypted' => env('PUSHER_APP_ENCRYPTED'), 42 ], 43 ], 44]; 45
HelloPusher
php
1<?php 2 3namespace App\Events; 4 5use Illuminate\Broadcasting\Channel; 6use Illuminate\Queue\SerializesModels; 7use Illuminate\Foundation\Events\Dispatchable; 8use Illuminate\Broadcasting\InteractsWithSockets; 9use Illuminate\Contracts\Broadcasting\ShouldBroadcast; 10 11class HelloPusher implements ShouldBroadcast 12{ 13 use Dispatchable, InteractsWithSockets, SerializesModels; 14 15 /** 16 * Create a new event instance. 17 * 18 * @return void 19 */ 20 public function __construct() 21 { 22 // 23 } 24 25 /** 26 * Get the channels the event should broadcast on. 27 * 28 * @return Channel|array 29 */ 30 public function broadcastOn() 31 { 32 return new Channel('my-channel'); 33 } 34 35 public function broadcastAs() 36 { 37 return "my-event"; 38 } 39 40 public function broadcastWith() 41 { 42 return [ 43 'message' => 'Hello Pusher!!' 44 ]; 45 } 46} 47
web.php
php
1<?php 2 3Route::get('/', function () { 4 return view('welcome'); 5}); 6 7Route::get('/hello', function(){ 8 event(new \App\Events\HelloPusher); 9});
.envファイルに以下を追加
PUSHER_APP_ID=xxxxx
PUSHER_APP_KEY=xxxxx
PUSHER_APP_SECRET=xxxxx
PUSHER_APP_CLUSTER=xxxx
PUSHER_APP_ENCRYPTED=true
Pusherを利用したことのある方がいましたらご回答いただけないでしょうか?
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。