In my Chrome App I need somehow Chrome to give the the reference to my object 'identity_service' in this variable when the callback is executed, but I don't know how.
This is my constructor:
function Identity(identity_host,identity_port) {
this.identity_host=identity_host;
this.identity_port=identity_port;
this.socket_fd=0;
chrome.sockets.tcp.create({}, this.socket_create_callback);
}
The function socket_create_callback is defined like this:
Identity.prototype.socket_create_callback=function(info) {
console.log('info='+info);
console.log('constructor name:'+this.constructor.name);
console.log('hostname='+this.identity_host);
this.socket_fd=info.socketId;
}
This is how I create it:
var identity_service=new Identity("localhost",4433);
Without object oriented programming Chrome calls the callback function with the parameter 'info' and it works fine. But when using OOP the 'this' variable doesn't hold the reference to my 'Identity' object otherwise the 'hostname' would not be undefined. This is what I get in the console log:
identity.js:21 info=[object Object]
identity.js:22 constructor name:Object
identity.js:23 hostname=undefined
With correct invocation of the callback the name of the csontructor would be 'Identity'. How can this be fixed?
The Chrome API call for creating a socket is this:
create
chrome.sockets.tcp.create( SocketProperties properties, function callback)
Creates a TCP socket.