To connect to TCP Socket from browser using JavaScript, we call the socket.create
method.
For instance, we write
chrome.experimental.socket.create("tcp", "127.0.0.1", 8080, (socketInfo) => {
chrome.experimental.socket.connect(socketInfo.socketId, (result) => {
chrome.experimental.socket.write(socketInfo.socketId, "Hello, world!");
});
});
to call the socket.create
method to create a socket to 127.0.0.1 port 8080.
We then call socket.connect
method to connect to the socket with the socketInfo
.
And then we call write
to send data to the socket.