Unity中引入Supersocket.ClientEngine并测试
在使用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().SetId()
.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", ));
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().SetAge().SetSex(PersonMessage.Types.Sex.Male).SetName("zstudio").SetPhone("").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;
}


Unity中引入Supersocket.ClientEngine并测试的更多相关文章
- 【Unity3D技巧】在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信 (二) : 引入中间层NotificationCenter
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 一对多的观察者模式机制有什么缺点? 想要查看 ...
- Unity中使用WebView
Unity中使用WebView @(设计) 需求,最近游戏中需要引入H5直播页面和更新比较频繁的赛事页面,需求包括:加密传参数.和Unity交互,在Unity框架下其实有几种方案: 内置函数Appli ...
- SuperSocket.ClientEngine介绍
项目地址:https://github.com/kerryjiang/SuperSocket.ClientEngine 其中需要引入的SuperSocket.ProtoBase项目:SuperSock ...
- 在Unity中使用LitJson解析json文件
LitJson 这个库需要找资源,找到LitJson.dll后将它放在Assets文件夹下,在脚本中使用using引入即可 测试代码 json文件: {"Archice":[{&q ...
- 关于Unity中的UGUI优化,你可能遇到这些问题
https://blog.uwa4d.com/archives/QA_UGUI-1.html 关于Unity中的UGUI优化,你可能遇到这些问题 作者:admin / 时间:2016年11月08日 / ...
- 动画重定向技术分析和Unity中的应用
http://www.jianshu.com/p/6e9ba1b9c99e 因为一些手游项目需要使用Unity引擎,但在动画部分需要使用重定向技术来实现动画复用,考虑到有些项目开发人员没有过这方面的经 ...
- 动画重定向技术分析及其在Unity中的应用
前言 笔者新的手游项目使用Unity引擎,动画部分要使用重定向技术来实现动画复用.笔者之前在大公司工作的时候对这块了解比较深入,读过Havok引擎在这部分的实现源码,并基于自己的理解,在公司自研的手游 ...
- SUPERSOCKET.CLIENTENGINE 简单使用
首先 引用 SuperSocket.ClientEngine.Core.dll和 SuperSocket.ClientEngine.Common.dll 然后 就可以使用ClientEngine了. ...
- Unity中使用Attribute
Attribute是c#的语言特性 msdn说明如下: The Attribute class associates predefined system information or user-def ...
随机推荐
- PTA甲级B1061 Dating
目录 B1061 Dating (20分) 题目原文 Input Specification: Output Specification: Sample Input: Sample Output: 生 ...
- 使用SFTP连接Centos
1.centos已经配置好了SFTP,直接使用root用户连接就可以,模式选SFTP即可. 2.虽然端口号没有填写,默认端口号是22 3.可能还是会遇到无法访问的问题,可以进行iptables防火墙的 ...
- 航空航天专用Everspin非易失性MRAM存储器
TAMU是由瑞典乌普萨拉的Ångström航空航天公司(ÅAC)开发的高级磁力计子系统.TAMU的目的是提供地球磁场的磁力计数据,以便与子画面观测相关.实验性TAMU由使用领先技术制造的四种类型的设备 ...
- sklearn.metrics中的评估方法
https://www.cnblogs.com/mindy-snail/p/12445973.html 1.confusion_matrix 利用混淆矩阵进行评估 混淆矩阵说白了就是一张表格- 所有正 ...
- 内网hash传递
前言: 我们在平常打点的时候,遇到有内网或者有域的环境的时候,我们只获得了内网中的一台机子的shell,由这台机子我们可以获得这台机子所在的网段的相关其他主机.比如说有域的时候的域控机,有多层内网的堡 ...
- java中数据类型转换注意事项
1.byte.short.char这三种类型互相做数学运算时都会先提升为int类型后再做运算 char a = 'A'; short b = 1; int num = a + b;//a和b在做运算前 ...
- Truck History POJ - 1789 板子题
#include<iostream> #include<cstring> #include<algorithm> #include<stdio.h> u ...
- Genymotion下载及安装(安卓虚拟机)
Genymotion下载及安装 一.注册\登录 打开Genymotion官网,https://www.genymotion.com/ ,首先点击右上角的Sign in进行登录操作.如何登录就不细讲 ...
- poj2387- Til the Cows Come Home(最短路板子题)
题目描述 Description Bessie is out in the field and wants to get back to the barn to get as much sleep a ...
- caffe+win10+git使用sh文件
在windows下是否可以执行sh文件呢,搜了一下,可以安装了git就可以执行,当然这不是唯一答案. 然后联想到caffe下有一些.sh文件可以尝试,就用create_mnist.sh尝试把. cre ...