コード例 #1
0
ファイル: NetManager.cs プロジェクト: nero1991/GameDev
    public bool connectTo(string name,string host,int port,NetStateChanged stateCallBack)
    {
        PomeloClient pclient = new PomeloClient();

        if (addClient (name, pclient)) {

            //listen on network state changed event
            pclient.NetWorkStateChangedEvent += (state) =>
            {
                string clientName = namesByClient[pclient];
                switch (state) {
                case NetWorkState.ERROR:
                    {
                        stateCallBack(clientName,SocketNetState.ERROR);
                    }break;
                    case NetWorkState.CLOSED:
                    {
                        stateCallBack(clientName,SocketNetState.CLOSED);
                    }break;
                    case NetWorkState.TIMEOUT:
                    {
                        stateCallBack(clientName,SocketNetState.TIMEOUT);
                    }break;
                    case NetWorkState.CONNECTED:
                    {
                        stateCallBack(clientName,SocketNetState.CONNECTED);
                    }break;
                    case NetWorkState.CONNECTING:
                    {
                        stateCallBack(clientName,SocketNetState.CONNECTING);
                    }break;
                    case NetWorkState.DISCONNECTED:
                    {
                        stateCallBack(clientName,SocketNetState.DISCONNECTED);
                    }break;
                }
            };

            pclient.initClient(host, port, () =>
            {
                //The user data is the handshake user params
                JsonObject user = new JsonObject();
                pclient.connect(user, data =>
                {
                    //process handshake call back data
                });
            });

            return true;
        } else {
            return false;
        }
    }
コード例 #2
0
ファイル: GateLogin.cs プロジェクト: jiangzhuo/mwClient
        private void button1_Click(object sender, EventArgs e)
        {
            var client = new PomeloClient();
            userData.client = client;
            client.NetWorkStateChangedEvent += (state) =>
            {
                statusLbl.Text = "network state change:" + state;
            };

            statusLbl.Text = "client initating";

            var newThread = new Thread(new ThreadStart(() =>
            {
                statusLbl.Text = "Begin login";
                var baseUrl = "http://" + serverHostCb.SelectedItem.ToString() + ":3003/";
                var requestUrl = "";
                var requestBody = "";
                if (tabControl1.SelectedIndex == 0)
                {
                    requestUrl = baseUrl + "login";
                    requestBody = "{" + String.Format("\"username\":\"{0}\",\"password\":\"{1}\"", usernameTb.Text, passwordTb.Text) + "}";
                }
                else
                {
                    requestUrl = baseUrl + "bindLogin";
                    requestBody = "{" + String.Format("\"platform\":\"{0}\",\"bindId\":\"{1}\"", platformCb.SelectedItem.ToString(), bindidTb.Text) + "}";
                }

                var request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                request.ContentType = "application/json; charset=utf-8";
                request.Accept = "application/json, text/javascript, */*";
                request.Method = "POST";
                try
                {
                    using (var writer = new StreamWriter(request.GetRequestStream()))
                    {
                        writer.Write(requestBody);
                    }
                }
                catch (Exception)
                {
                    statusLbl.Text = "no internet";
                    return;
                }

                var response = request.GetResponse();
                var stream = response.GetResponseStream();
                string json = "";

                using (var reader = new StreamReader(stream))
                {
                    while (!reader.EndOfStream)
                    {
                        json += reader.ReadLine();
                    }
                }

                dynamic loginResult = SimpleJson.SimpleJson.DeserializeObject(json);
                userData.ResultString = loginResult.ToString();
                if (loginResult.code != 200)
                {
                    statusLbl.Text = "login error:" + Convert.ToInt32(loginResult["code"]);
                    return;
                }
                else
                {
                    userData.Uid = loginResult.uid;
                    userData.Token = loginResult.token;
                }

                client.initClient(serverHostCb.SelectedItem.ToString(), Convert.ToInt32(portTb.Text),
                    () =>
                    {
                        statusLbl.Text = "client connecting";
                        client.connect(null, data =>
                        {
                            statusLbl.Text = "client connected";
                            statusLbl.Text += data.ToString();
                            Console.WriteLine("on data back" + data.ToString());
                            var msg = new JsonObject();
                            msg["uid"] = userData.Uid;
                            client.request("gate.gateHandler.queryEntry", msg, OnQuery);
                        });
                    });
            }));
            newThread.Start();
        }