本文介绍了使用javascript从浏览器连接到TCP套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个打开套接字并侦听的vb.net应用程序。 我需要使用浏览器上运行的javascript通过此套接字与该应用程序进行通信。那就是我需要在这个套接字上发送一些数据,以便在这个套接字上监听的应用程序可以接收这些数据,使用一些远程调用做一些事情并获取更多数据,并将其放回到我的javascript需要的套接字上阅读并在浏览器中打印。 我试过,socket.io,websockify,但没有任何证明是有用的。 因此,这个问题,我正在尝试什么?有没有一种方法可以让运行在浏览器中的javascript连接到一个tcp套接字并发送一些数据,并在套接字上侦听更多的数据响应并将其打印到浏览器中。 如果可能的话,可以让我指出正确的方向,以帮助我确定目标。 至于你的问题,目前你将不得不依赖于XHR或websockets。 目前没有流行的浏览器已经实现了任何这样的原始套接字api的JavaScript您可以创建并访问原始套接字,但是在JavaScript中实现原始套接字API的草案已在制定中。看看这些链接: http:/ /www.w3.org/TR/raw-sockets/ https://developer.mozilla.org/zh-CN/docs/Web/API/TCPSocket 仅适用于扩展程序,尽管已记录,但暂时隐藏。话虽如此,一些开发人员已经在使用它创建有趣的项目,例如这个IRC客户端。 要访问此API,您需要在扩展程序的清单中启用实验标记。使用套接字非常简单,例如: chrome.experimental.socket.create('tcp','127.0.0.1' ,8080,function(socketInfo){ chrome.experimental.socket.connect(socketInfo.socketId,function(result){ chrome.experimental.socket.write(socketInfo.socketId,Hello,world! ); }); }); I have a vb.net application that opens a socket and listens on it.I need to communicate via this socket to that application using a javascript running on a browser. That is i need to send some data on this socket so that the app which is listening on this socket can take that data, do some stuff using some remote calls and get some more data and put it back on the socket that my javascript needs to read and print it in the browser.Ive tried, socket.io, websockify but none have proved to be useful.Hence the question, is what i am trying even possible? Is there a way that a javascript running in a browser can connect to a tcp socket and send some data and listen on it for some more data response on the socket and print it to the browser.If this is possible can some one point me in the right direction as to which would help me establish the goal. 解决方案 As for your problem, currently you will have to depend on XHR or websockets for this.Currently no popular browser has implemented any such raw sockets api for javascript that lets you create and access raw sockets, but a draft for the implementation of raw sockets api in JavaScript is under-way. Have a look at these links:http://www.w3.org/TR/raw-sockets/https://developer.mozilla.org/en-US/docs/Web/API/TCPSocketChrome now has support for raw TCP and UDP sockets in its ‘experimental’ APIs. These features are only available for extensions and, although documented, are hidden for the moment. Having said that, some developers are already creating interesting projects using it, such as this IRC client.To access this API, you’ll need to enable the experimental flag in your extension’s manifest. Using sockets is pretty straightforward, for example:chrome.experimental.socket.create('tcp', '127.0.0.1', 8080, function(socketInfo) { chrome.experimental.socket.connect(socketInfo.socketId, function (result) { chrome.experimental.socket.write(socketInfo.socketId, "Hello, world!"); });}); 这篇关于使用javascript从浏览器连接到TCP套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-28 02:58