问题描述
我正在尝试向在线状态频道广播。但是 auth 好像不发送会员数据。
版本
composer.json
"laravel/framework": "6.20.22","laravel/passport": "9.0","predis/predis": "1.1.7",
package.json
laravel-echo-server: 1.6.2
"socket.io-client": "2.4.0","laravel-echo": "1.10.0",
服务器端
.env
broADCAST_DRIVER=redis
QUEUE_CONNECTION=sync
REdis_HOST=127.0.0.1
REdis_PASSWORD=null
REdis_PORT=6379
REdis_PREFIX=
laravel-echo-server.json
{
"authHost": "http://localhost:8080","authEndpoint": "/custom/broadcasting/auth","clients": [],"database": "redis","databaseConfig": {
"redis": {},"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},"devMode": true,"host": null,"port": "6001","protocol": "http","socketio": {},"secureOptions": 67108864,"sslCertPath": "","sslKeyPath": "","sslCertChainPath": "","sslPassphrase": "","subscribers": {
"http": true,"redis": true
}
}
web.PHP
Route::post('/custom/broadcasting/auth',function (\Illuminate\Http\Request $request) {
Log::debug('custom auth');
$user = User::where('id',1)->first();
return [$user->name];
});
Route::post('/messages',function (\Illuminate\Http\Request $request) {
broadcast(new \App\Events\MyEvent($request->clientId,$request->message));
return [
'message' => $request->message
];
});
MyEvent.PHP
class MyEvent implements Shouldbroadcast
{
use dispatchable,InteractsWithSockets,SerializesModels;
public $message;
public $clientID;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($clientID,$message)
{
$this->message = $message;
$this->clientID = $clientID;
}
public function broadcastOn()
{
return new PresenceChannel('chat.' . $this->clientID);
}
}
'redis' => [
'client' => env('REdis_CLIENT','predis'),'options' => [
'cluster' => env('REdis_CLUSTER','redis'),'prefix' => env('REdis_PREFIX',Str::slug(env('APP_NAME','laravel'),'_').'_database_'),],'default' => [
'url' => env('REdis_URL'),'host' => env('REdis_HOST','127.0.0.1'),'password' => env('REdis_PASSWORD',null),'port' => env('REdis_PORT','6379'),'database' => env('REdis_DB','0'),'cache' => [
'url' => env('REdis_URL'),'database' => env('REdis_CACHE_DB','1'),
客户端
bootstrap.js
import Echo from "laravel-echo"
window.io = require('socket.io-client');
window.Echo = new Echo({
broadcaster: 'socket.io',host: window.location.hostname + ':8080',authEndpoint: '/custom/broadcasting/auth',authorizer: (channel,options) => {
return {
authorize: (socketId,callback) => {
axios.post('/custom/broadcasting/auth',{
socket_id: socketId,channel_name: channel.name
})
.then(response => {
callback(false,response.data);
})
.catch(error => {
callback(true,error);
});
}
};
},});
client.vue
export default {
props: ['clientId'],data: function () {
return {
messages: [],newMessage: '',statusMessage: 'Joined a room',isstarted: false,members: [],checkedMember: '',}
},created() {
},computed: {
channel() {
var joining = window.Echo.join(`chat.${this.clientId}`)
console.log(joining);
return window.Echo.join(`chat.${this.clientId}`)
}
},mounted() {
console.log(`Joining to chat.${this.clientId}`)
console.log('this channel',this.channel);
this.channel.here((users) => {
console.log(`Joined chat.${this.clientId}`)
this.members = users;
})
.joining((user) => {
console.log('Joining ',JSON.stringify(user));
this.members.put(user)
})
.leaving((user) => {
this.members = this.members.filter(member => user.id !== member.id)
console.log(`leaved ${user.name}`);
})
.error((error) => {
console.error(error);
})
.listen('MyEvent',(e) => {
console.log(e);
this.messages.push({
message: e.message.message,user: e.user
});
});
},methods: {
addMessage(clientId,message) {
axios.post('/messages',{
clientId,message
}).then(response => {
this.messages.push({
message: response.data.message.message,// user: response.data.user
});
});
},sendMessage() {
this.addMessage(this.clientId,this.newMessage);
this.newMessage = '';
},sendStartMessage() {
this.statusMessage = 'Started';
this.isstarted = true;
this.addMessage(this.clientId,'start');
},sendStopMessage() {
this.statusMessage = 'Ready';
this.isstarted = false;
this.addMessage(this.clientId,'stop');
}
}
}
</script>
console debug
SocketIoPresenceChannel {events: {…},listeners: {…},name: "presence-chat.my-client-id",socket: Socket,options: {…},…}
ㄴ eventFormatter: EventFormatter {namespace: "App.Events"}
ㄴ events: {presence:subscribed: ƒ,presence:joining: ƒ,presence:leaving: ƒ,App\Events\MyEvent: ƒ}
ㄴ listeners: {presence:subscribed: Array(1),presence:joining: Array(1),presence:leaving: Array(1),App\Events\MyEvent: Array(1)}
name: "presence-chat.my-client-id"
ㄴ options: {auth: {…},authEndpoint: "/custom/broadcasting/auth",broadcaster: "socket.io",csrftoken: null,host: "localhost:8080",…}
ㄴ socket: Socket {io: Manager,nsp: "/",json: Socket,ids: 0,acks: {…},…}
__proto__: SocketIoPrivateChannel
如果我尝试使用私人和公共频道,它会起作用。 但是,如果我尝试使用 Presence 频道,则始终无法正常工作。 laravel 回显服务器说
Event: [object Object]
(node:75981) UnhandledPromiseRejectionWarning: TypeError: Cannot convert undefined or null to object
at /Users/myname/.nvm/versions/node/v14.5.0/lib/node_modules/laravel-echo-server/dist/channels/presence-channel.js:78:21
at processticksAndRejections (internal/process/task_queues.js:93:5)
(node:75981) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
Unable to join channel. Member data for presence channel missing
有线事情是我可以监听事件。 你能告诉我问题吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)