Android WebSocket package based on Java-websocket

Source: Internet
Author: User

This article draws on 79190643 thanks to the great God for his contribution

Use WebSocket for reference article Third party is:nv-websocket-client:2.2,

and here's what we're using: java-websocket:1.3.0

The core code is as follows:

 Public Abstract classMywebsocketserviceextendsServiceImplementsIwebsocket {Private Static FinalString TAG = Mywebsocketservice.class. Getsimplename (); PrivateServicebinder Binder =NewServicebinder (); Private intConnectstatus = 0; PrivateWebsocketthread Websocketthread; Privatewebsocketclient Client; @Nullable @Override Publicibinder onbind (Intent Intent) {log.i (TAG,"Onbind:"); if(Binder = =NULL) {Binder=NewMywebsocketservice.servicebinder (); }        returnBinder; }     Public classServicebinderextendsBinder { PublicMywebsocketservice GetService () {returnMywebsocketservice. This; }} @Override Public voidonCreate () {Super. OnCreate (); LOG.I (TAG,"OnCreate:"); //ConnectionWebsocketthread =NewMywebsocketservice.websocketthread ();    Websocketthread.start (); }    Private classWebsocketthreadextendsThread {@Override Public voidrun () {log.i (TAG,"Run:");        Setupwebsocket (); }    }    protected voidSetupwebsocket () {log.i (TAG,"Setupwebsocket:"); if(Connectstatus! = 0)return; Connectstatus= 1; Try{Client=NewWebsocketclient (Uri.create (Getconnecturl ()),Newdraft_10 ()) {@Override Public voidOnOpen (Serverhandshake handshakedata) {connectstatus= 2; } @Override Public voidonMessage (String message) {log.i (TAG,"OnMessage:" +message); //Message Distributiondispatchresponse (message); } @Override Public voidOnClose (intCode, String Reason,Booleanremote) {LOG.E (TAG,"OnClose:" + code +reason); Connectstatus= 0; //Eventbus Notice} @Override Public voidOnError (Exception ex) {LOG.E (TAG,"OnError:" +ex.getmessage ()); Connectstatus= 0; //Eventbus Notice                }            };        Client.connect (); }Catch(Exception e) {e.printstacktrace (); }} @Override Public voidOnDestroy () {Super. OnDestroy (); LOG.I (TAG,"OnDestroy:"); } @Override Public voidSendText (String text) {log.i (TAG,"SendText from four Activity:" +text); if(Textutils.isempty (text))return; if(Client! =NULL&& Connectstatus = = 2) {client.send (text); }} @Override Public intGetconnectstatus () {returnConnectstatus; } @Override Public voidReconnect () {log.i (TAG,"Reconnect:"); NewThread (NewRunnable () {@Override Public voidrun () {Try{Thread.Sleep (200); } Catch(interruptedexception e) {e.printstacktrace (); }                if(Websocketthread! =NULL&&websocketthread.isalive ()) {Connectstatus= 0; Websocketthread=NewWebsocketthread ();                Websocketthread.start (); }Else{log.e (TAG,"Run:reconnect Action is Failed" );    }}). Start (); } @Override Public voidStop () {log.i (TAG,"Stop:");        Client.close (); Connectstatus= 0; }    /*** Get server address *@return     */    protected AbstractString Getconnecturl (); /*** Distribute response data*/    protected Abstract voiddispatchresponse (String textresponse);}

Absbasewebsocketservice.java files in the same reference article

Its main functions are:

Several behaviors are implemented during WebSocket connection: Iwebsocket,

Provide a specific service binding,

Open WebSocket connection thread, callback message processing, etc.

Other code does not repeat, please look at the article, then on this at a glance, if you do not understand, you can comment in the comments section below,

Thank you!

Android WebSocket package based on Java-websocket

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.