无法构造“ WebSocket”:URL“ ws / ws”无效

时间:2019-07-01 21:19:13

标签: node.js websocket

我在浏览器中使用localhost:3000和端口5000,无法弄清楚为什么我得到此WebSocket URL'ws / ws'无效? 在Sources中,它似乎指向socket = new WebSocket(地址),但我不确定这是否也是指向init(data)的问题!

'use strict';

const protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://';
let address = protocol + window.location.host + window.location.pathname + '/ws';
let socket;
let isActive = false;

function init(data) {
    if (!data.proxySetup) {
        //Correction
        if (data.liveServerUrl.indexOf('http') !== 0)
            data.liveServerUrl = 'http' + data.liveServerUrl;
        if (data.actualUrl.indexOf('http') !== 0)
            data.actualUrl = 'http' + data.actualUrl;
        if (!data.actualUrl.endsWith('/'))
            data.actualUrl = data.actualUrl + '/';

        address = data.liveServerUrl.replace('http', 'ws') + '/ws';
    }
    socket = new WebSocket(address);
    socket.onmessage = (msg) => {
        reloadWindow(msg, data)
    };
}
  chrome.runtime.sendMessage({
    req: 'get-live-server-config'
}, (data) => {
    isActive = data.isEnable;
    if (isActive && !socket) {
        init(data);
    }

0 个答案:

没有答案