Cordova chrome.socket API. Any example?

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

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

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

function doConnect() {< br /> 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 hostname to a different IP or change the port. But sometimes I see the result = -1000, which means failure. Or sometimes I don’t see the result alert. I also have problems using chrome.socket.read.

Please criticize my code. Thank you very much!

The best API example for this plugin may be at https://github.com/GoogleChrome/ chrome-app-samples/years are published on GitHub. They are used for chrome.socket API, which is implemented by plugins. In particular, you can check TCP Server, Web Server and Websocket Server.

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

These examples all show server sockets – but I don’t have a good one Examples to show you the client.

As far as your code is concerned, there are two things that can immediately stand out:

First, you are trying to connect to TCP port 0, which is almost It must be incorrect. To connect to an HTTP service, you should omit “http://” from the address-just use the server name and use 80 as the port number.

Secondly, it looks like you Chrome.socket.connect is called immediately after chrome.socket.create, and chrome.socket.read is called immediately after chrome.socket.connect, instead of performing these operations in the callback. This may mean that the connect can be issued before the creation is complete Call, or you can issue a read call before completing the binding call. I will start with nested 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(theSock etId, 1000, function(readInfo) {
alert(readInfo.resultCode);
});
}
});
});

However, the next thing to prevent it from working is that the code does not actually send any requests to the server. chrome.socket is a low-level interface, so if you want to communicate via HTTP, your application must talk about HTTP.

A very simple sample code will communicate with the web server and return the first 1000 bytes of its homepage, as shown below:

// 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 bufView[i] = string.charCodeAt(i);
}
return buffer;< br />}

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 = createIn fo.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
});
});
}
});
});

Me I am trying to use the org.chromium.socket plugin. But I cannot find many examples. This 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) {< br /> alert(readInfo.resultCode);
});
}

I try to change the hostname to a different IP or change the port. But sometimes I see the result = -1000 means failure. Or sometimes I don’t see the result alert. I also have problems using chrome.socket.read.

Please criticize my code. Thank you very much!

The best API sample of this plugin may be published on GitHub at https://github.com/GoogleChrome/chrome-app-samples/ They are used for chrome.socket API, which is implemented by plugins. In particular, you can check TCP Server, Web Server and Websocket Server.

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

These examples all show server sockets – but I don’t have a good example to show you the client.

As far as your code is concerned, there are two things that can immediately stand out:

First, you are trying to connect to TCP port 0, which is almost certainly incorrect. To connect to an HTTP service, You should omit “http://” from the address-just use the server name, and use 80 as the port number.

Secondly, it looks like you call chrome.socket immediately after chrome.socket.create .connect, call chrome.socket.read immediately after chrome.socket.connect, instead of performing these operations in the callback. This may mean that the connect call can be issued before the creation is complete, or the read can be issued before the binding call is completed Call. I will start with nested 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(re adInfo.resultCode);
});
}
});
});

But the next thing to prevent it from working is the actual code The above does not send any requests to the server. chrome.socket is a low-level interface, so if you want to communicate via HTTP, your application must talk about HTTP.

A very simple sample code will be used with The web server communicates and returns the first 1000 bytes of its home page, as shown below:

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

Leave a Comment

Your email address will not be published.