Java WebSocketClient Does Local Data Receiving and Processing Service Program

Posted by MikeyNoedel on Thu, 03 Oct 2019 13:22:07 +0200

Client is the basic class

WebSocketClientTest is the main method entry.

maven address, other packages may need to be screenshots, etc.

<dependency>

<groupId>org.java-websocket</groupId>

<artifactId>Java-WebSocket</artifactId>

<version>1.3.0</version>

</dependency>

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.channels.NotYetConnectedException;
import java.util.Map;
 
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.handshake.ServerHandshake;
 
 
 
import net.sf.json.JSONObject;
 
public class Client extends WebSocketClient {
 
	public Client(URI serverUri, Draft protocolDraft, Map<String, String> httpHeaders, int connectTimeout) {
		super(serverUri, protocolDraft, httpHeaders, connectTimeout);
	}
 
	@Override
	public void onOpen(ServerHandshake arg0) {
		System.out.println("Open links");
		WebSocketClientTest.heartbeat=1;
	}
 
	@Override
	public void onMessage(String arg0) {
		if(arg0!=null){
			System.out.println("Receive message" + arg0);
			//Usually you perform the business you need here. Usually you return the JSON string and parse it for other operations.
		}
		
	}
 
	@Override
	public void onError(Exception arg0) {
		System.out.println("Error closed,Reconnect");
		WebSocketClientTest.heartbeat=0;
	}
 
	@Override
	public void onClose(int arg0, String arg1, boolean arg2) {
		System.out.println("Link closed,Reconnect");
		WebSocketClientTest.heartbeat=0;
	}
 
	@Override
	public void onMessage(ByteBuffer bytes) {
		try {
			System.out.println(new String(bytes.array(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			System.out.println("Abnormal");
			WebSocketClientTest.heartbeat=0;
		}
	}
}
import java.net.URI;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
 
import org.java_websocket.WebSocket.READYSTATE;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_17;
 
 
public class WebSocketClientTest {
	public static WebSocketClient client;
	
	public static int heartbeat = 0;// 0 stands for link disconnection or exception 1 stands for link. 2 stands for connection in progress.	
	public static String url="";//The requested path address includes ports
	public static void main(String[] args) throws Exception {
		Timer timer = new Timer();
		Task task = new Task();
		timer.schedule(task, new Date(), 5000);
	}
 
	public static void connect() throws Exception {
		client = new Client(new URI(url), new Draft_17(), null, 0);
		client.connect();
		int count = 0;
		heartbeat=2;
		while (!client.getReadyState().equals(READYSTATE.OPEN)) {
			count++;
			if (count % 1000000000 == 0) {
				System.out.println("It hasn't been opened yet.");
			}
		}
		client.send("Information sent to the server");
	}
 
	public static void reconnect() throws Exception {
		Thread.currentThread().sleep(15000);// Millisecond
		System.out.println("Start the attempt connection again");
		connect();
	}
 
	public static void send(byte[] bytes) {
		client.send(bytes);
	}
}
import java.util.TimerTask;


class Task extends TimerTask {
 
	@Override
	public void run() {
		try {
			System.out.println("Heartbeat detection:"+((WebSocketClientTest2.heartbeat == 1)?"Connection":"Unconnected"));
			if (WebSocketClientTest2.heartbeat ==0 ) {
				WebSocketClientTest2.connect();
			}
		} catch (Exception e) {
			e.printStackTrace();
 
		}
	}
}

The main code of webSocket is only posted here, and other business logic is not posted by JDBC.

What you need to pay attention to is about using Eclipse to punch jar packages.

I use Eclipse Java EE IDE for Web Developers.

Version: Mars Release (4.5.0)

According to the online installation of fatjar, the reason is actually using the wrong package. Here we need to use net.sf.fjep.fatjar_0.0.32.jar in Tucao.

Version 0.0.32 is available for personal testing. On the Internet, it is usually 0.0.31, which may be installed without response.

Here are links to installation and use

https://www.cnblogs.com/wbyp/p/6222182.html

After typing the jar package, you can run CMD java-jar xxxx/jar
 

Topics: Java JSON Eclipse Maven