Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting event when new data can be read - chrome.socket.read API
    primarykey
    data
    text
    <p>Trying to use TCP sockets using <code>chrome.socket</code> API on Chrome Version 25.0.1364.5 dev.</p> <p>Looking at the <a href="http://developer.chrome.com/apps/socket.html" rel="nofollow">documentation</a> for the <code>chrome.socket.read</code> there doesn't seem to be a way to get a notification when new data is available to be read.</p> <p>There is some <a href="https://github.com/GoogleChrome/chrome-app-samples/blob/master/tcpserver/tcp-server.js" rel="nofollow">sample code</a> for a TCP server which polls the read command every 500ms but I think this would not be efficient / accurate</p> <pre><code>// Start polling for reads. setInterval(this._periodicallyRead.bind(this, socketId), 500); </code></pre> <p>What's more confusing is that in the <a href="http://developer.chrome.com/apps/app_network.html" rel="nofollow">'Network Communications' Documentation</a> under section 'Receiving data', it is stated that a special handler can be passed as an <code>onEvent</code> option in <code>chrome.socket.create</code></p> <blockquote> <p>The parameter is an object with one value 'onEvent' that is a function reference to the method that will be called when data is available on the port.</p> </blockquote> <p>This onEvent parameter would be used like this</p> <pre><code>chrome.socket.create( 'udp', '127.0.0.1', 1337, { onEvent: handleDataEvent }, // &lt;-- call this when new data is available createHandler ) </code></pre> <p>But this appears to apply only for UDP connections as I get the following error when I try to use it</p> <pre><code>Error: Invocation of form socket.create(string, string, integer, object, function) doesn't match definition socket.create(string type, optional object options, function callback) at Object.normalizeArgumentsAndValidate (schemaUtils:119:11) at Object.&lt;anonymous&gt; (schema_generated_bindings:301:32) at chrome-extension://obljaojhdffbpcdfbeoiejegaodfoonp/background.js:11:15 at chrome.Event.dispatchToListener (event_bindings:387:21) at chrome.Event.dispatch_ (event_bindings:373:27) at dispatchArgs (event_bindings:249:22) at Object.app.runtime.onLaunched (app.runtime:116:7) at Object.chromeHidden.Event.dispatchEvent (event_bindings:255:35) </code></pre> <p>So the question is, can something like this be achieved with TCP Connections ? Instead of having to poll the <code>read</code> method every x milliseconds ?</p> <p><strong>Update</strong></p> <p>This is a workaround I am using until better documentation / event support exists.</p> <pre><code>function onReadHandler(readInfo) { // do things with data // .... // re register handler with callback itself chrome.socket.read(socketId,null,onReadHandler); } chrome.socket.read(socketId,null,onReadHandler); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload