我不使用socket.io,但它给了我这样的错误
app.js:18695 未捕获类型错误:Cannot read property 'socketId' of undefined
详细写了,我更新了,之前遇到过SSL问题,所以无法以https方式连接,处理的时候就出现了这个问题。
我这个使用 laravel-websockets、pusher、laravel echo
聊天刀片.php
var globalChannelId = 0;
var user_id = document.getElementById('user_id').value;
function channel(elem) {
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: '{{ route('channelControl') }}',
cache: false,
dataType: "json",
data: {user_id: user_id, receiver_id: elem.id},
method: 'POST',
success: function (data) {
if (data.status === 200) {
globalChannelId = data.data;
document.getElementById('channel_token').value = data.data;
document.getElementById('receiver_id').value = data.receiver_id;
var message = data.message;
console.log("message")
console.log(message)
var parentDiv = document.getElementById('parentDiv');
parentDiv.innerHTML = "";
var i;
for (i = 0; i < message.length; i++) {
var div = document.createElement('div');
div.classList = "d-flex";
var h6 = document.createElement('h6');
h6.innerHTML = message[i]["user"]["name"];
var span = document.createElement('span');
span.innerHTML = message[i]["message"];
div.appendChild(h6);
div.appendChild(span);
parentDiv.appendChild(div);
}
dinle()
scroll()
} else {
}
}
});
}
var dongu;
function dinle() {
Echo.disconnect();
Echo.connect();
// Echo.leave('privatechat.5D707F5F-CAAF-A775-1FE5-E1EA94208A22');
console.log(globalChannelId);
Echo.join('privatechat.' + globalChannelId)
.here((user) => {
var activeUser = document.getElementById('activeUser');
activeUser.innerHTML = "";
console.log(user);
console.log("here");
for(var i=0 ; i<user.length ; i++){
var h6 = document.createElement('h6');
h6.innerHTML = user[i].name;
h6.id=user[i].id;
activeUser.appendChild(h6);
}
})
.joining((user) => {
console.log('joining')
var activeUser = document.getElementById('activeUser');
var h6 = document.createElement('h6');
h6.innerHTML = user.name;
h6.id=user.id;
activeUser.appendChild(h6);
})
.leaving((user) => {
console.log('Leaving');
document.getElementById(user.id).innerHTML = "";
console.log(user);
})
.listen('PrivateChannelSend', (e) => {
console.log(e.message.message)
console.log(e)
var parentDiv = document.getElementById('parentDiv');
var div = document.createElement('div');
div.classList = "d-flex";
var h6 = document.createElement('h6');
h6.innerHTML = e.message["user"]["name"];
var span = document.createElement('span');
span.innerHTML = e.message["message"];
div.appendChild(h6);
div.appendChild(span);
parentDiv.appendChild(div);
scroll()
})
.listenForWhisper('typing',response => {
console.log('typing');
document.getElementById('yaziyor').style.display = "block";
console.log(response);
if(response){
clearTimeout(dongu);
}
dongu = setTimeout(function(){
document.getElementById('yaziyor').style.display = "none";
}, 3000);
});
}
广播.php
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'useTLS' => true,
'encrypted' => true,
'scheme' => 'https',
'debug' => true,
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
]
],
],
websockets.php
'ssl' => [
'apps' => [
[
'id' => env('PUSHER_APP_ID'),
'name' => env('APP_NAME'),
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'enable_client_messages' => true,
'enable_statistics' => true,
'encrypted' => true,
],
],
'local_cert' => '/usr/local/psa/var/modules/letsencrypt/etc/live/sub.xsite.com.tr/fullchain.pem',
'local_pk' => '/usr/local/psa/var/modules/letsencrypt/etc/live/sub.xsite.com.tr/privkey.pem',
'passphrase' => null,
'verify_peer' => false,
],