// Confirm to Register void cfmClick() { // get uset information string phone_num = canvas.transform.Find("RegisterDialog/phoneRegister/Text").gameObject.GetComponent <Text>().text; string pass_word = canvas.transform.Find("RegisterDialog/passRegister").gameObject.GetComponent <InputField>().text; string re_pass = canvas.transform.Find("RegisterDialog/repassRegister").gameObject.GetComponent <InputField>().text; tip_panel = canvas.transform.Find("tipPanel").gameObject; tip = canvas.transform.Find("tipPanel/Text").gameObject; // check phone number if (!CheckPhoneIsAble(phone_num)) { tip.GetComponent <Text>().text = "手机号不合法"; tip_panel.SetActive(true); Invoke("hideTips", 0.8f); } // check pass words if (pass_word != re_pass) { tip.GetComponent <Text>().text = "两次输入密码不一致"; tip_panel.SetActive(true); Invoke("hideTips", 0.8f); } else if (pass_word.Length < 6) { tip.GetComponent <Text>().text = "密码长度不得小于6位"; tip_panel.SetActive(true); Invoke("hideTips", 0.8f); } else { // Send phone_num and pass_word to backends UserInfo info = new UserInfo(); info.phone_number = phone_num; info.password = pass_word; string json = JsonConvert.SerializeObject(info); string post_url = "http://39.106.107.66:3000/add-user"; string resp_json = postData(post_url, json); // if(phone_num is occupied) ... else ... RegisterResp resp = JsonConvert.DeserializeObject <RegisterResp>(resp_json); if (resp.result_status == 0) { tip.GetComponent <Text>().text = "该手机号已注册过"; tip_panel.SetActive(true); Invoke("hideTips", 0.8f); } else { tip.GetComponent <Text>().text = "注册成功,请登录"; tip_panel.SetActive(true); Invoke("hideTips", 0.8f); RegisterDialogObj.SetActive(false); Invoke("showLogin", 0.3f); } } }
/// <summary> /// 连接服务器 /// </summary> /// <param name="serverIp">IP</param> /// <param name="serverPort">端口</param> /// <param name="connectCallback">连接成功的回调</param> /// <param name="connectFailedCallback">连接失败的回调</param> public void ConnectServer(string serverIp, int serverPort, ConnectResultCallback connectCallback) { RegisterResp.RegisterAll(); connectResultDelegate = connectCallback; socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress address = IPAddress.Parse(serverIp); IPEndPoint endpoint = new IPEndPoint(address, serverPort); socket.NoDelay = true; IAsyncResult result = socket.BeginConnect(endpoint, new AsyncCallback(ConnectedCallback), socket); //超时监测,当连接超过5秒还没成功表示超时 bool success = result.AsyncWaitHandle.WaitOne(5000, true); if (!success) { Closed(); connectResultDelegate?.Invoke(ConnectResult.fail); } else { isStopReceive = false; receiveMsgThread = new Thread(new ThreadStart(ReceiveMessage)); receiveMsgThread.IsBackground = true; receiveMsgFlag = false; receiveMsgThread.Start(); sendMsgThread = new Thread(new ThreadStart(SendCheck)); sendMsgThread.IsBackground = true; sendMsgFlag = false; sendMsgThread.Start(); } }
public void Connect(ConnectCallback connectCallback, ConnectCallback connectFailedCallback) { connectDelegate = connectCallback; connectFailedDelegate = connectFailedCallback; //采用TCP方式连接 socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //服务器IP地址 IPAddress address = IPAddress.Parse(NetProtocols.SERVER_ADDRESS); //服务器端口 IPEndPoint endpoint = new IPEndPoint(address, NetProtocols.SERVER_PORT); //异步连接,连接成功调用connectCallback方法 IAsyncResult result = socket.BeginConnect(endpoint, new AsyncCallback(ConnectedCallback), socket); //这里做一个超时的监测,当连接超过5秒还没成功表示超时 bool success = result.AsyncWaitHandle.WaitOne(5000, true); if (!success) { //超时 Closed(); NetTools.Log("connect Time Out"); if (connectFailedDelegate != null) { connectFailedDelegate(); } } else { //与socket建立连接成功,开启线程接受服务端数据。 isStopReceive = false; Thread thread = new Thread(new ThreadStart(ReceiveSorket)); thread.IsBackground = true; thread.Start(); } // RegisterResp.RegisterAll(); }