Unity中引入Supersocket.ClientEngine并测试
发表于2018-12-14
在使用Supersocket Server的过程中,发现Server是不支持.net 3.5的。
1.Server端中的几个Command:
namespace SuperSocketProtoServer.Protocol.Command { public class CallMessage : CommandBase<ProtobufAppSession, ProtobufRequestInfo> { public override void ExecuteCommand(ProtobufAppSession session, ProtobufRequestInfo requestInfo) { Console.WriteLine("CallMessage:{0}", requestInfo.Body.CallMessage.Content); var backMessage = global::SuperSocketProtoServer.Pack.BackMessage.CreateBuilder().SetContent("Hello I am from C# server by SuperSocket") .Build(); var message = DefeatMessage.CreateBuilder().SetType(DefeatMessage.Types.Type.BackMessage) .SetBackMessage(backMessage).Build(); using (var stream = new MemoryStream()) { CodedOutputStream cos = CodedOutputStream.CreateInstance(stream); cos.WriteMessageNoTag(message); cos.Flush(); byte[] data = stream.ToArray(); session.Send(new ArraySegment<byte>(data)); } } } } namespace SuperSocketProtoServer.Protocol.Command { public class BackMessage : CommandBase<ProtobufAppSession, ProtobufRequestInfo> { public override void ExecuteCommand(ProtobufAppSession session, ProtobufRequestInfo requestInfo) { Console.WriteLine("BackMessage:{0}", requestInfo.Body.BackMessage.Content); } } } namespace SuperSocketProtoServer.Protocol.Command { public class PersonMessage:CommandBase<ProtobufAppSession, ProtobufRequestInfo> { public override void ExecuteCommand(ProtobufAppSession session, ProtobufRequestInfo requestInfo) { Console.WriteLine("Recv Person Message From Client."); Console.WriteLine("person's id = {0}, person's name = {1}, person's sex = {2}, person's phone = {3}", requestInfo.Body.PersonMessage.Id, requestInfo.Body.PersonMessage.Name, requestInfo.Body.PersonMessage.Sex, requestInfo.Body.PersonMessage.Phone); var person = Pack.PersonMessage.CreateBuilder().SetAge(58).SetId(333) .SetName("native .net to unity3d message").SetSex(Pack.PersonMessage.Types.Sex.Male).SetPhone("+86 110") .Build(); var message = DefeatMessage.CreateBuilder().SetType(DefeatMessage.Types.Type.PersonMessage) .SetPersonMessage(person).Build(); using (var stream = new MemoryStream()) { CodedOutputStream cos = CodedOutputStream.CreateInstance(stream); cos.WriteMessageNoTag(message); cos.Flush(); byte[] data = stream.ToArray(); session.Send(new ArraySegment<byte>(data)); } } } }
2.Client(unity3d)端中的代码:
using System; using System.IO; using System.Net; using Google.ProtocolBuffers; using Pack; using SuperSocket.ClientEngine; using SuperSocketProtoClient.Protocol; using UnityEngine; public class SupersocketClientHelper : SingletonGeneric<SupersocketClientHelper> { private EasyClient client; private static SupersocketClientHelperStub stub = new GameObject("SupersocketClientHelperStub").AddComponent<SupersocketClientHelperStub>(); private SupersocketClientHelper() { } public void Initialize() { client = new EasyClient(); client.Initialize(new ProtobufReceiveFilter(), packageInfo => { switch (packageInfo.Type) { case DefeatMessage.Types.Type.BackMessage: Debug.LogFormat("BackMessage:{0}", packageInfo.Body.BackMessage.Content); break; case DefeatMessage.Types.Type.CallMessage: Debug.LogFormat("CallMessage:{0}", packageInfo.Body.CallMessage.Content); break; case DefeatMessage.Types.Type.PersonMessage: Debug.Log("Recv Person Message From SuperSocket Server."); Debug.LogFormat("person's id = {0}, person's name = {1}, person's sex = {2}, person's phone = {3}", packageInfo.Body.PersonMessage.Id, packageInfo.Body.PersonMessage.Name, packageInfo.Body.PersonMessage.Sex, packageInfo.Body.PersonMessage.Phone); break; } }); // .net35的SuperSocket.ClientEngine没有实现或者说不支持该函数 //var flag = client.ConnectAsync(new DnsEndPoint("127.0.0.1", 2017)); client.BeginConnect(new DnsEndPoint("127.0.0.1", 2017)); client.Connected += (sender, args) => { var callMessage = CallMessage.CreateBuilder() .SetContent("Hello I am form C# client by SuperSocket ClientEngine").Build(); var message = DefeatMessage.CreateBuilder() .SetType(DefeatMessage.Types.Type.CallMessage) .SetCallMessage(callMessage).Build(); using (var stream = new MemoryStream()) { CodedOutputStream os = CodedOutputStream.CreateInstance(stream); os.WriteMessageNoTag(message); os.Flush(); byte[] data = stream.ToArray(); client.Send(new ArraySegment<byte>(data)); } // 发送PersonMessage var personMessage = PersonMessage.CreateBuilder() .SetId(123).SetAge(33).SetSex(PersonMessage.Types.Sex.Male).SetName("zstudio").SetPhone("110").Build(); message = DefeatMessage.CreateBuilder().SetType(DefeatMessage.Types.Type.PersonMessage) .SetPersonMessage(personMessage).Build(); using (var stream = new MemoryStream()) { CodedOutputStream cos = CodedOutputStream.CreateInstance(stream); cos.WriteMessageNoTag(message); cos.Flush(); byte[] data = stream.ToArray(); client.Send(new ArraySegment<byte>(data)); } }; } public void Destory() { client.Close(); } } internal sealed class SupersocketClientHelperStub : MonoBehaviour { private void Awake() { DontDestroyOnLoad(this); } private void OnDestroy() { SupersocketClientHelper.Instance.Destory(); } }
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; }