これは、以下に示すように、ナビゲーター インターフェイスを介して可能になります。
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)
);
}
);
詳細については、w3.org tcp-udp-sockets のドキュメントで概説されています。
http://raw-sockets.sysapps.org/#interface-tcpsocket
https://www.w3.org/TR/tcp-udp-sockets/
もう 1 つの方法は、Chrome ソケットを使用することです。
接続の作成
chrome.sockets.tcp.create({}, function(createInfo) {
chrome.sockets.tcp.connect(createInfo.socketId,
IP, PORT, onConnectedCallback);
});
データの送信
chrome.sockets.tcp.send(socketId, arrayBuffer, onSentCallback);
データ受信中
chrome.sockets.tcp.onReceive.addListener(function(info) {
if (info.socketId != socketId)
return;
// info.data is an arrayBuffer.
});
使用を試みることもできますHTML5 Web Sockets
(これは直接の TCP 通信ではありませんが):
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で概説されているように独自に作成することもできます。