荣耀彩票代理

  • 热门专题

开发日记:KBEngine+Unity+php做个扑克小游戏DAY2

作者:  发布日期:2016-11-14 20:23:55
  • ZENMEBAN,ZHEIHENGANGA,WEISHANI,YINWEIkbeDEMOUXIEYUANYINRANGWOFANGQILESHIYONGTASUOYIBENDASUANJIXUGENGXINDE,SHUOYIXIAYUANYIN:

    ZAIDAY1ZHONGWOXIWANGkbeNENGGOUKAIQIYIGEhttpFUWU,BINGRANGphpDUANZUOYIGEwebQINGQIUJIANGXIAOXICHUANDIJIDUIYINGDEYONGHU,KESHIZHEIGEhttpFUWUWOSHIXIEQILAILE,FASONGXIAOXIDEHANSHUYEXIECHULAI(HUALEBUSHAOSHIJIAN,kbeDEZHUSHIHEWENDANGDOUBUDUO,TEBIESHIkbeBABaseHttpServerZHEIGEpythonKULINGWAINONGLEGEMINGZI,YONGhttp.server import as CAIDAORUCHENGGONG)GANGADEJIUSHIhttpFUWUHEFAXIAOXIDEHANSHUZENMEYEFANGBUDAOYIQI:

    1.YIDANMOUGEclassBUJICHENGZIKBEngine.Base,NEIMETAJIUWUFAFANGWENKBEngineDEJIHUSUOYOUJINGTAIHANSHU、SHUXING,JIUWUFAHUOQUDAODUIYINGYONGHUDEmailboxWANCHENGXIAOXIFASONG

    2.YIDANJICHENGKBEgine.Base,NIJIUZUOBULEHTTP FUWU,YINWEINIDEhandlerBIXUJICHENGbaseHandler,NIJICHENGBULE,QIEJISHINIJICHENGDAObaseHandlerQUFANGWENKBEgine.BaseDEmailboxZHILEIDEYOUHUIDAOGANGDESILUOJIZHIZHONG

    3.XITONGKUDEHTTPFUWUHUIZUSAIJINCHENG,ZHEIGEWENDANGHAISHIXIELE,BUGUOTIDAIKUANGJIATAIMAFAN,QIEDIAOSHITAIBUFANGBIAN,QIEYUFATAISHUXI,QIE…………SUIRANWOXIANGSHUOYIWANGEQIE,ZHINENGSHUOMINGWOWUNENGA…………

    DANGRANLUNTANHEGUANWANGDANGZHONGYEYOURENFANYINGLEISIDEWENTI,LIRUDISANFANGJIEKOUFANGWENKBEDECHENGYUAN/SHUXINGWENTI,BUGUOKANQILAIHAOXIANGBINGMEIYOUXIANCHENGDEJIEJUEFANGAN,ZUIHOUDEZUIHOU。。。WOFANGQILE

     

    RANHOUNI~~WOZIJILAOLAOSHISHIXIELEYIGEXIAOXIFUWUQI(JIYUsocket ,with WPF .net 4.5+)YIJIXIAOXIXIEYI

    荣耀彩票代理XIAOXIXIEYICAIYONGhttp://msgpack.org/ JIBENSHANGZHIYUANSUOYOUDEYUYAN,YINCISHIJISHANGWOZHEIGEXIAOXIFUWUQIKEYIFUWURENHELEIXINGDEKEHUDUAN,BUGUANNISHAPINGTAISHAYUYAN

     

    1.0BANXIEYI(HAIMEIMINGZINI)GUIDING:

    1.BasePackDAIBIAOFASONGDEBAO,BaseAckPackDAIBIAOHUIZHIBAO,BaseAckPackJICHENGZIBasePack

    2.MEIGEPackZHANGDUWEI1024ZIJIE,QIEDI0~4ZIJIEZHUANHUANCHENGintDAIBIAOpackLEIXING, BasePackJIQIZILEICONG1.2.3...10... BaseAckPack JIQIZILEICONG1001,1002,1003...1010...(YOUKAOLVFUSHU,QISHIYINGGAIYEok)

    WEISHAZHEIYANGZUO? ZHEILIHENQITE,NIZAIBAZHEI1024GEZIJIEYONGmsgpackZHUANCHENGDUIXIANGZHIQIAN,NIBINGBUZHIDAOZHEIGEpackSHINAGEDUIXIANG,NIBUNENGTONGYIANMOUYIGETEDINGDEDUIXIANGQUZHUAN,BIRULoginPackBIBasePackZHIDUOLE2GESHUXING,NIZAIBUZHIDAOTASHIYIGELoginPackHAISHIYIGEBasePackZHIQIAN,NIWUFACHAIKAITA,NIANRENHEYIZHONGLAICHAIKAIDOUYOUKENENGCHUCUO(SHUXINGDUOLEHUOSHAOLE,SHUXIiOS DEKVCDEYINGGAIHENQINGCHU),SUOYIBIXUXIANBAQIANMIAN4GEZIJIETENGCHULAI,KEXUANDE,DI5~8GEZIJIEFANGZHANGDU(mespackKEYIZHANGDUDAYUNEIRONGCHAIKAIMEIWENTI),DU8GEZIJIEZHIHOUZAIDUSHENGXIADE1016(DANGRANBUYIDINGMEIGEBAOYIDINGDESHI1024,KEYIGENGDA,BIJINGWOMUQIANGOUYONGLE)GEZIJIE

     

    using System;
    //send包
    namespace Packs
    {
        //基础包
        public class BasePack<T>
        {
            public int packType;
            public int fromId;
            public int toId;
            public int messageId;
            //将基本包转bytes
            public byte[] PackToBytes()
            {
                var encode = MsgPack.Serialization.MessagePackSerializer.Get<T>();
                byte[] packContent = encode.PackSingleObject(this);
                byte[] type = BasePack.intToBytes(this.packType);
                byte[] len = BasePack.intToBytes(packContent.Length);
                int lenth = packContent.Length;
    
    
                byte[] dest = new byte[1024];
    
                //第一个int空间:类型
                Buffer.BlockCopy(type, 0, dest, 0, type.Length);
    
                //第二个int空间:长度
                Buffer.BlockCopy(len, 0, dest, type.Length, len.Length);
                
                //剩余空间:包内容
                Buffer.BlockCopy(packContent, 0, dest, type.Length+len.Length, packContent.Length);
    
                Console.WriteLine("打包pack,类型:" + this.packType + "长度:" + packContent.Length);
    
                return dest;
            }
    
            //将bytes转回基本包
            public static T BytesToPack(byte[] bytes)
            {
                var encode = MsgPack.Serialization.MessagePackSerializer.Get<T>();
                return encode.UnpackSingleObject(bytes);
            }
    
    
    
        }
    
        public class BasePack:BasePack<BasePack>
        {
            public const int LOGIN_PACK = 1;
            public const int REGISTER_PACK = 2;
            public const int PING_PACK = 3;
            public const int PONG_PACK = 4;
            public const int TEXT_PACK = 5;
            public const int SYSTEM_PUSH_PACK = 6;
    
            public const int LOGIN_ACK = 1001;
            public const int REGISTER_ACK = 1002;
            public const int PING_ACK = 1003;
            public const int PONG_ACK = 1004;
            public const int TEXT_ACK = 1005;
            public const int SYSTEM_PUSH_ACK = 1006;
            public const int CONNECTED_ACK = 1007;
    
           
    
            /**  
    * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。  
    * @param value  
    *            要转换的int值 
    * @return byte数组 
    */
            public static byte[] intToBytes(int value)
            {
                byte[] byte_src = new byte[4];
                byte_src[3] = (byte)((value & 0xFF000000) >> 24);
                byte_src[2] = (byte)((value & 0x00FF0000) >> 16);
                byte_src[1] = (byte)((value & 0x0000FF00) >> 8);
                byte_src[0] = (byte)((value & 0x000000FF));
                return byte_src;
            }
    
            /**  
           * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序。 
           *   
           * @param ary  
           *            byte数组  
           * @param offset  
           *            从数组的第offset位开始  
           * @return int数值  
           */
            public static int bytesToInt(byte[] ary, int offset)
            {
                int value;
                value = (int)((ary[offset] & 0xFF)
                        | ((ary[offset + 1] << 8) & 0xFF00)
                        | ((ary[offset + 2] << 16) & 0xFF0000)
                        | ((ary[offset + 3] << 24) & 0xFF000000));
                return value;
            }
        }
    
        //1.登录包
        public class LoginPack: BasePack<LoginPack>
        {
            public string username;
            public string password;
    
            public LoginPack()
            {
                this.packType = BasePack.LOGIN_PACK;
            }
    
        }
    
    
        //5.文字包
        public class TextPack:BasePack<TextPack>
        {
            public string content;
            public string toUser;
            public string fromUser;
    
            public TextPack() {
                this.packType = BasePack.TEXT_PACK;
            }
        }
    
        //6.系统推送包
        public class SystemPushPack: BasePack<SystemPushPack>
        {
            public string content;
            public string toUser;
      
            public SystemPushPack()
            {
                this.packType = BasePack.SYSTEM_PUSH_PACK;
            }
        }
    
       
    }
    


     

     

    荣耀彩票代理3.serverDUANAcceptZHIHOULIJIFASONGConnectPack,KEHUDUANSHOUDAOHOUFASONGConnectAckPackWANCHENGLIANJIE

     

     private void OnAccept()
        {
    
            while (this.isServing)
            {
                //异步Accept 回调ConnEnd
                //serverSocket.BeginAccept(new System.AsyncCallback(this.ConnEnd), null);
    
                //同步Accept
                Socket clientSocket = serverSocket.Accept();
    
                ReceiveObject obj = new ReceiveObject();
                obj.acceptClient = clientSocket;
                clients.Add(obj);
    
                Thread receiveThread = new Thread(OnReceive);
                receiveThread.Start(obj);
    
                cThreads.Add(clientSocket.RemoteEndPoint.ToString(), receiveThread);
                Console.WriteLine("新的客户端连接:" + clientSocket.RemoteEndPoint.ToString());
    
                BaseACKPack pack = new BaseACKPack();
                pack.packType = BasePack.CONNECTED_ACK;
    
                clientSocket.Send(pack.PackToBytes());
            }
        }


     

     

    4.KEHUDUANFASONGLoginPack(YOUYUphpYIJINGXIAOYANLEYONGHUMINGHEMIMABINGQIESHENGCHENGLEtoken,SUOYIloginPackSHIJISHANGWOMEIYOUXIEXIAOYANMIMADELUOJI,DANCHUNDEBANGDINGYONGHUMING,YONGLAIJIESHOUXIAOXI),serverDUANCHAIKAIpackJIANGYONGHUMINGBANGDINGDAOKEHUDUANDUIXIANGZHONG,ZHEIGEDUIXIANGDENEIRONGRUXIA:

     

     

    using System.Net.Sockets;
    public class ReceiveObject
    {
        public Socket acceptClient;
        public byte[] buffer = new byte[1024];
        public string userId;
        public string userName;
        public int roomId;
    
        public ReceiveObject()
        {
    
        }
    }

     

     

    整个处理函数:

     

        private void OnReceive(object obj)
        {
    
            while (this.isServing)
            {
    
                ReceiveObject e = obj as ReceiveObject;
                Socket c = e.acceptClient;
                e.buffer = new byte[1024];
    
                //判断包类型,固定包在包之前
                int type = c.Receive(e.buffer, 0, sizeof(Int32), SocketFlags.None);
                if (type == 0)
                {
    
                    Console.WriteLine("客户端断开连接:" + c.RemoteEndPoint.ToString());
    
                    //clients.RemoveAll((ReceiveObject obj) => { return obj.acceptClient == 0 ? true : false; });
                    clients.Remove(e);
                    cThreads.Remove(c.RemoteEndPoint.ToString());
                    Thread.CurrentThread.Abort();
                    //断开连接
                    c.Shutdown(SocketShutdown.Both);
                    c.Close();
                    break;
    
                }
    
                type = BasePack.bytesToInt(e.buffer, 0);
                //获得包大小,固定第2个int
                int len = c.Receive(e.buffer, 0, sizeof(Int32), SocketFlags.None);
                len = BasePack.bytesToInt(e.buffer, 0);
                int receiveNumber = c.Receive(e.buffer, 0, 1024 - sizeof(Int32) * 2, SocketFlags.None);
    
                switch (type)
                {
                    case BasePack.LOGIN_PACK:
                        {
                            LoginPack lPack = LoginPack.BytesToPack(e.buffer);
                            Console.WriteLine("收到登录请求,用户名:" + lPack.username + "密码:" + lPack.password);
    
                            e.userName = lPack.username;
    
                            //发送登录ACK
                            LoginACKPack loginACK = new LoginACKPack();
                            //loginACK.success = true;
    
                            c.Send(loginACK.PackToBytes());
                        }
                        break;
                    case BasePack.TEXT_PACK:
                        {
                            //处理消息包
                            TextPack pack = TextPack.BytesToPack(e.buffer);
                            //处理basePack
                            Console.WriteLine("发送给" + pack.toUser + "的消息:" + pack.content);
    
                            //从clients组找用户
                            List<ReceiveObject> list = clients.FindAll((ReceiveObject o) => { return o.userName == pack.toUser ? true : false; });
    
                            foreach (ReceiveObject target in list)
                            {
                                target.acceptClient.Send(pack.PackToBytes());
                            }
    
                        }
                        break;
                    case BasePack.TEXT_ACK:
                        {
                            //处理消息回执
                            TextACKPack pack = TextACKPack.BytesToPack(e.buffer);
    
                            //删除对应的消息
    
                            pusher.DeleteMessageById(pack.messageId);
    
                        }
                        break;
                    case BasePack.SYSTEM_PUSH_ACK:
                        {
                            SystemPushACKPack pack = SystemPushACKPack.BytesToPack(e.buffer);
    
                            //删除对应的消息
                            pusher.DeleteMessageById(pack.messageId);
                        }
                        break;
                    default:
                        //处理未知包
                        {
    
                        }
                        break;
                }
    
            }
        }

    发送消息函数,目前写了2个case 原因:php端的推送类型很多,我直接写在pushPack的content内部,客户端用json解析开就行了,然后做了一个单聊的文本消息发送,按群组推还没来得及做:

     

     

        public void SendMsg(string from, string to, string body, int type, int messageId)
        {
            //从clients组找用户
            ReceiveObject target = clients.FindLast((ReceiveObject o) => { return o.userName == to ? true : false; });
            if (target == null) return;
            //推送一条消息至客户端
            //收到回执后才能修改sent状态为1
            Console.WriteLine("推送消息给:" + to + "类型:" + type + "内容:" + body + "id:" + messageId);
            switch (type)
            {
    
                //推送文字消息
                case BasePack.TEXT_PACK:
                    {
                        TextPack txtPack = new TextPack();
                        txtPack.fromUser = from;
                        txtPack.toUser = to;
                        txtPack.content = body;
                        txtPack.messageId = messageId;
    
                        target.acceptClient.Send(txtPack.PackToBytes());
    
                    }
                    break;
                //系统消息
                case BasePack.SYSTEM_PUSH_PACK:
                    {
                        SystemPushPack txtPack = new SystemPushPack();
    
                        txtPack.toUser = to;
                        txtPack.content = body;
                        txtPack.messageId = messageId;
    
                        target.acceptClient.Send(txtPack.PackToBytes());
    
                    }
                    break;
                default:
                    {
    
                    }
                    break;
            }
        }
    }

     

     

    然后就是消息队列和php《-》c#间的调用问题

     

    1.YANGEANZHAOp2pMOXINGHEpubSubMOXINGDEXIAOXIDUILIE,JI:

    p2pMOXING: RUGUOXIAOXIJIESHOUZHEDEusernameZAIclientsSHUZUZHONG,LIJIFASONGBIAO,FOUZECUNRUSHUJUKUZUOWEILIXIANXIAOXI,DAIGAIYONGHUDENGLUSHIZAICONGSHUJUKUQUCHUGAIYONGHUDELIXIANXIAOXIZHINEICUNZHONGJIXUFASONG,ZHIDAOSHOUDAOXIANGYINGLEIXINGDEackHUObaseAck(KEHUDUANDEXIEYIBIFUWUQIDUANDI),CONGSHUJUKUZHONGCHEDIYICHU;

    pubSub MOXING:BUGUANXIAOXIJIESHOUZAIclientsSHUZUZHONGYOUDUOSHAOGE(XIANGTONGDEroomIdBIAOJI),0DAOLILUNSHANGXIANGE,LIJIFASONGQIEBUXUYAOHUIZHIQIELIJICONGNEICUNZHONGYICHUQIEBUCUNRUSHUJUKU

     

    2.YOUYUphpHEc#CHENGXUSHI2GEBUTONGDEJINCHENG,SUOYISHEJIDAOJINCHENGJIANTONGXIN,RUGUOZHEI2GECHENGXUYUNXINGZAITONGYITAIDIANNAOSHANG,KEXINGDEBANFAYOU:GONGXIANGNEICUN、BENDIsocket、GUANDAODENGDENG??DANSHISHIJIQINGKUANGKENENGWOMENGENGXIWANGwebCHENGXUHEXIAOXICHENGXUKEYIBUZAITONGYITAIDIANNAO,YINCIQITADEFANGFA:GONGXIANGTONGYIGESHUJUKULIANJIE、httpLUNXUN

    JUTIKEYIGENJUQINGKUANGXUANZE,WOZHEILILIANGZHONGDOUYOUXIE。

    QIEWODEQIWANGSHIphpMEICHARUYITIAOXIAOXI,c#MASHANGTUISONGCHUQU,NEIMEc#ZUOSHUJUKULUNXUNHUOZHEhttpLUNXUNQISHIDOUHAIHAO,WOZHIYONGLEYIGEXIANCHENGZUOLUNXUN。

     

    荣耀彩票代理ZUIHOUJINTIANXIEXIAYOUXIDUANBA:

    ZHONGYUKEYITUIGEZHONGBAOLE,KAISHIYOUXIBAO、CHUPAIBAO、SHENGLIBAO DAY1YIJINGMIAOSHU,MUQIANZAIZUODE: KEHUDUANPAIXINGXIAOYANYIJIMEIYIJUZHONGDEMEIYILUNHESHIPANDING。

    荣耀彩票代理ZHEIGEYOUXIGUIZEJIUSHIBIAOZHUNDEPAODEKUAI,YEJIUSHINADAOHEITAO3DEWANJIADIYIJUDIYILUNXIANCHUPAI,ZHEILIHAIMEIZUO,KEYIZAISUOYOUWANJIASHOUDAOKAISHIYOUXIBAOZHIHOUZUOYIGEJIANDANDEXIAOYAN。

    GUOPAIZHIJIEDIAOYONGCHUPAIJIEKOU,CHUANYIGEKONGDEZIFUCHUANJIKE,MUQIANHAIMEIYOUZHUDONGGUOPAIHEJIESHUMEIYILUNDELUOJI,ZUOLEJIESHUMEIYIJUDELUOJI,JIPANDINGSHENGFU。

    荣耀彩票代理ZUIHOUSHIJIGECESHIJIETU,WANJIAid 45 HEWANJIA id 50WANLEYIJU:

     

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规