使用JavaScript从浏览器连接到TCP套接字

我有一个vb.net应用程序打开一个套接字,并在其上侦听。

我需要使用运行在浏览器上的JavaScript通过这个套接字与该应用程序进行通信。 这就是我需要发送一些数据在这个套接字,以便在这个套接字上侦听的应用程序可以采取这些数据,做一些东西使用一些远程调用,并获得更多的数据,并把它放回我的JavaScript需要的套接字阅读并在浏览器中打印。

我试过,socket.io,websockify,但没有certificate是有用的。

因此,这个问题,我正在尝试什么? 有没有一种方式,在浏览器中运行的JavaScript可以连接到一个TCP套接字,并发送一些数据,并监听它在套接字上的更多的数据响应,并将其打印到浏览器。

如果这是可能的,那么有人可以指出我正确的方向,哪个能帮助我确立目标。

至于你的问题,目前你将不得不依靠XHR或websockets这个。

目前没有stream行的浏览器已经实现了任何这样的原始套接字api的JavaScript,使您可以创build和访问原始套接字,但在原始套接字api实施草案的JavaScript是草案。 看看这些链接:
http://www.w3.org/TR/raw-sockets/
https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket

Chrome现在在其“实验”API中支持原始TCP和UDP套接字。 这些function仅适用于扩展 ,虽然有文件logging,但暂时隐藏。 话虽如此,一些开发人员已经在使用它创build有趣的项目,比如这个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!"); }); }); 

您可以使用HTML5 Web Sockets

 var connection = new WebSocket('ws://IPAddress:Port'); connection.onopen = function () { connection.send('Ping'); // Send the message 'Ping' to the server }; 

http://www.html5rocks.com/en/tutorials/websockets/basics/

您的服务器还必须使用WebSocket服务器(如pywebsocket)进行侦听,或者您可以按照Mozilla

额外:

更新:从2016年1月的W3C草案:

这可以通过导航器界面进行,如下所示:

http://raw-sockets.sysapps.org/#interface-tcpsocket

https://www.w3.org/TR/tcp-udp-sockets/

 navigator.tcpPermission.requestPermission({remoteAddress:"127.0.0.1", remotePort:6789}).then( () => { // Permission was granted // Create a new TCP client socket and connect to remote host var mySocket = new TCPSocket("127.0.0.1", 6789); // Send data to server mySocket.writeable.write("Hello World").then( () => { // Data sent sucessfully, wait for response console.log("Data has been sent to server"); mySocket.readable.getReader().read().then( ({ value, done }) => { if (!done) { // Response received, log it: console.log("Data received from server:" + value); } // Close the TCP connection mySocket.close(); } ); }, e => console.error("Sending error: ", e); ); 

看到jsocket 。 没有使用它自己。 自上次更新(截至2014年6月26日)已超过3年。

*使用闪光灯:(

从文档 :

 <script type='text/javascript'> // Host we are connecting to var host = 'localhost'; // Port we are connecting on var port = 3000; var socket = new jSocket(); // When the socket is added the to document socket.onReady = function(){ socket.connect(host, port); } // Connection attempt finished socket.onConnect = function(success, msg){ if(success){ // Send something to the socket socket.write('Hello world'); }else{ alert('Connection to the server could not be estabilished: ' + msg); } } socket.onData = function(data){ alert('Received from socket: '+data); } // Setup our socket in the div with the id="socket" socket.setup('mySocket'); </script> 

您真正需要的解决scheme是Web套接字。 不过,铬项目已经开发了一些新的技术,即直接TCP连接TCP铬