2017-02-07 20 views
5

Đây là server.js tập tin của tôi:Làm thế nào để bảo vệ các kênh riêng khi sử dụng socket.io và Laravel Echo?

var app = require('express')(); 
var http = require('http').Server(app); 
var io = require('socket.io')(http); 
var Redis = require('ioredis'); 
var redis = new Redis(); 

http.listen(3000, function(){ 
    console.log('Listening on Port 3000'); 
}); 

redis.psubscribe('*', function(err, count) { 
    console.log(err, count); 
}); 
redis.on('pmessage', function(subscribed, channel, message) { 
    message = JSON.parse(message); 
    io.emit(message.event, channel, message.data); 
}); 

Và một sự kiện đơn giản:

class FieldWasUpdated implements ShouldBroadcast 
{ 
    use InteractsWithSockets, SerializesModels; 
    public $instance; 
    public $key; 
    public $value; 

    public function __construct($instance, $key, $value) 
    { 
     $this->instance = $instance; 
     $this->key = $key; 
     $this->value = $value; 
    } 

    public function broadcastOn() 
    { 
     return new PrivateChannel("model." . $this->instance->getModelType() . "." . $this->instance->id); 
    } 
} 

Client kết nối đến socket.io:

Echo = new Echo({ 
    broadcaster: 'socket.io', 
    host: window.location.hostname + ':3000' 
}); 

Và sau đó lắng nghe cho các sự kiện (nó là bên trong mẫu Blade):

var channel = "model.{{ $instance->getModelType() }}.{{ $instance->id }}"; 
Echo.private(channel) 
    .listen("FieldWasUpdated", function(e) { 
     window.VueBus.$emit("updated", channel, e.key, e.value); 
    }) 
    .listen("FieldBecameDisabled", function(e) { 
     window.VueBus.$emit("disabled", channel, e.key); 
    }); 

Sự cố là: xác thực không được xử lý, mọi người dùng đều có thể đăng ký các kênh này.

Broadcast::channel("model.announcement.*", function($user, $id) { 
    return false; // this function is not called 
}) 

Đây là một sự kiện mẫu từ Chrome phát triển giao diện điều khiển (WebSocket):

[ 
    "App\\Events\\FieldWasUpdated", 
    "private-model.announcement.2", 
    { 
     "instance": 
     { 
      "type":"ANN_TYPE_MISSED_CALL", 
      "status":"ANN_STATUS_CANCELLED", 
      "name":"1233421", 
      "phone":"+7(222)222-3322", 
      "email":"[email protected]", 
      "message":"sdgdsgsdgdfdg", 
      "requirement":null, 
      "web_type":"ANN_WEB_TYPE_UNKNOWN", 
      "url":null, 
      "responsible_id":19, 
      "recommender_id":18 
     }, 
     "key":"message", 
     "value":"sdgdsgsdgdfdg" 
    } 
] 

Cũng không có /broadcast/auth URL, nhưng BroadcastServiceProvider có một cuộc gọi đến Broadcast::routes(); và khi tải trình duyệt, không có cuộc gọi đến /broadcast/auth xảy ra.

+0

thử phát sóng :: kênh ("model.announcement. *", Chức năng ($ user, $ id) { hủy bỏ (401); }) –

Trả lời

Các vấn đề liên quan