我一直在尝试为简单的聊天实施this教程。套接字显示断开连接并显示WebSocket is already in CLOSING or CLOSED state.
后已连接,但“ MessageSent”上的事件侦听器不起作用。在数据库中创建一条消息。
这是服务器
var server = require('http').Server();
var Redis = require('ioredis');
var redis = new Redis();
var io = require('socket.io')(server);
io.set('origins', '*:*');
redis.psubscribe('*');
redis.on('pmessage', function (pattern, channel, message) {
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
server.listen(3000);
这是在created()中成功获取请求的回调:
let socket = io('http://localhost:3000');
socket.on("connect",function(){
console.log("socket connected");
});
socket.on("disconnect",function(){
console.log("socket disconnected");
});
// For each channel...
for (let channel of _this.channels) {
// ... listen for new events/messages
socket.on(channel.name+':App\\Events\\MessageSent', data => {
console.log("asd");
console.log(data);
if (_this.activeChannel == channel.id) {
_this.messages.push(data.data);
}
});
}
该位在控制器中
$message = Message::forceCreate([
'channel_id' => $channel,
'author_username' => $request['data']['username'],
'message' => $request['data']['message'],
]);
$chan = Channel::find($channel);
event(new MessageSent($chan->name, $message));
return $message;