请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

failed: WebSocket is closed before the connection is established.

打开控制台看了下如下
报错:
WebSocket connection to ‘ws:/ws/message/lars/’ failed: WebSocket is closed before the connection is established.
图片描述
我查了下Provisional headers are shown好像是说请求没能发出去

js中这么写的

        initwebsocket(){
        const ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
        const ws_path = ws_scheme + "://"+ window.location.host+ '/ws/message/'+`${this.current_talk_obj}/`;
        const ws = new ReconnectingWebSocket(ws_path);

        const that = this;

        // 监听后端发送过来的消息
        ws.onmessage = function (event) {
          const data = JSON.parse(event.data);
          console.log(data);
          console.log(data.sender,that.current_talk_obj);
          if (data.sender === that.current_talk_obj) {  // 发送者为当前选中的用户
            that.Get_conversation();
            console.log('get new info')
            // 将接收到的消息插入到聊天框
            // scrollConversationScreen();  // 滚动条下拉到底
          }
        }

和老师的比对了也没发现什么问题,请老师帮忙看下,谢谢

11175
收起

正在回答

1回答

您好,验证一下你定义的“ws_path”是否有效呢,看后端有写这个websocket的连接吗

1 回复 有任何疑惑可以回复我~
  • 提问者 李嘉图principal #1
    确实如老师所言,window.location.host 获取的是前端地址,我的项目使前后端分离的......,另外,测试的时候要加端口,部署时不加端口也是容易出问题的地方,谢谢老师
    回复 有任何疑惑可以回复我~ 2020-09-08 14:30:46
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信