Exemple #1
0
    public void OnLoginClick()
    {
        if (String.IsNullOrEmpty(Id.text))
        {
            return;
        }
        if (String.IsNullOrEmpty(Pw.text))
        {
            return;
        }

        var client = SocketService.Locator;

        if (!client.Connected())
        {
            client.Connect("127.0.0.1", 2001);
        }
        client.Send(ProtoMaker.Pack(new Client2Server.UserAuth
        {
            Username = Id.text,
            Password = Pw.text,
        }));

        Pw.text = ""; // パスワードリセット
    }
Exemple #2
0
        static void ReceivedEvent(Session session, byte[] bytes)
        {
            switch (ProtoMaker.GetProtoType(bytes))
            {
            case ProtoType.Admin:
                var admin = ProtoMaker.Unpack <Client2Server.Admin>(bytes);
                Console.WriteLine(admin.Message);
                break;

            case ProtoType.UserAuth:
                var auth = ProtoMaker.Unpack <Client2Server.UserAuth>(bytes);
                Console.WriteLine("Username : {0}, Password : {1}", auth.Username, auth.Password);
                // TODO : DBから認証情報をチェック
                session.userInfo.Auth();
                session.userInfo.Username = auth.Username;
                session.Send(ProtoMaker.Pack(new Server2Client.UserAuth
                {
                    Username = session.userInfo.Username,
                }));
                break;

            case ProtoType.UserInfo:
                var info = new Server2Client.UserInfo();
                info.numUser  = sessions.Count;
                info.Username = new string[sessions.Count];
                for (var i = 0; i < sessions.Count; ++i)
                {
                    info.Username[i] = sessions[i].userInfo.Username;
                }
                session.Send(ProtoMaker.Pack(info));
                break;

            case ProtoType.UserChat:
                var c2sChat = ProtoMaker.Unpack <Client2Server.UserChat>(bytes);
                var s2CChat = ProtoMaker.Pack(new Server2Client.UserChat
                {
                    Username = session.userInfo.Username,
                    Message  = c2sChat.Message
                });
                // 全員配信
                for (var i = 0; i < sessions.Count; ++i)
                {
                    sessions[i].Send(s2CChat);
                }
                break;

            case ProtoType.CmdMove:
                // 全員配信
                var s2CMove = ProtoMaker.Pack(ProtoType.UserMove);
                for (var i = 0; i < sessions.Count; ++i)
                {
                    sessions[i].Send(s2CMove);
                }
                break;
            }
        }
Exemple #3
0
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Return))
     {
         if (!String.IsNullOrEmpty(input.text))
         {
             SocketService.Locator.Send(ProtoMaker.Pack(new Client2Server.UserChat
             {
                 Message = input.text
             }));
             input.text = "";
         }
     }
 }
Exemple #4
0
    void Received(byte[] bytes)
    {
        switch (ProtoMaker.GetProtoType(bytes))
        {
        case ProtoType.Admin:
            var admin = ProtoMaker.Unpack <Server2Client.Admin>(bytes);
            LoggerService.Locator.Log(admin.Message);
            break;

        case ProtoType.UserAuth:
            var auth = ProtoMaker.Unpack <Server2Client.UserAuth>(bytes);
            LoggerService.Locator.Log("Username : {0}", auth.Username);
            SceneManager.LoadScene("Room");     // 入室
            break;
        }
    }
Exemple #5
0
    void Received(byte[] bytes)
    {
        switch (ProtoMaker.GetProtoType(bytes))
        {
        case ProtoType.UserInfo:
            var info = ProtoMaker.Unpack <Server2Client.UserInfo>(bytes);
            usernames.text = String.Join("\n", info.Username);
            break;

        case ProtoType.UserChat:
            var chat = ProtoMaker.Unpack <Server2Client.UserChat>(bytes);
            AddMessage(String.Format("{0} : {1}", chat.Username, chat.Message));
            break;

        case ProtoType.UserMove:     // 誰か入室した
            // ユーザ一覧取得:手軽なのでこのサンプルは一覧すべて取得する
            SocketService.Locator.Send(ProtoMaker.Pack(ProtoType.UserInfo));
            break;
        }
    }
Exemple #6
0
        static void TaskReceive(TcpListener listener)
        {
            while (true)
            {
                // 接続要求あるかどうか確認
                if (listener.Pending())
                {
                    // 接続要求を処理する
                    var client = listener.AcceptTcpClient();
                    sessions.Add(new Session(client, ReceivedEvent));
                    Console.WriteLine("AcceptTcpClient : {0}", client.Client.RemoteEndPoint);
                }
                // 受信処理
                foreach (var session in sessions)
                {
                    session.Poll();
                }

                // 接続切断したものがあれば削除
                if (sessions.RemoveAll(s => s.Disconnected) > 0)
                {
                    // TODO : 以下コピペーなのでリファクタリング
                    var info = new Server2Client.UserInfo();
                    info.numUser  = sessions.Count;
                    info.Username = new string[sessions.Count];
                    for (var i = 0; i < sessions.Count; ++i)
                    {
                        info.Username[i] = sessions[i].userInfo.Username;
                    }
                    // 削除されたものがあるので情報を再送信
                    foreach (var session in sessions)
                    {
                        session.Send(ProtoMaker.Pack(info));
                    }
                }
                Task.Delay(16).Wait();
            }
        }
Exemple #7
0
 void Start()
 {
     SocketService.Locator.AddReceivedEvent(Received);
     // 入室
     SocketService.Locator.Send(ProtoMaker.Pack(ProtoType.CmdMove));
 }