你可以使用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所述编写自己的服务器

额外:

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

);

更多推荐