Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova chrome.socket API. Any example?

I am trying to use org.chromium.socket plugin. But I cannot 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 try to change the host name to different IP or change the port. But sometime I see result = -1000, which indicates failure. Or sometime I cannot see result alert. I also have problem using chrome.socket.read.

Please critique my code. Thank you very much!

like image 951
Cheung Brian Avatar asked Jan 10 '23 23:01

Cheung Brian


1 Answers

The best API examples for this plugin are probably those published on GitHub at https://github.com/GoogleChrome/chrome-app-samples/. They're for the chrome.socket API, which is what the plugin implements. In particular, you can check out the TCP Server, Web Server and Websocket Server.

I also wrote a simple web server using the plugin as a demo back in September; you can find it on GitHub here

These examples all show server sockets, though -- I don't have a good example to show you of the client side.

As far as your code goes, two things stand out immediately:

First, you are attempting to connect to TCP port 0, which is almost certainly incorrect. TO connect to an HTTP service, you should leave out the "http://" from the address -- just use the server name, and use 80 as the port number.

Second, it looks like you are calling chrome.socket.connect immediately after chrome.socket.create, and chrome.socket.read immediately after chrome.socket.connect, rather than doing these in callbacks. This might mean that the connect call could be issued before the create is finished, or the read call could be issued before the bind call is completed. I would start by nesting the calls, like this:

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);
            });
        }
    });
});

However, the next thing that's going to stop this from working is that the code doesn't actually send any request to the server. chrome.socket is a low-level interface, and so if you want to communicate over HTTP, your app is going to have to talk HTTP.

A very simple bit of example code that will talk to a web server, and return the first thousand bytes of its home page, looks like this:

// 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
                });
            });
        }
    });
});
like image 81
Ian Clelland Avatar answered Jan 22 '23 04:01

Ian Clelland