Javascript 到远程服务器的TCP连接

Javascript 到远程服务器的TCP连接,javascript,tcp,port,Javascript,Tcp,Port,我对TCP和服务器连接相关的问题非常陌生。我的任务是实现一个功能,检查连接到服务器的用户端口是否打开或关闭,以确定何时在前端向他们显示消息,通知他们端口的状态 经过这么多的研究,我仍然不知道如何建立这种连接,该功能需要在前端运行,这使得连接更加困难。我曾尝试在后端实现相同的功能,我能够用它实现我想要的。但是由于这个包依赖于NodeJs的net包,所以我不能在客户端使用它 我用WebSocket甚至图像实现了一些自定义函数,但是浏览器总是给我一个net::ERR\u EMPTY\u响应错误,而不是

我对TCP和服务器连接相关的问题非常陌生。我的任务是实现一个功能,检查连接到服务器的用户端口是否打开或关闭,以确定何时在前端向他们显示消息,通知他们端口的状态

经过这么多的研究,我仍然不知道如何建立这种连接,该功能需要在前端运行,这使得连接更加困难。我曾尝试在后端实现相同的功能,我能够用它实现我想要的。但是由于这个包依赖于NodeJs的
net
包,所以我不能在客户端使用它

我用WebSocket甚至图像实现了一些自定义函数,但是浏览器总是给我一个
net::ERR\u EMPTY\u响应
错误,而不是获取连接状态

下面是我到目前为止写的一些代码,试图找出问题所在。 如果能给我指出正确的方向,我将不胜感激。多谢各位

export const checkSocketIoConnect = (url, timeout) => {
    return new Promise(function(resolve, reject) {
        let errAlready = false;
        timeout = timeout || 50000;
        let socket = io(url, {
            transport: ['websocket'],
            reconnection: true, timeout: timeout, reconnectionDelay: 1000,
            reconnectionAttempts: 50,
            rejectUnauthorized: false
        });

        // success
        socket.on('connect', function() {
            console.log('connecting succesfulyy');
            clearTimeout(timer);
            resolve();
            socket.close();
        });

        // set our own timeout in case the socket ends some other way than what we are listening for
        let timer = setTimeout(function() {
            console.log('timer');
            timer = null;
            error('local timeout');
        }, timeout);

        // common error handler
        const error = (data) => {
            console.log('errrror');
            if (timer) {
                clearTimeout(timer);
                timer = null;
            }
            if (!errAlready) {
                errAlready = true;
                reject(data);
                socket.disconnect();
            }
        };

        // errors
        socket.on('connect_error', error);
        socket.on('connect_timeout', error);
        socket.on('error', error);
        socket.on('disconnect', error);

    });
};

利用我找到的这个软件包,我能够解决这个问题。该包使用DOM中的图像。这是给那些希望以后解决这个问题的人的

我利用我找到的这个软件包解决了这个问题。该包使用DOM中的图像。这是给那些希望以后解决这个问题的人的

export const newPortChecker = (url) => {
    let websocket = new WebSocket(url);
    // websocket.send('pinging server');
    websocket.onopen = function(event) {
        setInterval(() => {
            websocket.send('ping');
        }, 30000);
        console.log(event);
    };

    websocket.onmessage = function(event) {
        console.log(event.data);
    };

    websocket.onerror = function(event) {
        console.log(event);
    };

    websocket.onclose = function(event) {
        console.log(event);
    };
};
export const portChecker = (cb, host, port, timeout) => {
    let timeouts = (timeout == null) ? 100 : timeout;
    let img = new Image();

    img.onerror = function() {
        if (!img) return;
        img = undefined;
        console.log('open');
        cb('open');
    };

    img.onload = img.onerror;
    img.src = 'https://' + host + ':' + port;

    setTimeout(function() {
        if (!img) return;
        img = undefined;
        console.log('closed');
        cb('closed');
    }, timeouts);
};