All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mugui.base.client.net.bagsend.BagSend Maven / Gradle / Ivy

The newest version!
package com.mugui.base.client.net.bagsend;

import java.io.File;
import java.util.HashMap;

import org.apache.commons.lang3.StringUtils;

import com.mugui.base.base.Autowired;
import com.mugui.base.base.Component;
import com.mugui.base.client.net.baghandle.NetHandle;
import com.mugui.base.client.net.bean.Message;
import com.mugui.base.client.net.bean.NetBag;
import com.mugui.base.util.Other;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.RandomUtil;

@Component
public class BagSend {
	private String server;

	@Autowired
	private NetHandle netHandle;

	private String session;

	private String setSession(String session) {
//        SharedPreferences mugui = DataSave.app.getBaseContext().getSharedPreferences("mugui", Context.MODE_PRIVATE);
//        mugui.edit().putString("session", session).commit();
		return this.session = session;
	}

	private String getSession() {
//        SharedPreferences mugui = DataSave.app.getBaseContext().getSharedPreferences("mugui", Context.MODE_PRIVATE);
//        String session = mugui.getString("session", null);
//        return session;
		return session;
	}

	private HashMap sessionhead = new HashMap<>();

	private String http_server;
	private String ws_server;

	public void init() {
		http_server = "http://" + server + "/";
		ws_server = "ws://" + server + "/";
	}

	public void setServerUrl(String url) {
		server = url;
		init();
	}

	@Autowired
	private BagSendTask bagSendTask;

	public NetCall sendBag(NetBag bag, File... file) {

		NetCall netCall = new NetCall();
		bagSendTask.add(new Runnable() {
			@Override
			public void run() {
				bag.setHash(Other.MD5(bag.toString() + System.currentTimeMillis() + RandomUtil.randomInt()));
				bag.setSession(getSession());
				String post = null;
				try {
					if (file.length > 0) {
						post = HTTPUtil.post(http_server, sessionhead, bag.toJson(), file);
					} else {
						post = HTTPUtil.post(http_server, sessionhead, bag.toString());
					}
					if (post == null) {
						throw new RuntimeException(http_server + " bag:" + bag + "访问错误");
					}
				} catch (Exception e) {
					Message message = new Message();
					message.setDate(e.getMessage());
					message.setMsg("服务器出错了");
					message.setType(500);
					bag.setData(message);
					post = bag.toString();
				}
				NetBag bag2 = NetBag.newBean(NetBag.class, post);
				if (StringUtils.isNotBlank(bag2.getSession()))
					sessionhead.put("Cookie", "SESSION=" + (setSession(Base64.encode(bag2.getSession()))) + "");
				System.out.println("处理结果: " + netHandle.httpHandle(bag2, netCall));
			}
		});
		return netCall;
	}

	public NetCall sendData(String func) {
		return sendData(func, null);
	}

	public NetCall sendData(String func, Object data) {
		NetBag bag = new NetBag();
		bag.setFunc(func).setData(data);
		return sendBag(bag);
	}

	public NetCall sendData(String func, Object data, File file) {
		NetBag bag = new NetBag();
		bag.setFunc(func).setData(data);
		return sendBag(bag, file);
	}

	public NetCall subWs(String func, Object data) {
		NetCall netCall = WSUtil.subNetCall(func);
		ws("sub." + func, data);
		return netCall;

	}

	public NetCall subWs(String func) {
		return subWs(func, null);
	}

	public void unsubWs(String func) {
		WSUtil.unsubNetCall(func);
		ws("unsub." + func, null);
	}

	@Autowired
	private WsHandle wsHandle;

	private void ws(String type_func, Object data) {
		bagSendTask.add(new Runnable() {
			@Override
			public void run() {
				NetBag bag = new NetBag();
				bag.setFunc(type_func);
				bag.setData(data);
				bag.setHash(Other.MD5(bag.toString()));
				bag.setSession(Base64.decodeStr(getSession()));
				WSUtil.ws(ws_server, sessionhead, bag.toString(), wsHandle);
			}
		});
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy