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

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

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


import com.mugui.base.util.Other;

import java.util.HashMap;


public class WSUtil {

	public static void ws(String serverUrl, HashMap sessionhead, String string, WsHandle wsHandle) {
		if (wsHandle.getClient() == null) {
			synchronized (WSUtil.class) {
				if (wsHandle.getClient() == null) {
					wsHandle.init(serverUrl);
					sessionhead.forEach((k, v) -> {
						wsHandle.getClient().addHeader(k, v);
					});
					wsHandle.getClient().connect();
					long time = System.currentTimeMillis();
					while(!wsHandle.getClient().isOpen()||System.currentTimeMillis()-time>10000){
						Other.sleep(10);
					}
					if(!wsHandle.getClient().isOpen()){
						throw new RuntimeException("连接超时");
					}
				}
			}
		}
		wsHandle.getClient().send(string);
	}
	private static HashMap wsNetCalls = new HashMap<>();

	public static NetCall getWsNetCall(String func){
		return wsNetCalls.get(func.replace("sub.", "").replace("unsub.", ""));
	}
	public static NetCall subNetCall(String func){
		NetCall netCall=null;
		synchronized (wsNetCalls) {
			netCall = wsNetCalls.get(func);
			if (netCall == null) {
				netCall = new NetCall();
				wsNetCalls.put(func, netCall);
			}
		}
		return wsNetCalls.get(func);
	}

	public static void unsubNetCall(String func) {
		synchronized (wsNetCalls) {
			wsNetCalls.remove(func);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy