public void request(string route, JsonObject param, Action <JsonObject> callback) { if (state == NetWorkState.CONNECTED) { Debug.Log("request:" + route + " param = " + param.ToString()); int reqId = NetworkTimmer.startTimeoutCounter(callback); pclient.request(route, param, (data) => { // do something with 'data' if need Loom.QueueOnMainThread(() => { Debug.Log(route + ": data = " + data); NetworkTimmer.stopTimeoutCounter(reqId); if (callback != null) { try { callback(data); } catch (Exception e) { Debug.Log(e); sendErrToService(route + ":data=" + data.ToString(), e); } } }); }); } else { // show the error or try to reconnect reconnect(null); } }
public void connectToServer(JsonObject user, Action <JsonObject> callback) { if (this.state != NetWorkState.CONNECTED) { Debug.Log("正在初始化:host: " + host + " port:" + port); int reqId = NetworkTimmer.startTimeoutCounter((JsonObject obj) => { if (callback != null) { callback(obj); } }); Loom.RunAsync(() => { pclient.initClient(host, port, () => { Loom.QueueOnMainThread(() => { Debug.Log("初始化成功: host:" + host + " port:" + port); if (InitSuccessDelegate != null) { InitSuccessDelegate(); } Debug.Log("开始连接Socket"); }); pclient.connect(user, (data) => { Debug.Log("连接Socket成功"); Loom.QueueOnMainThread(() => { NetworkTimmer.stopTimeoutCounter(reqId); // do something with 'data' if need if (ConnectSuccessDelegate != null) { ConnectSuccessDelegate(data); } if (callback != null) { callback(data); } }); }); }); }); } else { Loom.QueueOnMainThread(() => { Debug.Log("重复连接:host: " + host + " port:" + port); if (callback != null) { callback(new JsonObject()); } }); } }