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 ...
随机推荐
- Magento2 updated quote_item table - 更新quote_item 表自定义字段
/** * @param $class * @return mixed */ public function mc_get_obj($class) { return \Magento\Framewor ...
- [Python] for in单行循环生成dict
for循环体内的语句只有一行的情况的下,可以简化for循环的书写,尤其当你需要生成一个可迭代对象的时候 d = {x:x*10 for x in range(3)} print(d) d1 = [x* ...
- 纪中集训2020.02.05【NOIP提高组】模拟B 组总结反思——【佛山市选2010】组合数计算,生成字符串 PPMM
目录 JZOJ2290. [佛山市选2010]组合数计算 比赛时 之后 JZOJ2291. [佛山市选2010]生成字符串 比赛时 之后 JZOJ2292. PPMM 比赛时 之后 JZOJ2290. ...
- Linux内核提权漏洞(CVE-2019-13272)
漏洞描述 kernel / ptrace.c中的ptrace_link错误地处理了想要创建ptrace关系的进程的凭据记录,这允许本地用户通过利用父子的某些方案来获取root访问权限 进程关系,父进程 ...
- PWA - Manifest
manifest 在一个JSON文本文件中提供有关应用程序的信息(如名称,作者,图标和描述) manifest 的目的是将Web应用程序安装到设备的主屏幕 部署一个 manifest <link ...
- C++ STL:next_permutation和prev_permutation
两个函数都在#include <algorithm>里 顾名思义,next_permutation用来求下一个排列,prev_permutation用来求上一个排列. 当前的排列不满足函数 ...
- C# 引入Sqlite 未能加载文件或程序集“System.Data.SQLite
个人博客 地址:https://www.wenhaofan.com/article/20190501224046 问题 在Visual Studio 中 使用NuGet 通过 install-pack ...
- 关于eclipse 项目导入不了 maven依赖的解决办法
1.首先确定你的项目是maven 项目 ,如果不是:项目右键Configure -->Convert to maven project. 2.在SVN导出的Maven项目,或以前不是用Maven ...
- react-native-----hello word!
react-native运行helloword 今天是个特殊的时刻,我前天开始学习react-native,一直环境塔建出错,运行打包出错,今晚,我终于把这个难搞的环境给搭建好了,并成功运行了第一个h ...
- pymysql 连接池
pymysql连接池 import pymysql from DBUtils.PooledDB import PooledDB, SharedDBConnection ''' 连接池 ''' clas ...