我尝试在工作项目中实现聊天,该项目包含几种类型的用户。
from bs4 import BeautifulSoup
# remove all attributes
def _remove_all_attrs(soup):
for tag in soup.find_all(True):
tag.attrs = {}
return soup
# remove all attributes except some tags
def _remove_all_attrs_except(soup):
whitelist = ['a','img']
for tag in soup.find_all(True):
if tag.name not in whitelist:
tag.attrs = {}
return soup
# remove all attributes except some tags(only saving ['href','src'] attr)
def _remove_all_attrs_except_saving(soup):
whitelist = ['a','img']
for tag in soup.find_all(True):
if tag.name not in whitelist:
tag.attrs = {}
else:
attrs = dict(tag.attrs)
for attr in attrs:
if attr not in ['src','href']:
del tag.attrs[attr]
return soup
事件文件
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'company' => [
'driver' => 'session',
'provider' => 'companies',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
控制器
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\ChartMessages;
class MessageSent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $chartMessage;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(ChartMessages $chartMessage)
{
$this->chartMessage = $chartMessage;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PresenceChannel('chart');
}
}
Vue
namespace App\Http\Controllers;
use Auth;
use App\User;
use Illuminate\Http\Request;
use App\ChartMessages;
use App\Events\MessageSent;
class ChartController extends Controller
{
public function __contruct(){
$this->middalware('auth');
}
public function index($type, $to_id)
{
if($type == 'company'){
$company = Auth::guard('company')->user();
if($company && isset($company->id)){
return ChartMessages::where(['from' => 'company', 'from_id' => $company->id])->orWhere(['to' => 'company', 'to_id' => $company->id])->get();
}
}
if($type == 'user'){
if(Auth::user() && isset(Auth::user()->id)){
$user = User::findOrFail(Auth::user()->id);
return ChartMessages::where(['to' => 'user', 'to_id' => $user->id])->orWhere(['from' => 'user', 'from_id' => $user->id])->get();
}
}
}
public function sendMessage(Request $request){
$chartMessage = ChartMessages::create([
'from' => 'company',
'to' => 'user',
'from_id' => $request->from_id,
'to_id' => $request->to_id,
'from_name' => $request->from_name,
'to_name' => $request->to_name,
'message' => $request->message
]);
broadcast(new MessageSent($chartMessage))->toOthers();
return ['status' => 'success'];
}
}
在channels.php文件中
export default {
props: ['to', 'from', 'type'],
data() {
return {
messages: [],
newMessage: ''
}
},
created(){
this.fetchMessages();
Echo.join('chart').
listen('MessageSent', (event) => {
this.messages.push(event.chartMessage);
})
},
methods:{
fetchMessages(){
axios.get('/chart-messages/'+this.type+'/'+this.to.id).then( response => {
this.messages = response.data;
})
},
sendMessage(){
this.messages.push({
to_id: this.to.id,
from_id: this.from.id,
to_name: this.to.name,
from_name: this.from.name,
message: this.newMessage
});
axios.post('/send-message', {'message': this.newMessage, 'from_id': this.from.id, 'to_id': this.to.id, 'from_name': this.from.name, 'to_name': this.to.name,});
this.newMessage = '';
}
}
}
为此,我在文件BroadcastServiceProvider中添加了
Broadcast::channel('chart', function () { return (Auth::guard('company')->check() || Auth::check());});
现在,当类型为user的用户发送消息时,他们会收到所有人以及类型为user和company的用户。
但是,当公司的用户发送消息时,只有像公司这样的用户才能看到其消息,而类型用户则看不到。
任何想法如何制作,以便公司等用户的消息都能看到任何人。