代码优化

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