JavaScript 监听端口 [英] JavaScript listen on a port

查看:46
本文介绍了JavaScript 监听端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 dump1090 来监听和解码 ADS-B 信号.它通过端口 8080 将数据输出到 Web 前端,一切正常.该代码还通过端口 30003 和 30002 生成输出,这些端口是原始消息.我正在尝试侦听"这些消息,但是出现套接字错误.我在js中使用的代码是:

I'm using dump1090 to listen to and decode ADS-B signals. It outputs the data over port 8080 to a web front end and all is well. The code also produces output over ports 30003 and 30002 which are the raw messages. I'm trying to 'listen' for these messages however I'm getting a socket error. The code I'm using in js is:

function dumpSocket() {
    console.log("attempting to dump socket")
    let ws = new WebSocket ("ws://192.168.0.29:30003")
    ws.onmessage = function (data) {
        console.log(data)
    }
    window.onbeforeunload = function(event) {
        socket.close();
    }
}

我得到的错误是:

VM3157:164 WebSocket 连接到ws://192.168.0.29:30003/"失败:WebSocket 握手时出错:net::ERR_INVALID_HTTP_RESPONSE

VM3157:164 WebSocket connection to 'ws://192.168.0.29:30003/' failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE

但是,如果我对 IP 和端口进行 netcat,我收到的消息没问题.我哪里出错了?

If I netcat to the IP and Port however, I get the messages no problem. Where am I going wrong?

推荐答案

很遗憾,你不能使用 websockets 来监听 raw tcp 或 udp socket,在服务器端你也需要创建一个 websocket 接口.

Unfortunately, you can't use websockets to listen to raw tcp or udp socket, on the server side you need to create a websocket interface as well.

>

但是在 Chrome 扩展程序中,您可以使用 chrome.sockets.tcp 参见:

https://developer.chrome.com/apps/sockets_tcp

mozilla 也有一些有限的支持:

There is also some limited support by mozilla:

https://developer.mozilla.org/en-US/docs/Archive/B2G_OS/API/TPC_Socket_API

另一种解决方案是创建一个 nodeJS 服务器,它将侦听原始 tcp 套接字并将其转换为 websocket.

Another solution is creating a nodeJS server, which will be listening on the raw tcp socket and converting it to a websocket.

你也可以试试 websockify:https://github.com/novnc/websockify

you can also try websockify: https://github.com/novnc/websockify

这篇关于JavaScript 监听端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