开发环境:JDK1.7,Uniyt5.4.2,数据格式ProtoStuff1.08,Netty5.0.0,数据库MySQL。
代码对应关系如下:
首先实现数据模型设计,用于ProtoStuff数据传输。 这里的类服务端与客户端对应,以便消息的序列化与反序列化。
1 package com.netty.model; 2 3 import java.util.List; 4 5 public class SocketModel { 6 7 private int type; //消息类型 8 private int area; //消息区域 9 private int command; //消息指令 10 private List<String> message; //消息内容 11 12 public void setType(int type) 13 { 14 this.type = type; 15 } 16 17 public int getType() 18 { 19 return type; 20 } 21 22 public void setArea(int area) 23 { 24 this.area = area; 25 } 26 27 public int getArea() 28 { 29 return area; 30 } 31 32 public void setCommand(int command) 33 { 34 this.command = command; 35 } 36 37 public int getCommand() 38 { 39 return command; 40 } 41 42 public void setMessage(List<String> message) 43 { 44 this.message = message; 45 } 46 47 public List<String> getMessage() 48 { 49 return message; 50 } 51 }
1 using System.Collections.Generic; 2 using ProtoBuf; 3 4 [ProtoContract] 5 public class SocketModel { 6 7 [ProtoMember(1)] 8 private int type; //消息类型 9 10 [ProtoMember(2)] 11 private int area; //消息区域 12 13 [ProtoMember(3)] 14 private int command; //消息指令 15 16 [ProtoMember(4)] 17 private List<string> message; //消息内容 18 19 public SocketModel() 20 { 21 22 } 23 24 public SocketModel(int type, int area, int command, List<string> message) 25 { 26 this.type = type; 27 this.area = area; 28 this.command = command; 29 this.message = message; 30 } 31 32 public void SetType(int type) 33 { 34 this.type = type; 35 } 36 37 public int GetType() 38 { 39 return type; 40 } 41 42 public void SetArea(int area) 43 { 44 this.area = area; 45 } 46 47 public int GetArea() 48 { 49 return area; 50 } 51 52 public void SetCommand(int command) 53 { 54 this.command = command; 55 } 56 57 public int GetCommand() 58 { 59 return command; 60 } 61 62 public void SetMessage(List<string> message) 63 { 64 this.message = message; 65 } 66 67 public List<string> GetMessage() 68 { 69 return message; 70 } 71 }
1 package com.netty.protocol; 2 3 public class LoginProtocol { 4 5 public static final int Area_LoginRequest = 5; 6 public static final int Area_LoginResponse = 6; 7 8 public static final int Login_InvalidMessage = 0; 9 public static final int Login_InvalidUsername = 1; 10 public static final int Login_InvalidPassword = 2; 11 public static final int Login_Succeed = 10; 12 13 }
1 package com.netty.protocol; 2 3 public class TypeProtocol { 4 public static final int TYPE_LOGIN = 0; //登录 5 public static final int TYPE_USER = 1; //注册用户 6 public static final int TYPE_WIZARD = 2; //用户引导 7 public static final int TYPE_BATTLE = 3; //战斗 8 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class LoginProtocol{ 5 6 public const int Area_LoginRequest = 5; //login request 7 public const int Area_LoginResponse = 6; //login response 8 9 public const int Login_InvalidMessage = 0; //invalid message 10 public const int Login_InvalidUsername = 1; //invalid login username 11 public const int Login_InvalidPassword = 2; //invalid login password 12 13 public const int Login_Succeed = 10; //Login success 14 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class TypeProtocol { 5 6 public const int TYPE_LOGIN = 0; 7 public const int TYPE_USER = 1; 8 public const int TYPE_WIZARD = 2; 9 public const int TYPE_BATTLE = 3; 10 }
引用包下载地址(Netty&ProtoStuff&MySQL)