代码优化
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package cn.wenat;
|
||||
|
||||
import java.sql.Time;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -50,24 +49,29 @@ public class LocalServer {
|
||||
private long speed = 0;// 实时网速统计
|
||||
|
||||
public void setDomain(String domain) {
|
||||
|
||||
this.domain = domain;
|
||||
}
|
||||
|
||||
public void setForward(String forward) {
|
||||
|
||||
this.forward = forward;
|
||||
}
|
||||
|
||||
public void setServer(String server) {
|
||||
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
private CallListener callListener;
|
||||
|
||||
public void setCallListener(CallListener callListener) {
|
||||
|
||||
this.callListener = callListener;
|
||||
}
|
||||
|
||||
public void ping() {
|
||||
|
||||
if (socket == null) {
|
||||
return;
|
||||
}
|
||||
@@ -105,6 +109,7 @@ public class LocalServer {
|
||||
|
||||
@Override
|
||||
public void eventCall(String eventName, String message, Object... args) {
|
||||
|
||||
if (null != message) {
|
||||
callListener.eventCall("[" + eventName + "]:" + message);
|
||||
}
|
||||
@@ -141,6 +146,7 @@ public class LocalServer {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
long temp = speed;
|
||||
speed = 0;
|
||||
callListener.speedCall(temp);
|
||||
@@ -153,18 +159,21 @@ public class LocalServer {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
ping();
|
||||
}
|
||||
}, 0, 10000);
|
||||
}
|
||||
|
||||
private void handlerPing(Object[] args) {
|
||||
|
||||
long time = (long) args[0];
|
||||
long ms = System.currentTimeMillis() - time;
|
||||
callListener.ping(ms);
|
||||
}
|
||||
|
||||
private void handlerBindDomain(Object... args) {
|
||||
|
||||
try {
|
||||
org.json.JSONObject jsonObject = (org.json.JSONObject) args[0];
|
||||
int code = jsonObject.getInt("code");
|
||||
@@ -188,6 +197,7 @@ public class LocalServer {
|
||||
* @param args
|
||||
*/
|
||||
private void handlerRequest(Object... args) {
|
||||
|
||||
org.json.JSONObject object = (org.json.JSONObject) args[0];
|
||||
JSONObject request = JSON.parseObject(object.toString());
|
||||
String url = request.getString("url");
|
||||
@@ -236,7 +246,7 @@ public class LocalServer {
|
||||
|
||||
// 兼容普通post和json/xml post
|
||||
if (contentType.equals("application/x-www-form-urlencoded")) {
|
||||
response = HttpClientUtils.post(post, params);
|
||||
response = HttpClientUtils.post(post, request.getJSONObject("body"));
|
||||
} else {
|
||||
//其他的全部postBody
|
||||
response = HttpClientUtils.postBody(post, request.getString("body"), encoding);
|
||||
@@ -278,6 +288,9 @@ public class LocalServer {
|
||||
}
|
||||
|
||||
byte[] bytes = (byte[]) response.getBody();
|
||||
if (bytes == null) {
|
||||
bytes = new byte[]{};
|
||||
}
|
||||
long length = bytes.length;
|
||||
this.traffic += length;
|
||||
this.speed += length;
|
||||
@@ -304,6 +317,7 @@ public class LocalServer {
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
|
||||
if (socket != null) {
|
||||
socket.close();
|
||||
socket = null;
|
||||
@@ -312,6 +326,7 @@ public class LocalServer {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
new MainForm().setVisible(true);
|
||||
|
||||
}
|
||||
|
||||
@@ -7,17 +7,22 @@ import java.awt.event.WindowEvent;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
/**
|
||||
* 父类
|
||||
*/
|
||||
public class BaseForm extends JFrame {
|
||||
|
||||
private static final long serialVersionUID = -8446051052250946428L;
|
||||
|
||||
public BaseForm() {
|
||||
|
||||
getContentPane().setLayout(null);
|
||||
getContentPane().setBackground(new Color(248, 251, 253));
|
||||
this.addWindowListener(new WindowAdapter() {
|
||||
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
|
||||
System.exit(0);
|
||||
super.windowClosing(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user