Android WebSocket Development

Source: Internet
Author: User

One, add the WebSocket dependency package in the module Build.gradle

// WebSocket Dependency Package implementation ' com.neovisionaries:nv-websocket-client:2.2 '

Two, connect WebSocket

  Try{WebSocket ws=Newwebsocketfactory (). Createsocket (URL, connect_timeout)//WS address, and set time-out . Setframequeuesize (F Rame_queue_size)//Set frame queue maximum value to 5. setmissingcloseframeallowed (false)//setting does not allow the server to close the connection but does not send the close frame. AddListener (Wslistener =NewWslistener ())//Add callback Listener. connectasynchronously ();//Asynchronous Connections        } Catch(IOException e) {e.printstacktrace (); }

Third, add internal classes, implement callback monitoring

/*** Inherit the default listener null implementation Websocketadapter, rewrite the method we need * Ontextmessage receive text message * onconnected Connection Successful * Onconnecterror connection lost Fail * ondisconnected connection off*/    classWslistenerextendsWebsocketadapter {@Override Public voidOntextmessage (WebSocket WebSocket, String text)throwsException {Super. Ontextmessage (websocket, text);            Logger.info (text); String[] Msgs= Text.split ("\\|"); if(Msgs.length >= 2) {notificationshow (msgs[0], Msgs[1]); Sendreceivemessagebroadcast (msgs[0], Msgs[1]); }} @Override Public voidOnconnected (WebSocket WebSocket, map<string, list<string>>headers)throwsException {Super. onconnected (WebSocket, headers); Logger.info ("Connection succeeded"); } @Override Public voidOnconnecterror (WebSocket WebSocket, websocketexception exception)throwsException {Super. Onconnecterror (WebSocket, exception); Logger.warning ("Connection error:" +exception.getmessage ()); } @Override Public voidOndisconnected (WebSocket WebSocket, Websocketframe servercloseframe, Websocketframe clientcloseframe,Booleanclosedbyserver)throwsException {Super. ondisconnected (WebSocket, Servercloseframe, Clientcloseframe, closedbyserver); Logger.warning ("Disconnected"); }    }

Android WebSocket Development

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.