Ejemplo n.º 1
0
        public async Task OnCreateRoom()
        {
            try
            {
                Request request = new Request()
                {
                    UserID = 62,
                    GameID = 32,
                    RoomID = 52,
                };
                CreateExtInfo createEx = new CreateExtInfo()
                {
                    UserID     = 62,
                    RoomID     = 52,
                    State      = 2,
                    CreateTime = 1223344,
                };
                request.CpProto = ObjectToByteString(createEx);

                Package.Types.Frame message = new Package.Types.Frame()
                {
                    Type     = Package.Types.FrameType.CpsService,
                    Version  = 2,
                    CmdId    = (UInt32)MvsGsCmdID.MvsCreateRoomReq,
                    UserId   = 1001,
                    Reserved = 100,
                    Message  = ObjectToByteString(request),
                };
                using (var call = client.Stream(new Metadata {
                    { "ctx", "ctx" }, { "userid", "13" }
                }))
                {
                    var responseReaderTask = Task.Run(async() =>
                    {
                        while (await call.ResponseStream.MoveNext())
                        {
                            var note    = call.ResponseStream.Current;
                            Reply reply = new Reply();
                            ByteStringToObject(reply, note.Message);
                            Console.WriteLine("OnCreateRoom:receve msg:" + GetReply(reply));
                        }
                        Console.WriteLine("OnCreateRoom:response over");
                    });

                    Console.WriteLine("OnCreateRoom:send OnCreateRoom start");

                    await call.RequestStream.WriteAsync(message);

                    await call.RequestStream.CompleteAsync();

                    await responseReaderTask;
                }
            }
            catch (RpcException e)
            {
                Console.WriteLine("RPC failed", e);
                throw;
            }
        }
Ejemplo n.º 2
0
    /// <summary>
    /// 向directory服务注册gameServer
    /// </summary>
    private async void Stream()
    {
        Metadata meta = new Metadata
        {
            { "userid", userID + "" },
            { "token", "" }
        };

        try
        {
            using (var call = client.Stream(meta))
            {
                var resp = Task.Run(async() =>
                {
                    while (await call.ResponseStream.MoveNext())
                    {
                        //var frame = call.ResponseStream.Current;
                    }
                });
                await call.RequestStream.WriteAsync(LoginMsg());

                while (true)
                {
                    if (Stopped)
                    {
                        await call.RequestStream.WriteAsync(LogoutMsg());

                        break;
                    }
                    await call.RequestStream.WriteAsync(HeartbeatMsg());

                    System.Threading.Thread.Sleep(5000);
                }
                await call.RequestStream.CompleteAsync();

                await resp;
            }
        }
        catch (RpcException e)
        {
            Logger.Error("catch error {0}", e);
            Connect();
            System.Threading.Thread.Sleep(5000);
            Stream();
        }
    }