常州微信小程序开发-Unity3D使用Protobuf、ProtobufHelper

在序列化,Protobuf有着天然的优势,Protobuf作为Google的一个开源序列化库,因为使用的数据压缩算法等优化,序列化的数据较Xml更小,速度更快。下面常州网站开发培训就给大家介绍下Protobuf和ProtobufHelper的使用。

using

System.Text;

using

System.IO;

using

ProtoBuf;

namespace

Protobuf

class

ProtobufHelper

///

///序列化成string

///

///

///

/// 返回字符串

public static string

Serialize(T t)

using (MemoryStream ms =

new MemoryStream())

Serializer.Serialize(ms,

t);

return

Encoding.UTF8.GetString(ms.ToArray());

///

///序列化到文件

///

///

///

///

public static void

Serialize(string path, T data)

using (Stream file =

File.Create(path))

Serializer.Serialize(file,

data);

file.Close();

///

///

常州企业培训根据字符串反序列化成对象

///

///

///

///

public static T

DeSerialize(string content)

using (MemoryStream ms =

new MemoryStream(Encoding.UTF8.GetBytes(content)))

T t =

Serializer.Deserialize(ms);

return t;

///

///根据文件流返回对象

///

///

///

///

public static T

DeSerialize(Stream filestream)

return

Serializer.Deserialize(filestream);

using

UnityEngine;

using

System.Collections;

using

ProtoBuf;

#if false

[ProtoContract]

public class

Person

[ProtoMember(1)]

public string

Name{get;

set;}

[ProtoMember(2)]

public bool

Gender{get;

set;}

[ProtoMember(3)]

public string

Msg{get;

set;}

#endif

//------------------------------------------------------------------------------

//

// This code was generated

by a tool.

//

// Changes to this file may

cause incorrect behavior and will be lost if

// the code is

regenerated.

//

//------------------------------------------------------------------------------

// Generated from:

PersonInfo.proto

namespace

PersonInfo

[global::System.Serializable,

global::ProtoBuf.ProtoContract(Name=@"Person")]

public

partial class Person : global::ProtoBuf.IExtensible

public

Person(){}

private string _Child =

"";

[global::ProtoBuf.ProtoMember(1,

IsRequired = false, Name=@"Child", DataFormat =

global::ProtoBuf.DataFormat.Default)]

[global::System.ComponentModel.DefaultValue("")]

public string

Child

get{return

_Child;}

set{_Child =

value;}

private readonly

global::System.Collections.Generic.List _Parent = new

global::System.Collections.Generic.List();

[global::ProtoBuf.ProtoMember(2,

Name=@"Parent", DataFormat =

global::ProtoBuf.DataFormat.Default)]

public

global::System.Collections.Generic.List Parent

get{return

_Parent;}

private string

_Name;

[global::ProtoBuf.ProtoMember(3,

IsRequired = true, Name=@"Name", DataFormat =

global::ProtoBuf.DataFormat.Default)]

public string

Name

get{return

_Name;}

set{_Name =

value;}

private bool

_Gender;

[global::ProtoBuf.ProtoMember(4,

IsRequired = true, Name=@"Gender", DataFormat =

global::ProtoBuf.DataFormat.Default)]

public bool

Gender

get{return

_Gender;}

set{_Gender =

value;}

private string

_Msg;

[global::ProtoBuf.ProtoMember(5,

IsRequired = true, Name=@"Msg", DataFormat =

global::ProtoBuf.DataFormat.Default)]

public string

Msg

get{return

_Msg;}

set{_Msg =

value;}

private

global::ProtoBuf.IExtension extensionObject;

global::ProtoBuf.IExtension

global::ProtoBuf.IExtensible.GetExtensionObject(bool

createIfMissing)

{return

global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject,

createIfMissing);}

[global::System.Serializable,

global::ProtoBuf.ProtoContract(Name=@"SearchRequest")]

public

partial class SearchRequest :

