首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:在建立连接之前WebSocket已关闭

错误:在建立连接之前WebSocket已关闭
EN

Stack Overflow用户
提问于 2021-04-11 16:09:07
回答 1查看 524关注 0票数 0

我有一个不和谐的音乐机器人,它已经运行了将近一年。从昨天开始,我收到一个问题,上面写着:

代码语言:javascript
运行
复制
Unable to play a music: 
Error: WebSocket was closed before the connection was established

下面是我的代码:

代码语言:javascript
运行
复制
    try {
      var connection = await voiceChannel.join();
      if(guild.me.hasPermission("DEAFEN_MEMBERS")) guild.me.voice.setDeaf(true);
      queueContruct.connection = connection;
      play(message.guild, queueContruct.songs[0], message);
    } catch (err) {
      queue.delete(message.guild.id);
      errorMessage(message, "1058");
      console.error("Unable to play a music: ");
      return console.error(err);
    }

播放功能:

代码语言:javascript
运行
复制
async function play(guild, song, message) {
  const serverQueue = queue.get(guild.id);

  if (!song) {
    if(serverQueue.voiceChannel){
      serverQueue.voiceChannel.leave();
    }
    else {
      guild.voice.channel.leave();
    }
    queue.delete(guild.id);
    return;
  }

  try {
    let player = await ytdl(song.url, {filter: 'audioonly', quality: 'highestaudio', highWaterMark: 1 << 25, opusEncoded: true, encoderArgs: ['-af', 'bass=g=0']});
    const dispatcher = serverQueue.connection.play(player, { type: 'opus', highWaterMark: 1, bitrate: 'auto', fec: true, volume: false })
      .on("finish", () => {
        serverQueue.songs.shift();
        play(guild, serverQueue.songs[0], message);
      })
      .on("error", error => {
        console.error(error);
      });

      dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
    } catch(err){
      errorMessage(message, "1060");
    }
}

我不知道问题出在哪里。有什么想法吗?这个错误到底是什么意思呢?

EN

回答 1

Stack Overflow用户

发布于 2021-04-11 16:18:25

在未建立连接的情况下调用websocket上的close()时就会发生这种情况。您在问题中包含的代码不包含close();请检查close(),并确保在未建立连接时不调用它。

discord.js使用ws,这是抛出错误的地方:

https://github.com/websockets/ws/blob/a2c0d447af711ca245cb534159fa7c4d9ae67e64/lib/websocket.js#L222

因此,如果websocket处于连接状态,并且您调用close(),则会抛出错误。

参考资料:https://stackoverflow.com/a/12503628/9483495

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67042749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档