private void HandshakeHandler(Package package) { Message <Heartbeat> msg = MessageProtocol.Decode <Heartbeat>(package.buff); if (msg.err > 0) { handshakeTcs.TrySetResult(false); OnError?.Invoke(msg.errMsg); return; } if (heartBeatServiceGo == null) { GameObject go = new GameObject(); go.name = "heartBeatServiceGo"; heartBeatServiceGo = go.AddComponent(typeof(HeartBeatServiceGameObject)) as HeartBeatServiceGameObject; heartBeatServiceGo.Setup(msg.info.heartbeat, OnServerTimeout, socket); } else { OnReconected?.Invoke(); heartBeatServiceGo.ResetTimeout(msg.info.heartbeat); }//*/ handshakeTcs.TrySetResult(true); }
public async UniTask <Message <S> > RequestAsync <T, S>(string route, T info = default, string modelName = null) { uint rqID = (uint)Interlocked.Increment(ref RqID); try { #if SOCKET_DEBUG Debug.Log(string.Format("[{0}][Request] -->> [{1}] {2}", rqID, route, JsonUtility.ToJson(info))); #endif Package pack = await protocol.RequestAsync <T>(rqID, route, info, modelName); Message <S> msg = MessageProtocol.Decode <S>(pack.buff); #if SOCKET_DEBUG Debug.Log(string.Format("[{0}][Request] <<-- [{1}] {2} {3}", rqID, route, JsonUtility.ToJson(msg), JsonUtility.ToJson(msg.info))); #endif return(msg); } catch (Exception e) { //Debug.Log(string.Format("[{0}][RequestAsync Exception]{1}", rqID, e.Message)); throw e; } }
private void ErrorHandler(Package package) { Message <byte[]> msg = MessageProtocol.Decode <byte[]>(package.buff); Debug.LogError(string.Format("packType:{2} err:{0} msg:{1}", msg.err, msg.errMsg, package.packageType)); }
private async UniTaskVoid CreateConeccetion() { Debug.Log("開始連線..." + host); int count = 3; bool isConeccet = false; while (count-- > 0 && !isConeccet) { Debug.Log(host); isConeccet = await client.ConnectAsync("jon"); } if (isConeccet) { img.gameObject.SetActive(true); // On client.On("testOn", (Package pack) => { TestPush info = MessageProtocol.DecodeInfo <TestPush>(pack.buff); Debug.Log(JsonUtility.ToJson(info)); //img.gameObject.SetActive(false); }); //請求/響應 TestRq testRq = new TestRq(); Message <TestRp> a = await client.RequestAsync <TestRq, TestRp>("TestController.testA", testRq); if (a.err > 0) { Debug.LogWarning("err:" + a.err); Debug.LogWarning("err msg:" + a.errMsg); } else { Debug.Log("a:" + a.info.packageType); } //請求/響應 Message <TestRp2> a3 = await client.RequestAsync <TestRq, TestRp2>("TestController.testC", null, "custom1"); if (a3.err > 0) { Debug.LogWarning("err:" + a3.err); Debug.LogWarning("err msg:" + a3.errMsg); } else { Debug.Log("a:" + a3.info.info); } //通知 //TestNotify testRq2 = new TestNotify() { name="小叮噹" }; //client.Notify("TestController.testB", testRq2); //*/ } else { Debug.Log("多次嘗試連線但依然未連線"); } }