global::ProtoBuf.IExtensible

public

SearchRequest(){}

private string

_query;

[global::ProtoBuf.ProtoMember(1,

IsRequired = true, Name=@"query", DataFormat =

global::ProtoBuf.DataFormat.Default)]

public string

query

get{return

_query;}

set{_query =

value;}

private int _page_number

= default(int);

[global::ProtoBuf.ProtoMember(2,

IsRequired = false, Name=@"page_number", DataFormat =

global::ProtoBuf.DataFormat.TwosComplement)]

[global::System.ComponentModel.DefaultValue(default(int))]

public int

page_number

get{return

_page_number;}

set{_page_number =

value;}

private int

_result_per_page = (int)10;

[global::ProtoBuf.ProtoMember(3,

IsRequired = false, Name=@"result_per_page", DataFormat =

global::ProtoBuf.DataFormat.TwosComplement)]

[global::System.ComponentModel.DefaultValue((int)10)]

public int

result_per_page

get{return

_result_per_page;}

set{_result_per_page =

value;}

private

SearchRequest.Corpus _corpus =

SearchRequest.Corpus.UNIVERSAL;

[global::ProtoBuf.ProtoMember(4,

IsRequired = false, Name=@"corpus", DataFormat =

global::ProtoBuf.DataFormat.TwosComplement)]

[global::System.ComponentModel.DefaultValue(SearchRequest.Corpus.UNIVERSAL)]

public

SearchRequest.Corpus corpus

get{return

_corpus;}

set{_corpus =

value;}

[global::ProtoBuf.ProtoContract(Name=@"Corpus")]

public enum

Corpus

[global::ProtoBuf.ProtoEnum(Name=@"UNIVERSAL",

Value=0)]

UNIVERSAL =

0,

[global::ProtoBuf.ProtoEnum(Name=@"WEB",

Value=1)]

WEB = 1,

[global::ProtoBuf.ProtoEnum(Name=@"IMAGES",

Value=2)]

IMAGES = 2,

[global::ProtoBuf.ProtoEnum(Name=@"LOCAL",

Value=3)]

LOCAL = 3,

[global::ProtoBuf.ProtoEnum(Name=@"NEWS",

Value=4)]

NEWS = 4,

[global::ProtoBuf.ProtoEnum(Name=@"PRODUCTS",

Value=5)]

PRODUCTS = 5,

[global::ProtoBuf.ProtoEnum(Name=@"VIDEO",

Value=6)]

VIDEO = 6

private

global::ProtoBuf.IExtension extensionObject;

global::ProtoBuf.IExtension

global::ProtoBuf.IExtensible.GetExtensionObject(bool

createIfMissing)

{return

global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject,

createIfMissing);}

using

UnityEngine;

using

System.Collections;

using

ProtoBuf;

using

System;

using

System.Collections.Generic;

using

System.IO;

using

PersonInfo;

public class

ProtobufTest1 : MonoBehaviour

private const string

Path = "C://data";

void Start()

Person p1 = new

Person(){Name = "小明", Gender =

true};

Person p2 = new

Person(){Name = "小红", Gender =

false};

List

persons = new List(){p1,

p2};

//序列化存储

Protobuf.ProtobufHelper.Serialize>(Path,

persons);

//或者

//string content =

Protobuf.ProtobufHelper.Serialize>(persons);

//File.WriteAllText(Path,

content);

//将数据从文件中读取出来,反序列化

List

Persons1;

using

(Stream file = File.OpenRead(Path))

Persons1 =

Protobuf.ProtobufHelper.DeSerialize>(file);

//或者

//List Persons1 =

Protobuf.ProtobufHelper.DeSerialize>(content);

foreach (Person p in

Persons1)

Debug.Log("Name:" +

p.Name + ": " + "Gender:" +

p.Gender);

using

Protobuf;

using

System;

using

UnityEngine;

using

WebSocket4Net;

using

PersonInfo;

public class

ProtobufTest2 : MonoBehaviour

