Android socket connection loses after app being idle for some time -
my application listens socket. application connected socket using following method.
public void connect() { this.connectionstatus = connect_status_connecting; log.v(appconstants.debug_tag, userid + " : connecting server"); if (mthread != null && mthread.isalive()) { return; } mthread = new thread(new runnable() { @override public void run() { try { log.v(appconstants.debug_tag, userid + " : thread action started"); string secret = createsecret(); int port = (muri.getport() != -1) ? muri.getport() : (muri.getscheme().equals("wss") ? 443 : 80); string path = textutils.isempty(muri.getpath()) ? "/" : muri.getpath(); if (!textutils.isempty(muri.getquery())) { path += "?" + muri.getquery(); } string originscheme = muri.getscheme().equals("wss") ? "https" : "http"; uri origin = new uri(originscheme, "//" + muri.gethost(), null); socketfactory factory = muri.getscheme().equals("wss") ? getsslsocketfactory() : socketfactory.getdefault(); msocket = factory.createsocket(muri.gethost(), port); printwriter out = new printwriter(msocket.getoutputstream()); out.print("get " + path + " http/1.1\r\n"); out.print("upgrade: websocket\r\n"); out.print("connection: upgrade\r\n"); out.print("host: " + muri.gethost() + "\r\n"); out.print("origin: " + origin.tostring() + "\r\n"); out.print("sec-websocket-key: " + secret + "\r\n"); out.print("sec-websocket-version: 13\r\n"); if (mextraheaders != null) { (namevaluepair pair : mextraheaders) { out.print(string.format("%s: %s\r\n", pair.getname(), pair.getvalue())); } } out.print("\r\n"); out.flush(); hybiparser.happydatainputstream stream = new hybiparser.happydatainputstream(msocket.getinputstream()); // read http response status line. statusline statusline = parsestatusline(readline(stream)); if (statusline == null) { log.v(appconstants.debug_tag, "received no reply server."); throw new httpexception("received no reply server."); } else if (statusline.getstatuscode() != httpstatus.sc_switching_protocols) { throw new httpresponseexception(statusline.getstatuscode(), statusline.getreasonphrase()); } // read http response headers. string line; boolean validated = false; while (!textutils.isempty(line = readline(stream))) { header header = parseheader(line); if (header.getname().equals("sec-websocket-accept")) { string expected = createsecretvalidation(secret); string actual = header.getvalue().trim(); if (!expected.equals(actual)) { log.v(appconstants.debug_tag, "bad sec-websocket-accept header value."); throw new httpexception("bad sec-websocket-accept header value."); } validated = true; } } if (!validated) { log.v(appconstants.debug_tag, "no sec-websocket-accept header."); throw new httpexception("no sec-websocket-accept header."); } onconnect(); log.v(appconstants.debug_tag, userid + " : thread should connected now"); // decode websocket frames. mparser.start(stream); } catch (eofexception ex) { log.d(appconstants.debug_tag, "websocket eof!", ex); ondisconnect(0, "eof"); } catch (sslexception ex) { // connection reset peer log.d(appconstants.debug_tag, "websocket ssl error!", ex); ondisconnect(0, "ssl"); } catch (exception ex) { onerror(ex); } } }); log.v(appconstants.debug_tag, userid + " : thread started"); mthread.start(); }
here problem when leave application 15 or more idle, connection automatically closes , app becomes unusable.what reason behind , how resolve this?
probably, server closes idle connection. send ping (or pong) periodically keep connection.
btw, guess code based on open-source websocket library. if so, note not of commercial quality. not perform closing handshake on disconnect()
.
Comments
Post a Comment