javascript - How to set a timeout for Firefox OS TCP Sockets -
i working on firefox-os app tries connect list of ip's in sequence through tcp socket api.
however, close socket if doesn't connect within few seconds, , if connection inactive more few seconds.
example:
var socket = navigator.moztcpsocket.open(ip, port); //would set timeout connection here socket.onopen = function(event){ var servicerequest = new object(); servicerequest.type = "myservice"; var sendstr = json.stringify(servicerequest); sendstr+="\n"; sendstr = sendstr.tostring('utf-8'); socket.send(sendstr); //and timeout receiving data here socket.ondata = function(event){ //etc } }
there no way specify timeout far know. if want specify timeout, should use usual javascript settimeout
, store id.
use onopen
event on tcpsocket object cancel timeout. if timeout triggered. can call close
method on socket.
var socket = navigator.moztcpsocket.open(ip, port); var timeout = settimeout(function () { socket.close() }, timeoutduration) //would set timeout connection here socket.onopen = function(event){ // prevent timingout if open cleartimeout(timeout) var servicerequest = new object(); servicerequest.type = "myservice"; var sendstr = json.stringify(servicerequest); sendstr+="\n"; sendstr = sendstr.tostring('utf-8'); socket.send(sendstr); //and timeout receiving data here socket.ondata = function(event){ //etc } }
Comments
Post a Comment