Guest User

Untitled

a guest
Oct 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. {"event":"App\Events\EventName","data":{"data":{"power":"10"}},"socket":null}
  2.  
  3. <?php
  4.  
  5. /*
  6. |--------------------------------------------------------------------------
  7. | Web Routes
  8. |--------------------------------------------------------------------------
  9. |
  10. | Here is where you can register web routes for your application. These
  11. | routes are loaded by the RouteServiceProvider within a group which
  12. | contains the "web" middleware group. Now create something great!
  13. |
  14. */
  15.  
  16. // Route::get('/', function () {
  17. // return view('welcome');
  18. // });
  19.  
  20. Route::get('/', function() {
  21. // this doesn't do anything other than to
  22. // tell you to go to /fire
  23. return "go to /fire";
  24. });
  25.  
  26. Route::get('fire', function () {
  27. // this fires the event
  28. event(new AppEventsEventName());
  29. return "event fired";
  30. });
  31.  
  32. Route::get('test', function () {
  33. // this checks for the event
  34. return view('test');
  35. });
  36.  
  37. BROADCAST_DRIVER=redis
  38. CACHE_DRIVER=file
  39. SESSION_DRIVER=file
  40. QUEUE_DRIVER=sync
  41.  
  42. REDIS_HOST=127.0.0.1
  43. REDIS_PASSWORD=null
  44. REDIS_PORT=6379
  45.  
  46. PUSHER_APP_ID=
  47. PUSHER_APP_KEY=
  48. PUSHER_APP_SECRET=
  49.  
  50. var app = require('express')();
  51. var http = require('http').Server(app);
  52. var io = require('socket.io')(http);
  53. var Redis = require('ioredis');
  54. var redis = new Redis();
  55. redis.subscribe('test-channel', function(err, count) {
  56. });
  57.  
  58. redis.on('message', function(channel, message) {
  59. console.log('Message Recieved: ' + message);
  60. message = JSON.parse(message);
  61. io.emit(channel + ':' + message.event, message.data);
  62. });
  63.  
  64.  
  65. http.listen(3000, function(){
  66. console.log('Listening on Port 3000');
  67. });
  68.  
  69. namespace AppEvents;
  70.  
  71. use IlluminateBroadcastingChannel;
  72. use IlluminateQueueSerializesModels;
  73. use IlluminateContractsBroadcastingShouldBroadcast;
  74.  
  75. class EventName implements ShouldBroadcast
  76. {
  77. use SerializesModels;
  78.  
  79. /**
  80. * Create a new event instance.
  81. *
  82. * @return void
  83. */
  84.  
  85.  
  86. public $data;
  87.  
  88. public function __construct()
  89. {
  90. $this->data = array(
  91. 'power'=> '10'
  92. );
  93. }
  94.  
  95. public function broadcastOn()
  96. {
  97. return ['test-channel'];
  98. }
  99. }
  100.  
  101. @extends('layouts.master')
  102.  
  103. @section('content')
  104. <p id="power">0</p>
  105. @stop
  106.  
  107. @section('footer')
  108. <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
  109.  
  110. <script>
  111.  
  112. var socket = io('http://localhost:3000');
  113. // var socket = io('http://192.168.10.10:3000');
  114. socket.on("test-channel:App\Events\EventName", function(message){
  115. // increase the power everytime we load test route
  116.  
  117. $('#power').text(parseInt($('#power').text()) + parseInt(message.data.power));
  118. $('#power').text("testeste");
  119. // $('#power').text(parseInt($('#power').text()) + parseInt(message.data.power));
  120. });
  121. </script>
  122. @stop
Add Comment
Please, Sign In to add comment