//服务器IP地址和端口号

private string

ServerAdress;

//

常州软件技术培训玩家名字

private string PlayName

= "Aladdin";

//发送的消息

private string

message;

//接收到服务器发回来的消息

private string

ServerMessage;

public WebSocket

websocket;

void Start()

//服务器IP地址和端口号

ServerAdress =

"ws://127.0.0.1:2014/";

message = "Clients Send

Message";

ServerMessage = "Server

Message";

Application.runInBackground

= true;

void OnGUI()

//绘制2个TxtBox文本输入框

PlayName =

GUI.TextField(new Rect(10, 10, 100, 20), PlayName);

message =

GUI.TextArea(new Rect(10, 40, 200, 200), message);

ServerMessage =

GUI.TextArea(new Rect(10, 250, 400, 200),

ServerMessage);

//连接到常州平台运营服务器

if

(GUI.Button(new Rect(250, 10, 150, 40), "Client连接"))

ConnectToServer();

};

if

(websocket != null)

//测试向服务器发送消息

if (GUI.Button(new

Rect(250, 60, 150, 40), "SendMessageToServer"))

if (message.Length <

1 || PlayName.Length < 1)

return;

websocket.Send(ProtobufHelper.Serialize(new

Person(){Name = this.PlayName,

Gender = true, Msg = message}));

};

//断开连接按钮

if (GUI.Button(new

Rect(250, 120, 150, 40), "CloseSocket"))

websocket.Close();

websocket =

null;

};

private void

ConnectToServer()

websocket = new

WebSocket(ServerAdress);

websocket.Opened += new

EventHandler(websocket_Opened);

websocket.Closed += new

EventHandler(websocket_Closed);

websocket.MessageReceived

+= new EventHandler(websocket_MessageReceived);

websocket.Open();

///

///连接上常州微信公众平台服务器

///

///

///

private void

websocket_Opened(object sender, EventArgs e)

websocket.Send(PlayName

+ "Has Join The Game");

///

///接受连接

///

///

///

private void

websocket_MessageReceived(object sender, MessageReceivedEventArgse)

//TODO:这里要做消息分发处理,我这里就暂时默认服务器下发的就是Person的Protobuf的消息

var

personMsg = ProtobufHelper.DeSerialize(e.Message);

ServerMessage += "Name:"

+ personMsg.Name + "Gender:"

+ personMsg.Gender + "Info:"

+ personMsg.Msg + "\n";

///

///断开连接常州网站开发建设

///

///

///

private void

websocket_Closed(object sender, EventArgs e)

websocket.Close();

