2012-09-13 163 views
58

我有一個打開一個套接字並在其上偵聽的vb.net應用程序。使用javascript從瀏覽器連接到TCP套接字

我需要通過這個插座進行通信使用的瀏覽器中運行的JavaScript該應用程序。那就是我需要在這個套接字上發送一些數據,以便在這個套接字上監聽的應用程序可以接收這些數據,使用一些遠程調用做一些事情並獲取更多數據,並將其放回到我的javascript需要的套接字上閱讀並在瀏覽器中打印。

我用盡,socket.io,websockify但都沒有被證明是有用的。

因此,這個問題,我試圖甚至可能嗎?有沒有一種方法可以讓瀏覽器中運行的JavaScript連接到TCP套接字併發送一些數據,並在套接字上偵聽更多的數據響應並將其打印到瀏覽器。

如果這是可能能有人指出我在正確的方向,以這將幫助我樹立的目標。

+1

不,你是有限的WebSockets的 – Bergi

+1

@Bergi - HTTP是通過TCP協議,那麼,爲什麼可以在HTTP連接製成,但不是TCP? –

+0

@ kilaka:因爲在瀏覽器環境中可用的(標準)API [僅限於這些](http://stackoverflow.com/q/10902256/1048572)。 – Bergi

回答

19

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

概述附加:

更新:從W3C草案2016年1月:

這將是通過導航界面可能如下圖所示:

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); 
    ); 
+22

爲了詳細闡述這個答案:服務器還必須使用WebSocket服務器進行偵聽。 WebSockets無法直接連接到原始TCP套接字。 websockify是一個充當WebSocket-to-TCP代理的工具:它位於您的服務器上並偵聽WebSocket連接,然後將WebSocket通信轉發到指定的TCP套接字並從指定的TCP套接字轉發。 – apsillers

+24

這樣做**不回答問題** - websocket是一個基於TCP的協議(例如HTTP)而不是直接的TCP通信。 –

+0

但如何將消息放入瀏覽器窗口? :( – shzyincu

0

你真正需要的解決方案是網絡套接字。然而,鉻項目開發了一些新技術,直接TCP連接TCP chromium

31

至於你的問題,目前你將不得不依賴於XHR或websockets。

目前還沒有流行的瀏覽器已經實現了JavaScript的任何這種原始套接字的API,可以讓你創建和訪問原始套接字,但對原始套接字的API在JavaScript執行情況的草案正在路。看看這些鏈接:
http://www.w3.org/TR/raw-sockets/
https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket

現在,Chrome擁有原始的TCP,並在其「實驗性」的API UDP套接字支持。這些功能僅適用於擴展,雖然有記錄,但暫時隱藏。話雖如此,一些開發人員已經在使用它創建有趣的項目,例如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!");   
    }); 
}); 
+0

什麼瀏覽器支持原始套接字? –

+0

w3原始套接字impl:https://github.com/whiteout-io/tcp-socket – Rondo

+1

與Websockets和Rawsockets相比,任何性能都有所提高嗎? –

3

請參閱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> 
0

ws2s項目旨在把插座到瀏覽器端JS這是轉變的WebSocket到插座的WebSocket服務器

ws2s原理圖

enter image description here

代碼示例:

var socket = new WS2S("wss://feling.io/ws2s-server/").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) 
}