public void sendInvite() { notyet = false; StartCoroutine(chatConnect()); StopCoroutine(RecordVoice.record()); StopCoroutine(receive()); StopCoroutine(receiveWithLag()); }
public GroupChat() { InitializeComponent(); rVoice = new RecordVoice(this.axWindowsMediaPlayer1); //ftp连接类初始化 ftp = new FtpConnect(MainForm.clientConnect, MainForm.IP, ConfigurationManager.AppSettings["FtpName"], ConfigurationManager.AppSettings["FtpPassword"]); //*********************************** 参数要来自配置文件 ftp.ftpDownLoadFileSuccess += ftp_ftpDownLoadFileSuccess; // ftp.ftpConnectFailed += ftp_ftpConnectFailed; // this.groupChatHistRtb.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBox1_LinkClicked); }
IEnumerator chatConnect() { yield return(new WaitForSeconds(1.1f)); try{ notyet = true; string ipaddress = ipAd.text; //"192.168.1.255"; int port = int.Parse(portField.text); int portToSend = int.Parse(portToSendField.text); //10002; Byte[] sendBytes = Encoding.ASCII.GetBytes("message.text"); minUdpClient = new UdpClient(port); RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); //Creates an IPEndPoint to record the IP Address and port number of the sender. SendIPEndPoint = new IPEndPoint(IPAddress.Parse(ipaddress), portToSend); int loops = 0; Debug.Log("Starting"); StartCoroutine(RecordVoice.record()); StartCoroutine(receive()); status.text = ("connecting"); } catch (Exception e) { //Debug.Log(String.Format("Error: {0}", e.Message)); status.text = ("connecting problem " + e.Message); } while (notyet) { /* * minUdpClient.Send (sendBytes, sendBytes.Length, SendIPEndPoint); * //System.Threading.Thread.Sleep (1000); * //Debug.Log("sending UDP port : " + ((IPEndPoint)minUdpClient.Client.LocalEndPoint).Port.ToString()); * try { * if (minUdpClient.Available > 0) { * receiveBytes = minUdpClient.Receive (ref RemoteIpEndPoint); * String returnData = Encoding.ASCII.GetString (receiveBytes); * receivedMessage.text = ("Data Received:\n" + returnData + "\nfrom ip " + RemoteIpEndPoint.Address.ToString() + " from port " + RemoteIpEndPoint.Port.ToString ()); * status.text = ("UDP Connection Successful. Sending message and receiving message" +" ("+loops+")"); * //minUdpClient.Close (); * //Console.ReadLine (); * //notyet = false; * } else { * status.text = ("Sending message and waiting to receive message" +" ("+loops+")"); * } * } catch (Exception e) { * status.text = (e.ToString ()); * }*/ yield return(new WaitForSeconds(1)); } minUdpClient.Close(); status.text = ("Disconnected"); }
IEnumerator receiveWithLag() { while (true) { yield return(new WaitForSeconds(0.05f)); if (minUdpClient.Available > 0) { yield return(new WaitForSeconds(0.1f)); Debug.Log("receive data"); receiveBytes = minUdpClient.Receive(ref RemoteIpEndPoint); status.text = ("connected " + RemoteIpEndPoint.ToString()); RecordVoice.playSound(receiveBytes); } } }
IEnumerator receive() { while (true) { yield return(new WaitForSeconds(0.0005f)); if (minUdpClient.Available > 0) { try { Debug.Log("receive data"); receiveBytes = minUdpClient.Receive(ref RemoteIpEndPoint); status.text = ("connected " + RemoteIpEndPoint.ToString()); RecordVoice.playSound(receiveBytes); } catch (Exception e) { //Debug.Log(String.Format("Error: {0}", e.Message)); status.text = ("connecting problem " + e.Message); } } } }