Exemplo n.º 1
0
 public Protocol(PomeloClient pc, System.Net.Sockets.Socket socket)
 {
     this.pc = pc;
     this.transporter = new Transporter (socket, this.processMessage);
     this.handshake = new HandShakeService(this);
     this.state = ProtocolState.start;
 }
Exemplo n.º 2
0
 public Protocol(PomeloClient pc, Socket socket)
 {
     this.pc          = pc;
     this.transporter = new Transporter(socket, new Action <byte[]>(this.processMessage));
     this.handshake   = new HandShakeService(this);
     this.state       = ProtocolState.start;
 }
Exemplo n.º 3
0
        public static void Main()
        {
            JsonObject server = read ("./server.json");
            string host = (string)server["host"];
            int port = Convert.ToInt32(server["port"]);

            Console.WriteLine ("before init");

            pc = new PomeloClient(host, port);

            Console.WriteLine ("before connect");
            pc.connect(null, delegate(JsonObject data){
                Console.WriteLine ("after connect " + data.ToString());
                JsonObject msg = new JsonObject();

                msg["uid"] = 1;
                pc.request("gate.gateHandler.queryEntry", msg, onQuery);
            });

            while(true) {
                string str = Console.ReadLine();
                if(str != null){
                    send (str);
                }
                System.Threading.Thread.Sleep(100);
            }
        }
Exemplo n.º 4
0
 public Protocol(PomeloClient pc, System.Net.Sockets.Socket socket)
 {
     this.pc          = pc;
     this.transporter = new Transporter(socket, this.ProcessMessage);
     this.transporter.onDisconnect = this.OnDisconnect;
     this.handshake = new HandShakeService(this);
     this.state     = ProtocolState.start;
 }
Exemplo n.º 5
0
        public Protocol(PomeloClient pc, TransporterBase inTransporter)
        {
            this.pc          = pc;
            this.transporter = inTransporter;
            this.transporter.SetMessageProcesser(this.processMessage);
            this.transporter.onDisconnect = onDisconnect;

            this.handshake = new HandShakeService(this);
            this.state     = ProtocolState.start;
        }
Exemplo n.º 6
0
 public void Connect(string host, int port)
 {
     this.host = host;
     this.port = port;
     socket    = new Pomelo.DotNetClient.PomeloClient(this.host, this.port);
     socket.connect((data_connect) => {
         Log("socket connect");
         Request("connector.entryHandler.entry", (data_enter) => {
             On("START", (data) => {
             });
         });
     });
 }
Exemplo n.º 7
0
        /// <summary>
        /// 连接gate服务器
        /// </summary>
        /// <param name="userName"></param>
        void LoginGateServer(string userName)
        {
            _pomelo = new PomeloClient(_gate_server_ip, _gate_server_port);
            AppendLog("开始连接 gate server  " + _gate_server_ip + ":" + _gate_server_port);
            _pomelo.connect(null, (data) => {
                AppendLog("成功连接 gate server :" + data.ToString());
                JsonObject msg = new JsonObject();
                msg["uid"] = userName;

                _pomelo.request("gate.gateHandler.queryEntry", msg, LoginGateServerCallback);
            });
        }
Exemplo n.º 8
0
        /// <summary>
        /// 连接connector服务器
        /// </summary>
        /// <param name="result"></param>
        void LoginConnectorServer(JsonObject result)
        {
            string host = (string)result["host"];
            int port = Convert.ToInt32(result["port"]);

            AppendLog("Connector Server 分配成功 " + result.ToString());

            _pomelo = new PomeloClient(host, port);

            _pomelo.connect(null, (data) => {
                AppendLog("成功连接 connector server " + host + ":" + port);
                JoinChannel(tb_name.Text, tb_channel.Text);
            });
        }
Exemplo n.º 9
0
        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();
        }
Exemplo n.º 10
0
        /// <summary>
        /// 连接gate服务器
        /// </summary>
        /// <param name="userName"></param>
        void LoginGateServer(string userName)
        {
            Console.WriteLine("开始连接 gate server  " + _gate_server_ip + ":" + _gate_server_port);
            _pomelo = new PomeloClient(_gate_server_ip, _gate_server_port);

            _pomelo.connect(null, (data) =>
            {
               // if (Convert.ToInt32(data["code"]) == 200)
                {
                    Console.WriteLine("成功连接 gate server :\n" + FormatJson(data.ToString()));
                    JsonObject msg = new JsonObject();
                    msg["uid"] = userName;
                    _pomelo.request("gate.gateHandler.queryEntry", msg, LoginGateServerCallback);
                }
                //else
                //{
                //    AppendLog("oh shit...连接 gate 出错..");
                //}
            });
        }
Exemplo n.º 11
0
        /// <summary>
        /// 连接connector服务器
        /// </summary>
        /// <param name="result"></param>
        void LoginConnectorServer(JsonObject result)
        {
            string host = (string)result["host"];
            int port = Convert.ToInt32(result["port"]);

            Console.WriteLine("Connector Server 分配成功,开始连接:" + host + ":" + port);

            _pomelo = new PomeloClient(host, port);

            _pomelo.connect(null, (data) =>
            {
                Console.WriteLine("成功连接 connector server:\n " + FormatJson(data.ToString()));
                JoinChannel(tb_name.Text, tb_channel.Text);
            });
        }
Exemplo n.º 12
0
        private static void startConnect(string host, int port)
        {
            pc = new PomeloClient(host, port);
            pc.connect(null, delegate(JsonObject data){
                pc.on("onChat", delegate(JsonObject msg) {
                    Console.WriteLine("{0} : {1}.", msg["from"], msg["msg"]);
                });
                pc.on("onAdd", delegate(JsonObject msg) {
                    Console.WriteLine("user {0} come in to the room.", msg["user"]);
                });
                pc.on("onLeave", delegate(JsonObject msg) {
                    Console.WriteLine("user {0} leave the room", msg["user"]);
                });
                pc.on("disconnect", delegate(JsonObject msg) {
                    Console.WriteLine("disconnect : " + msg);
                });

                //kick ();
                login ();
            });
        }