Unity中引入Supersocket.ClientEngine并测试
在使用Supersocket Server的过程中,发现Server是不支持.net 3.5的.
1.Server端中的几个Command:
1 namespace SuperSocketProtoServer.Protocol.Command 2 { 3 public class CallMessage : CommandBase<ProtobufAppSession, ProtobufRequestInfo> 4 { 5 public override void ExecuteCommand(ProtobufAppSession session, ProtobufRequestInfo requestInfo) 6 { 7 Console.WriteLine("CallMessage:{0}", requestInfo.Body.CallMessage.Content); 8 var backMessage = global::SuperSocketProtoServer.Pack.BackMessage.CreateBuilder().SetContent("Hello I am from C# server by SuperSocket") 9 .Build(); 10 11 var message = DefeatMessage.CreateBuilder().SetType(DefeatMessage.Types.Type.BackMessage) 12 .SetBackMessage(backMessage).Build(); 13 using (var stream = new MemoryStream()) 14 { 15 CodedOutputStream cos = CodedOutputStream.CreateInstance(stream); 16 cos.WriteMessageNoTag(message); 17 cos.Flush(); 18 byte[] data = stream.ToArray(); 19 session.Send(new ArraySegment<byte>(data)); 20 } 21 } 22 } 23 } 24 25 namespace SuperSocketProtoServer.Protocol.Command 26 { 27 public class BackMessage : CommandBase<ProtobufAppSession, ProtobufRequestInfo> 28 { 29 public override void ExecuteCommand(ProtobufAppSession session, ProtobufRequestInfo requestInfo) 30 { 31 Console.WriteLine("BackMessage:{0}", requestInfo.Body.BackMessage.Content); 32 } 33 } 34 } 35 36 namespace SuperSocketProtoServer.Protocol.Command 37 { 38 public class PersonMessage:CommandBase<ProtobufAppSession, ProtobufRequestInfo> 39 { 40 public override void ExecuteCommand(ProtobufAppSession session, ProtobufRequestInfo requestInfo) 41 { 42 Console.WriteLine("Recv Person Message From Client."); 43 Console.WriteLine("person's id = {0}, person's name = {1}, person's sex = {2}, person's phone = {3}", 44 requestInfo.Body.PersonMessage.Id, 45 requestInfo.Body.PersonMessage.Name, 46 requestInfo.Body.PersonMessage.Sex, 47 requestInfo.Body.PersonMessage.Phone); 48 49 var person = Pack.PersonMessage.CreateBuilder().SetAge(58).SetId(333) 50 .SetName("native .net to unity3d message").SetSex(Pack.PersonMessage.Types.Sex.Male).SetPhone("+86 110") 51 .Build(); 52 var message = DefeatMessage.CreateBuilder().SetType(DefeatMessage.Types.Type.PersonMessage) 53 .SetPersonMessage(person).Build(); 54 using (var stream = new MemoryStream()) 55 { 56 CodedOutputStream cos = CodedOutputStream.CreateInstance(stream); 57 cos.WriteMessageNoTag(message); 58 cos.Flush(); 59 byte[] data = stream.ToArray(); 60 session.Send(new ArraySegment<byte>(data)); 61 } 62 } 63 } 64 }
2.Client(unity3d)端中的代码:
1 using System; 2 using System.IO; 3 using System.Net; 4 using Google.ProtocolBuffers; 5 using Pack; 6 using SuperSocket.ClientEngine; 7 using SuperSocketProtoClient.Protocol; 8 using UnityEngine; 9 10 public class SupersocketClientHelper : SingletonGeneric<SupersocketClientHelper> 11 { 12 private EasyClient client; 13 private static SupersocketClientHelperStub stub = new GameObject("SupersocketClientHelperStub").AddComponent<SupersocketClientHelperStub>(); 14 15 private SupersocketClientHelper() 16 { 17 18 } 19 20 public void Initialize() 21 { 22 client = new EasyClient(); 23 client.Initialize(new ProtobufReceiveFilter(), packageInfo => 24 { 25 switch (packageInfo.Type) 26 { 27 case DefeatMessage.Types.Type.BackMessage: 28 Debug.LogFormat("BackMessage:{0}", packageInfo.Body.BackMessage.Content); 29 break; 30 case DefeatMessage.Types.Type.CallMessage: 31 Debug.LogFormat("CallMessage:{0}", packageInfo.Body.CallMessage.Content); 32 break; 33 case DefeatMessage.Types.Type.PersonMessage: 34 Debug.Log("Recv Person Message From SuperSocket Server."); 35 Debug.LogFormat("person's id = {0}, person's name = {1}, person's sex = {2}, person's phone = {3}", 36 packageInfo.Body.PersonMessage.Id, 37 packageInfo.Body.PersonMessage.Name, 38 packageInfo.Body.PersonMessage.Sex, 39 packageInfo.Body.PersonMessage.Phone); 40 break; 41 } 42 }); 43 44 // .net35的SuperSocket.ClientEngine没有实现或者说不支持该函数 45 //var flag = client.ConnectAsync(new DnsEndPoint("127.0.0.1", 2017)); 46 client.BeginConnect(new DnsEndPoint("127.0.0.1", 2017)); 47 client.Connected += (sender, args) => 48 { 49 var callMessage = CallMessage.CreateBuilder() 50 .SetContent("Hello I am form C# client by SuperSocket ClientEngine").Build(); 51 var message = DefeatMessage.CreateBuilder() 52 .SetType(DefeatMessage.Types.Type.CallMessage) 53 .SetCallMessage(callMessage).Build(); 54 55 using (var stream = new MemoryStream()) 56 { 57 CodedOutputStream os = CodedOutputStream.CreateInstance(stream); 58 os.WriteMessageNoTag(message); 59 os.Flush(); 60 byte[] data = stream.ToArray(); 61 client.Send(new ArraySegment<byte>(data)); 62 } 63 64 // 发送PersonMessage 65 var personMessage = PersonMessage.CreateBuilder() 66 .SetId(123).SetAge(33).SetSex(PersonMessage.Types.Sex.Male).SetName("zstudio").SetPhone("110").Build(); 67 message = DefeatMessage.CreateBuilder().SetType(DefeatMessage.Types.Type.PersonMessage) 68 .SetPersonMessage(personMessage).Build(); 69 using (var stream = new MemoryStream()) 70 { 71 CodedOutputStream cos = CodedOutputStream.CreateInstance(stream); 72 cos.WriteMessageNoTag(message); 73 cos.Flush(); 74 byte[] data = stream.ToArray(); 75 client.Send(new ArraySegment<byte>(data)); 76 } 77 }; 78 } 79 80 public void Destory() 81 { 82 client.Close(); 83 } 84 } 85 86 internal sealed class SupersocketClientHelperStub : MonoBehaviour 87 { 88 private void Awake() 89 { 90 DontDestroyOnLoad(this); 91 } 92 93 private void OnDestroy() 94 { 95 SupersocketClientHelper.Instance.Destory(); 96 } 97 }
3.消息使用Google.ProtocolBuffers:
message CallMessage { optional string content = 1; } message BackMessage { optional string content = 1; } message PersonMessage { required int32 id = 1; required string name = 2; enum Sex { Male = 1; Female = 2; } required Sex sex = 3 [default = Male]; required uint32 age = 4; required string phone = 5; } import "BackMessage.proto"; import "CallMessage.proto"; import "PersonMessage.proto"; message DefeatMessage { enum Type { CallMessage = 1; BackMessage = 2; PersonMessage = 3; } required Type type = 1; optional CallMessage callMessage = 2; optional BackMessage backMessage = 3; optional PersonMessage personMessage = 4; }