protobuf 微信小程序_常州微信小程序开发-Unity3D使用Protobuf、ProtobufHelper相关推荐

  1. 微信公众号_订阅号+服务号开发工具包-翟东平-专题视频课程

    微信公众号_订阅号+服务号开发工具包-15114人已学习 课程介绍         "微信公众平台深度开发Java版 v2.0"系列课程共有6季,使用JAVA语言,系统讲解微信公众 ...

  2. 计算机教室如何防火,2020校园防火安全小知识_消防安全小知识顺口溜

    消防安全知识必须人人懂,火灾隐患无处不在,稍不注意就容易引发大祸.所以,我们要从身边做起,从自己做起.以下是小编整理了关于2020校园防火安全小知识_消防安全小知识顺口溜,希望你喜欢. 校园防火安全小 ...

  3. python 自动化微信小程序_干货 | 微信小程序自动化测试最佳实践(附 Python 源码)...

    原标题:干货 | 微信小程序自动化测试最佳实践(附 Python 源码) 本文为霍格沃兹测试学院测试大咖公开课<微信小程序自动化测试>图文整理精华版. 随着微信小程序的功能和生态日益完善, ...

  4. 微信群打卡小程序_用微信小程序“小打卡”,打造免费的阅读平台!

    小归 一直和微信小程序--"小打卡"的创始人保持着微信通讯,也写了<快捷|以"小打卡"为例带你玩转微信小程序!>,这篇文章主要是真对如何打卡来写的, ...

  5. Spring Boot+微信小程序_保存微信登录者的个人信息

    1. 前言 微信小程序开发平台,提供有一类 API,可以让开发者获取到微信登录用户的个人数据.这类 API 统称为开放接口. Tip:微信小程序开发平台,会把微信登录用户的个人信息分为明文数据和敏感数 ...

  6. 仿qq邮箱源码程序_巧用小程序·云开发实现邮件发送功能丨实战

    先看效果图: 通过上面的日志,可以看出我们是158开头的邮箱给250开头的邮箱发送邮件,下面是成功接收到的邮件. 准备工作 1.qq邮箱一个 2.开通你的qq邮箱的授权码(会具体讲解) 3.注册自己的 ...

  7. wxml 判断 小程序_如何判断小程序外包公司是否靠谱

    微信小程序依附微信App,由于其无需下载,无需安装,无需注册,直接用微信授权登录,不占内存等优势在短短2年多的时间斩获超过10亿用户.又因为微信官方开通了小程序附近五公里展示以及在微信搜一搜增加&qu ...

  8. 大小端交换的程序_数据库在小程序云开发中的应用

    " 高效率数据库为小程序·云开发赋能,共同为客户提供更多优质解决方案. " 目录 小程序·云开发介绍小程序·云开发的数据库服务解决方案及客户成功案例 小程序云开发介绍 " ...

  9. 专属海报小程序_轻松生成小程序分享海报

    小程序海报组件 需求 小程序分享到朋友圈只能使用小程序码海报来实现,生成小程序码的方式有两种,一种是使用后端方式,一种是使用小程序自带的canvas生成:后端的方式开发难度大,由于生成图片耗用内存比较 ...

最新文章

  1. 开放-封闭原则(The Open-Closed Principle,OCP)
  2. 【Memcache】下载与安装
  3. NLP开源 CMU Sphinx
  4. 使用JSARToolKit5 开发AR应用 (2) Marker
  5. Java transient关键字使用小记
  6. Java安全的发布对象
  7. 在移动端H5开发中(关于安卓端position:fixed和position:absolute;和虚拟键盘冲突的问题,以及解决方案)...
  8. iOS 学习 - 18.TextField 自定义菜单事件,复制和微信分享
  9. 【14年浙江省赛 F ZOJ 3781】Paint the Grid Reloaded 【建图、bfs】
  10. 小菜鸟一步步打造图书馆外挂之十六:手动启动入口的实现
  11. bzero 和 memset 函数对比
  12. 在vue项目中使用阿里巴巴矢量图标库
  13. python 3.6 安装 win32 win32com模块
  14. python中pyserial模块使用方法
  15. img pdf 展示_vue中如何实现pdf文件预览的方法
  16. h5页面禁用手机识别
  17. 大众车机天宝187A Hack笔记
  18. Dubbo监控中心Dubbo-admin安装
  19. 网络隔离环境下的跨网数据传输,如何保障安全性?
  20. 极点突然中文标点变全角了

热门文章

  1. hadoop相关练习
  2. Python爬虫实战(二):抓取京东苹果手机评价
  3. 被算法“囚禁”的外卖骑手:我为你卖命工作,你让我成高危职业
  4. 虚幻引擎图文笔记:“ThirdPerson_Jump Asset“节点和“ThirdPerson_JumpStart Asset“节点到底是啥?
  5. 云鲸扫拖一体机器人说明书_让做家务变的更简单:云鲸智能扫拖一体机器人测评...
  6. html5视频上传云,vue+七牛云上传视频文件
  7. 关于mysql的项把他们都_卸载mysql | 学步园
  8. 实验2——指纹单模块实验
  9. 【SU-03T离线语音模块】:学习配置使用
  10. 楚留香服务器维护时间,《楚留香:一梦江湖》11月20日维护更新公告