WebSocket synchronous return with Countdownlatch implementation

Source: Internet
Author: User

Jar Package used: Https://github.com/TooTallNate/Java-WebSocket

WebSocket Reference website

Jar Package originally wanted to upload, but in Csdn can't find the place to upload

Main class
Import Java.net.uri;import Java.util.concurrent.countdownlatch;import Org.java_ Websocket.handshake.serverhandshake;import Com.zmy.util.comn;public class Websockstu {public static void main (String[ ] args) {t ();} public static string T () {string uri = "wss://api.radarlab.org:443"; uri= "ws://api.radarlab.org:5006"; Websocketclientext cc=null;try{cc = new Websocketclientext (new Uri (URI)) {@Overridepublic void OnMessage (String message ) {setmsg (message); comn.pl ("Got:" + message + "\ n"); getcdl (). Countdown (); @Overridepublic void OnOpen (Serverhandshake handshake) {getcdlconn (). Countdown (); comn.pl ("You is connected to Chatserver:" + geturi () + "\ n");} @Overridepublic void OnClose (int code, String reason, Boolean remote) {comn.pl ("You had been disconnected from:" + ge TURI () + "; Code: "+ code +" "+ reason +" \ n ");} @Overridepublic void OnError (Exception ex) {comn.pl ("Exception occured ... \ n" + ex + "\ n"); Ex.printstacktrace ();}};} catch (Exception e) {e.printstacktrace ();} Cc.connect(); Try{countdownlatch CDL = new Countdownlatch (1); Cc.setcdlconn (CDL); cdl.await ();} catch (Exception e) {e.printstacktrace ();} String m= "{\" id\ ": 2,\" command\ ": \" account_info\ ", \" account\ ": \" rlnr6ruczshrvheagmsr2kdhwncmhs2hzc\ ", \" strict\ ": True,\ "ledger_index\": \ "validated\"} "; byte[] B = null;try{b=m.getbytes (" UTF-8 ");} catch (Exception e) {e.printstacktrace ();} Cc.send (b); Try{countdownlatch CDL = new Countdownlatch (1); cc.setcdl (CDL); cdl.await ();} catch (Exception e) {e.printstacktrace ();} comn.pl ("Return:" +cc.getmsg ()); Cc.close (); return cc.getmsg ();}}




Material class

Import Java.net.uri;import java.util.arraylist;import java.util.list;import java.util.concurrent.CountDownLatch; Import Org.java_websocket.client.websocketclient;public abstract class Websocketclientext extends websocketclient{ Public Websocketclientext (URI Serveruri) {super (Serveruri);} Private list<object> Objs = new arraylist<object> ();p rivate String msg = null;private Countdownlatch CDL = nul L;private Countdownlatch cdlconn = null;public list<object> getobjs () {return OBJS;} public void Setobjs (list<object> objs) {this.objs = Objs;} Public String getmsg () {return msg;} public void Setmsg (String msg) {this.msg = msg;} Public Countdownlatch getcdl () {return CDL;} public void setcdl (Countdownlatch cdl) {this.cdl = CDL;} Public Countdownlatch Getcdlconn () {return cdlconn;} public void Setcdlconn (Countdownlatch cdlconn) {this.cdlconn = Cdlconn;}}

Just to print

public class COMn {public static void main (string[] args) {}public static void P (Object o) {System.out.print (o);} public static void pl (Object o) {System.out.println (o);}}


Welcome everyone to Exchange

WebSocket synchronous return with Countdownlatch implementation

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.