JavaScript 监听端口

编程入门 行业动态 更新时间:2024-10-22 23:39:47
本文介绍了JavaScript 监听端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用 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/apps/sockets_tcp

mozilla 也有一些有限的支持:

There is also some limited support by mozilla:

https://developer.mozilla/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/novnc/websockify

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

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

更多推荐

[db:关键词]

本文发布于:2023-04-26 05:11:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1129002.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:端口   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!