SFS支持常见的数据格式,但是倘若要传输自定义对象该如何实现呢?下面介绍下使用smartfoxserver 在unity客户端和java服务端之间传输自定义对象。
unity客户端:
1、自定义对象MyClan:
using UnityEngine;
using System.Collections;
using Sfs2X.Protocol.Serialization;
namespace com.magicbone.clan.serialization
{
public class MyClan : SerializableSFSType
{
public string name;
public int symbol;
public string description;
public int clantype;
public int minitrophy;
public int membercount;
public int trophycount;
public string GetJavaPackageName ()
{
return "com.magicbone.clan.serialization";
}
public MyClan(){
}
public string Name {
get { return name; }
set { name = value; }
}
public int Sysbol {
get { return symbol;}
set { symbol = value;}
}
public string Description {
get { return description;}
set { description = value;}
}
public int ClanType {
get { return clantype;}
set { clantype = value;}
}
public int MiniTrophy {
get { return minitrophy; }
set { minitrophy = value; }
}
public int MemberCount {
get { return membercount; }
set { membercount = value; }
}
public int TrophyCount {
get { return trophycount; }
set { trophycount = value; }
}
}
}
注意:这个自定义对象MyClan首先要加上命名空间,命名空间要和java服务端的自定义对象MyClan的包名保持一致。另外必须加上GetJavaPackageName()这个方法,否则sfs找不到我们自定义的这个对象。
2、客户端如何使用自定义对象:
DefaultSFSDataSerializer serializer = DefaultSFSDataSerializer.Instance;
DefaultSFSDataSerializer.RunningAssembly = Assembly.GetExecutingAssembly();
MyClan clan = new MyClan();
clan.Name = "clan";
clan.Sysbol = symbol;
clan.Description = description;
clan.ClanType = clantype;
clan.MiniTrophy = minitrophy;
clan.MemberCount = 0;
ISFSObject sfsObject = new SFSObject ();
sfsObject.PutClass("Clan",clan);
smartFox.Send(new ExtensionRequest("CreateClan",sfsObject));
注:必须加上DefaultSFSDataSerializer serializer = DefaultSFSDataSerializer.Instance;
DefaultSFSDataSerializer.RunningAssembly = Assembly.GetExecutingAssembly();
java服务端:
1、首先定义一个接口Clan,注意包名一定要和客户端的一致奥。
package com.magicbone.clan.serialization;
public interface Clan {
String getName();
void setName(String name);
int getSymbol();
void setSymbol(int symbol);
String getDescription();
void setDescription(String description);
int getClantype();
void setClantype(int clantype);
int getMinitrophy();
void setMinitrophy(int minitrophy);
int getMembercount();
void setMembercount(int membercount);
int getTrophycount();
void setTrophycount(int trophycount);
}
2、自定义数据对象实现接口Clan:
package com.magicbone.clan.serialization;
import com.smartfoxserver.v2.protocol.serialization.SerializableSFSType;
public class MyClan implements Clan, SerializableSFSType{
public String name;
public int symbol;
public String description;
public int clantype;
public int minitrophy;
public int membercount;
public int trophycount;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSymbol() {
return symbol;
}
public void setSymbol(int symbol) {
this.symbol = symbol;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getClantype() {
return clantype;
}
public void setClantype(int clantype) {
this.clantype = clantype;
}
public int getMinitrophy() {
return minitrophy;
}
public void setMinitrophy(int minitrophy) {
this.minitrophy = minitrophy;
}
public int getMembercount() {
return membercount;
}
public void setMembercount(int membercount) {
this.membercount = membercount;
}
public int getTrophycount() {
return trophycount;
}
public void setTrophycount(int trophycount) {
this.trophycount = trophycount;
}
@Override
public String toString() {
return "Clan [name=" + name + ", symbol=" + symbol + ", description="
+ description + ", clantype=" + clantype + ", minitrophy="
+ minitrophy + ", membercount=" + membercount
+ ", trophycount=" + trophycount + "]";
}
}
3、在接收的地方进行接收:
package com.magicbone.clan;
import com.magicbone.clan.serialization.MyClan;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
public class CreateClanHandler extends BaseClientRequestHandler {
private Mongo mongo;
@Override
public void handleClientRequest(User arg0, ISFSObject obj) {
MyClan clan = (MyClan)obj.getClass("Clan");
trace("clan:"+clan.toString());
}
}
此时所有的代码部分已经处理完毕,下面还有一个非常重要的步骤:
将服务端的Clan接口以及MyClan对象导出jar包到sfs安装目录下面的SFS2X\extensions\__lib__目录下,将其它的文件导出jar包到D:\Program Files\SmartFoxServer2X\SFS2X\extensions\Test目录下(Test名字可随便定义)。
ok,到这里全部大功告成。赶快启动sfs测试下奥,有问题可以留言奥,互相学习。
关注侯哥的博客奥:http://hougenit.iteye.com/admin
分享到:
相关推荐
SFS工具箱.mp4
SFS_V4.5培训
sfs源码
利用sfs算法对目标在不同角度的拍摄图像进行三维重构的MATLAB代码
序列前向选择方法(SFS)
sfs2x社区版
Spatial Framework Services (简称SFS),即...通过网络,SFS server能够将网络服务特征数据(Web Feature Service,简称WFS)或网络服务地图影像数据(Web Feature Service ,简称WMS)以流方式传输到客户端应用程序。
SFS algorithm matlab code
java\SFS\spring\hibernate\mysql
SFS2X游戏服务端搭建结合untiy3D的用户指导手册 希望能给大家一些启示
模式识别中用SFS算法实现特征选择MATLAB实现代码
Laravel开发-laravel-sfs Laravel应用程序中的简单雄辩的文件存储支持
有两个原图像,通过SFS算法三维恢复,并计算鸡蛋体积
SFS.12.0_Rio_Cracked.rar
实现特征选择的前向序列算法,又称为集合增加法。
将WAV格式文件转换为SFS格式的文件 需要的朋友可以试试
javascript sfs多个关键字请用空格分隔,最多填写5个。点击右侧Tag快速添加
SFS代码4_SFS特征选择_SFS_特征选择_SFS算法.zip
SFS相关的C语言代码,供大家学习使用