Пример #1
0
    // Use this for initialization
    void Start()
    {
        StarXClient client = new StarXClient();

        client.Init("127.0.0.1", 32250, () =>
        {
            Debug.Log("init client callback");
            client.Connect((data) =>
            {
                Debug.Log("connect client callback");

                // 服务器主动推送消息
                client.On("OnServerNotify", (m) =>
                {
                    Debug.Log("OnServerNotify: " + Encoding.UTF8.GetString(m));
                });

                // 服务器主动推送消息
                client.On("OnServerNotify2", (m) =>
                {
                    Debug.Log("OnServerNotify2: " + Encoding.UTF8.GetString(m));
                });

                // 请求gate服务器,LoginHandler.Login服务
                client.Request("gate.LoginHandler.Login", Encoding.UTF8.GetBytes("login handler request test message"), (resp) =>
                {
                    Debug.Log("gate.LoginHandler.Login response: " + Encoding.UTF8.GetString(resp));
                });
                // 通知gate服务器,LoginHandler.NotifyTest服务
                client.Notify("gate.LoginHandler.NotifyTest", Encoding.UTF8.GetBytes("login handler notify test message"));

                // 请求gate服务器,LoginHandler.TestRpc服务
                client.Request("gate.LoginHandler.TestRpc", Encoding.UTF8.GetBytes("login handler TestRpc request test message"), (resp) =>
                {
                    Debug.Log("gate.LoginHandler.TestRpc response: " + Encoding.UTF8.GetString(resp));
                });

                // 请求chat服务器,ChatHandler.Chating服务
                client.Request("chat.ChatHandler.Chating", Encoding.UTF8.GetBytes("login handler request test message"), (resp) =>
                {
                    Debug.Log("chat.ChatHandler.Chating response: " + Encoding.UTF8.GetString(resp));
                });
                // 通知chat服务器,ChatHandler.UserIsInputing服务
                client.Notify("chat.ChatHandler.UserIsInputing", Encoding.UTF8.GetBytes("someone is inputing"));
            });
        });
    }
Пример #2
0
    // Use this for initialization
    void Start()
    {
        StarXClient client = new StarXClient();

        client.Init("127.0.0.1", 3250, () =>
        {
            Debug.Log("init client callback");
            client.Connect((data) =>
            {
                Debug.Log("connect client callback");

                // 服务器主动推送消息
                client.On("onNewUser", (m) =>
                {
                    NewUser nu = NewUser.Parser.ParseFrom(m);
                    Debug.Log("onNewUser: "******"onMembers", (m) =>
                {
                    AllMembers am = AllMembers.Parser.ParseFrom(m);
                    Debug.Log("onMembers: " + am.Members);
                });

                client.On("onMessage", (m) =>
                {
                    Testdata.UserMessage um = UserMessage.Parser.ParseFrom(m);
                    Debug.Log("onMessage: " + um.Name + " : " + um.Content);
                });

                //客户端请求,服务器回答
                Testdata.Ping first = new Testdata.Ping {
                    Content = "hello"
                };
                client.Request("room.join", first.ToByteArray(), (resp) =>
                {
                    JoinResponse jp = JoinResponse.Parser.ParseFrom(resp);
                    Debug.Log("room.join response: " + jp.Result);
                });

                // 客户端推送,没有回消息
                UserMessage msg = new UserMessage {
                    Name = "小明", Content = "我来了"
                };
                client.Notify("room.message", msg.ToByteArray());
            });
        });
    }
Пример #3
0
        static void Main(string[] args)
        {
            Console.CancelKeyPress += new ConsoleCancelEventHandler(myHandler);

            TerminationRequestedEvent = new ManualResetEvent(false);

            client = new StarXClient();
            client.NetWorkStateChangedEvent += (NetWorkState state) => {
                if (completed)
                {
                    Console.WriteLine();
                }
                if (state == NetWorkState.CONNECTED)
                {
                    connected = true;
                }
                Console.Write("status: ");
                Console.WriteLine(state);
            };
            client.Init("127.0.0.1", 3250, () =>
            {
                // Console.WriteLine("init client callback");
                client.Connect((data) =>
                {
                    Console.Write("Connect-response: ");
                    Console.WriteLine(Encoding.UTF8.GetString(data));
                    // Console.WriteLine("connect client callback");

                    // 服务器主动推送消息-新用户加入
                    client.On("onNewUser", (m) =>
                    {
                        Console.WriteLine("onNewUser-message: " + Encoding.UTF8.GetString(m));
                    });

                    // 服务器主动推送消息-用户离开
                    client.On("onUserLeft", (m) =>
                    {
                        Console.WriteLine("onUserLeft-message: " + Encoding.UTF8.GetString(m));
                    });
                    // 服务器主动推送消息-成员数更新
                    client.On("onMembers", (m) =>
                    {
                        Console.WriteLine("onMembers-message: " + Encoding.UTF8.GetString(m));
                    });

                    // 请求服务器,room.join 信息
                    client.Request("room.join", Encoding.UTF8.GetBytes("{}"), (resp) =>
                    {
                        Console.WriteLine("room.join-response: " + Encoding.UTF8.GetString(resp));
                    });
                });
            });


            string nickname = getNickName();

            while (!TerminationRequestedEvent.WaitOne(0))
            {
                Thread.Sleep(50);
                if (connected)
                {
                    Console.Write(string.Format("{0}> ", nickname));
                    string message = Console.ReadLine();
                    Dictionary <string, string> room_message = new Dictionary <string, string>();
                    room_message.Add("name", nickname);
                    room_message.Add("content", message);
                    // 通知服务器,room.message 信息
                    client.Notify("room.message", Encoding.UTF8.GetBytes(SimpleJson.SerializeObject(room_message)));
                }
                Thread.Sleep(50);
            }
            Thread.Sleep(50);
            Console.Write("Press any key to exit...");
            Console.ReadKey(true);
        }