Exemplo n.º 1
0
        /// connected to server with specified IP and port
        public void ConnectServer(string ip, int port)
        {
            try
            {
                IPAddress  mIp          = IPAddress.Parse(ip);
                IPEndPoint ip_end_point = new IPEndPoint(mIp, port);

                try {
                    clientSocket.Connect(ip_end_point);
                    IsConnected = true;
                    Debug.Log("connection success");
                }
                catch
                {
                    IsConnected      = false;
                    encounteredError = true;
                    error            = "Failed to connect to " + ip + ":" + port;

                    Debug.Log("connection failed to " + ip + ":" + port);
                    return;
                }


                //retrieve meetingID and token
                GameObject pBoard   = GameObject.FindGameObjectsWithTag("pBoard")[0];
                ReadFile   readFile = pBoard.GetComponent(typeof(ReadFile)) as ReadFile;

                //receive once
                Message message = ReceiveMessage(result, clientSocket);

                //Debug.Log(message.Signal.Equals("AUTH"));
                //receive AUTH and send TOKEN
                if (message.Signal.Equals("AUTH"))
                {
                    ByteBuffer bf2 = new ByteBuffer();
                    bf2.WriteString("TOKE\n");
                    bf2.WriteString(readFile.GetAuthToken() + PAYLOAD_UUID_ENDING);                    // the token
                    clientSocket.Send(bf2.ToBytes());
                }

                message = ReceiveMessage(result, clientSocket);

                //receive NVAL or MEET
                if (message.Signal.Equals("MEET"))
                {
                    ByteBuffer bf2 = new ByteBuffer();
                    bf2.WriteString("MID\n");
                    bf2.WriteString(readFile.getMeetingCode() + PAYLOAD_UUID_ENDING);                    //meeting ID
                    clientSocket.Send(bf2.ToBytes());
                }
                else if (message.Signal.Equals("NVAL"))
                {
                    //end the meeting
                    encounteredError = true;
                    error            = "Authentication Failed";
                    Debug.Log("connection fail");
                    return;
                }

                message = ReceiveMessage(result, clientSocket);

                //reveive NVAL or VAL
                if (message.Signal.Equals("VAL"))
                {
                    //Debug.Log("connection success aaa");
                    //connect success return clientSocket?
                    Thread listeningThread = new Thread(ListeningMessages);
                    listeningThread.Start();
                    Thread sendingThread = new Thread(SendingMessages);
                    sendingThread.Start();
                }
                else if (message.Signal.Equals("NVAL"))
                {
                    //end the meeting
                    encounteredError = true;
                    error            = "Authentication Failed";
                    Debug.Log("connection fail");
                    return;
                }
            }catch (ApplicationException e) {
                encounteredError = true;
                error            = e.Message;
                return;
            }
        }