Beispiel #1
0
        public void processRequest(string requestdata)
        {
            JObject jObject = JObject.Parse(requestdata);

            MyTransactionFactory factory     = MyTransactionFactory.getInstance();
            MyBaseTransaction    transaction = factory.createTransaction(jObject);

            switch (transaction.MyTransactioncode)
            {
            case Transaction_Code.cl_client_connect_infor:
                string _password = _currentForm.getPassword();
                MyTr_Client_Connect_Infor transactionoff = (MyTr_Client_Connect_Infor)transaction;
                if (_password == transactionoff.password)
                {
                    // if password is match
                    this.Username = transactionoff.username;
                    MyBaseTransaction info = new MyBaseTransaction();
                    info.MyTransactioncode = Transaction_Code.sv_login_accept;
                    _mysendFactory.sendJsonObject(info);
                    _currentForm.addClientToListView(this);

                    _currentForm.addToReceiverText(">> User có id: " + id + " có tên: " + username);
                }
                else
                {
                    // if password not match
                    _currentForm.addToReceiverText(">> User có id: " + id + " thử kết nối nhưng không đúng mật khẩu.");
                    _mysendFactory.quickSendJsonObject(Transaction_Code.sv_incorrect_info);
                }
                break;

            case Transaction_Code.cl_disconnect:
                Stop();
                _currentForm.addToReceiverText(">> User " + username + "(" + id + ") đã ngắt kết nối!");
                break;

            case Transaction_Code.cl_answer_question:
                MyTr_Cl_AnswerQuestion trans_answer = new MyTr_Cl_AnswerQuestion();
                trans_answer = factory.recreateMyTr_Cl_AnswerQuestion(jObject);
                _currentForm.updateClientAnswer(this, trans_answer.Answer);
                setClientAnswer(trans_answer.Answer, _currentIndexQuestion);
                break;
            }
        }
Beispiel #2
0
        private void btLogin_Click(object sender, EventArgs e)
        {
            if (_socketMange.startConnection(tbIpServer.Text))
            {
                // send message to server
                MyTr_Client_Connect_Infor info = new MyTr_Client_Connect_Infor(tbName.Text, tbPassword.Text);

                _mysendFactory.sendJsonObject(info);

                NetworkStream networkStream = clientSocket.GetStream();
                byte[]        bytesFrom     = new byte[10025];
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                string dataFromClient = MyDecodeUnicode.DecodeFromUtf8(Encoding.Unicode.GetString(bytesFrom));

                processRequest(dataFromClient);
            }
            else
            {
                MessageBox.Show("Server not found!", "Alert");
            }
        }