首页 文章

使用javascript从浏览器连接到TCP Socket

提问于
浏览
75

我有一个vb.net应用程序,它打开一个套接字并监听它 .

我需要使用在浏览器上运行的javascript通过此套接字与该应用程序进行通信 . 那就是我需要在这个套接字上发送一些数据,以便正在监听这个套接字的应用程序可以获取该数据,使用一些远程调用做一些事情并获取更多数据并将其放回我的javascript需要的套接字上在浏览器中阅读并打印 .

我试过,socket.io,websockify,但没有一个被证明是有用的 .

因此,问题是,我正在尝试甚至可能吗?有没有办法让浏览器中运行的javascript可以连接到tcp套接字并发送一些数据并在其上监听,以便在套接字上获得更多数据响应并将其打印到浏览器 .

如果可能的话,有人可以指出正确的方向,这将有助于我确定目标 .

5 回答

  • 0

    你可以使用 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/

    您的服务器也必须使用诸如pywebsocket之类的WebSocket服务器进行侦听,或者您也可以按照Mozilla所述编写自己的服务器 .

    Additional:

    更新:从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);
        );
    
  • 23

    至于你的问题,目前你将不得不依赖于XHR或websockets .

    目前还没有流行的浏览器为javascript实现任何这样的原始套接字api,可以让你创建和访问原始套接字,但是在JavaScript中实现原始套接字api的草案正在进行中 . 看看这些链接:
    http://www.w3.org/TR/raw-sockets/
    https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket

    Chrome现在支持其“实验”API中的原始TCP和UDP套接字 . 这些功能 are only available for extensions 虽然有记录,但暂时隐藏起来 . 话虽如此,一些开发人员已经在使用它创建有趣的项目,例如this IRC client .

    要访问此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!");         
        });
    });
    
  • 40

    您真正需要的解决方案是Web套接字 . 但是,chrome项目开发了一些直接TCP连接的新技术TCP chromium

  • 5

    jsocket . 我自己没用过 . 自上次更新至今已超过3年(截至2014年6月26日) .

    *使用闪光:(

    来自documentation

    <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>
    
  • 3

    ws2s项目旨在为浏览器端js带来套接字 . 它是一个websocket服务器,它将websocket转换为socket .

    ws2s原理图

    enter image description here

    代码示例:

    var socket = new WS2S("wss://ws2s.feling.io/").newSocket()
    
    socket.onReady = () => {
      socket.connect("feling.io", 80)
      socket.send("GET / HTTP/1.1\r\nHost: feling.io\r\nConnection: close\r\n\r\n")
    }
    
    socket.onRecv = (data) => {
      console.log('onRecv', data)
    }
    

相关问题