Cordova chrome.socket API Any example?

I am trying to use the org.chromium.socket plugin. But I can not find many examples. Here is my code:

var connButton = document.getElementById("connButton");

connButton.addEventListener("click", doConnect, false);

function doConnect() {
    var theSocketId = 0;
    chrome.socket.create("tcp", null, function(createInfo) {
        alert(createInfo.socketId);
        theSocketId = createInfo.socketId;
     });
     chrome.socket.connect(theSocketId, "http://www.yahoo.com", 80, function(result) {
        alert(result);
     });
chrome.socket.read(theSocketId, 1000, function(readInfo) {
      alert(readInfo.resultCode);
  });
  }

I am trying to change the host name to a different IP address or change the port. But someday I see the result = -1000, which indicates a failure. Or someday I do not see a warning about the results. I also have a problem using chrome.socket.read.

Please criticize my code. Thank you very much!

+4
source share
1 answer

API , , GitHub https://github.com/GoogleChrome/chrome-app-samples/. API chrome.socket, . , TCP Server, - -.

-, ; GitHub

, - , .

, :

TCP- 0, . HTTP, "http://" - 80 .

-, , chrome.socket.connect chrome.socket.create chrome.socket.read chrome.socket.connect, , . , , , . , :

chrome.socket.create("tcp", null, function(createInfo) {
    alert(createInfo.socketId);
    theSocketId = createInfo.socketId;
    chrome.socket.connect(theSocketId, "www.yahoo.com", 80, function(result) {
        alert(result);
        if (result === 0) {
            chrome.socket.read(theSocketId, 1000, function(readInfo) {
                alert(readInfo.resultCode);
            });
        }
    });
});

, , , , - . chrome.socket - , , HTTP, HTTP.

, - , :

// Utility functions to convert between array buffers and strings

function stringToArrayBuffer(string) {
    var buffer = new ArrayBuffer(string.length);
    var bufView = new Uint8Array(buffer);
    for (var i=0; i < string.length; i++) {
        bufView[i] = string.charCodeAt(i);
    }
    return buffer;
}

function arrayBufferToString(buffer) {
    return String.fromCharCode.apply(null, new Uint8Array(buffer));
}

// Set the hostname; we'll need it for the HTTP request as well
var hostname = "www.yahoo.com";

chrome.socket.create("tcp", function(createInfo) {
    var socketId = createInfo.socketId;
    chrome.socket.connect(socketId, hostname, 80, function(result) {
        if (result === 0) {
            var requestString = "GET / HTTP/1.1\r\nHost: "+hostname+"\r\nConnection: close\r\n\r\n";
            var requestBuffer = stringToArrayBuffer(requestString);
            chrome.socket.write(socketId, requestBuffer, function(writeInfo) {
                chrome.socket.read(socketId, 1000, function(readInfo) {
                    var htmlString = arrayBufferToString(readInfo.data);
                    // do something with htmlString here
                });
            });
        }
    });
});
+9

Source: https://habr.com/ru/post/1532754/


All Articles