110,533
社区成员
发帖
与我相关
我的任务
分享
private function onConnect( event:Event ):void {
//连上服务端后压缩单体对象发送到服务端
textField.text = "已连接上服务器,下面发送消息...";
var param: Object = {Id: '1', Name: '赵果', Address: '中国浙江'};
var _bytes:ByteArray = new ByteArray();
_bytes.writeObject(param);
_bytes.compress();
socket.writeBytes(_bytes);
socket.flush();
}
private function onData( event:ProgressEvent ):void {
//接收到服务端的消息解压缩后内容也能正常显示
var _bytes:ByteArray = new ByteArray();
while(socket.bytesAvailable > 0){
socket.readBytes(_bytes);
}
_bytes.uncompress();
var tmp:Object = _bytes.readObject();
textField.text = tmp.Id + ',' + tmp.Name + ',' + tmp.Address;
}
private void onReceive(ICourageService server, object target)
{
.......
SocketAsyncEventArgs _eventArgs = target as SocketAsyncEventArgs;
byte[] _bytes = _eventArgs.Buffer;//服务端接收到的二进制数据
//利用FluorineFx组件实现ByteArray解压缩
MemoryStream ms = new MemoryStream(_bytes, 0, _eventArgs.BytesTransferred);
FluorineFx.AMF3.ByteArray ba = new FluorineFx.AMF3.ByteArray(ms);
ba.Uncompress();
STCServer.Node node = (STCServer.Node)ba.ReadObject();//问题就出在这里,运行到此行报错说无法将AsObject对象强制转换成Node类对象
......
}
namespace STCServer
{
public class Node
{
public uint ID;
public string Name;
public string Address;
}
}
public static class AmfEntityConvert
{
delegate object ConvertAction(object data);
static Dictionary<Type, ConvertAction> dicTypeRelation = new Dictionary<Type, ConvertAction>();
static AmfEntityConvert()
{
dicTypeRelation[typeof(FluorineFx.AMF3.ByteArray)] = new ConvertAction((object data) =>
{
byte[] bytes = null;
if (data != null)
{
bytes = (data as FluorineFx.AMF3.ByteArray).GetBuffer();
}
return bytes;
});
}
public static T ConvertObject<T>(object asObject)
{
if (asObject != null)
{
Type type = asObject.GetType();
if (dicTypeRelation.Keys.Contains(type))
{
return (T)dicTypeRelation[type](asObject);
}
else
{
return (T)Convert.ChangeType(asObject, typeof(T));
}
}
return default(T);
}
public static object ConvertData(Type type, object obj, params object[] pams)
{
Type classType = typeof(AmfEntityConvert);
MethodInfo staticMethod = classType.GetMethod("ConvertObject");
MethodInfo genericMethod = staticMethod.MakeGenericMethod(type);
return genericMethod.Invoke(obj, pams);
}
/// <summary>
/// 转换ASObject对象为.Net类型对象
/// </summary>
/// <param name="pObj"></param>
/// <param name="type"></param>
/// <returns></returns>
public static object ConvertEntity(ASObject asObjeect, Type type)
{
try
{
var tempObj = Activator.CreateInstance(type);
foreach (var asObj in asObjeect)
{
string key = asObj.Key;
object value = asObj.Value;
MemberInfo[] members = type.GetMember(key);
if (null != value && members != null && members.Length > 0)
{
object newValue = null;
Type asValueType = asObj.Value.GetType();
//.Net类型实体成员
MemberInfo member = members[0];
Type memberType = null;
FieldInfo field = null;
PropertyInfo property = null;
if (member.MemberType == MemberTypes.Field)
{
field = type.GetField(member.Name);
memberType = field.FieldType;
}
if (member.MemberType == MemberTypes.Property)
{
property = type.GetProperty(member.Name);
memberType = property.PropertyType;
}
if (property != null || field != null)
{
//如果是ASObject对象
if (asValueType == typeof(ASObject))
{
newValue = ConvertEntity((ASObject)value, memberType);
}
//如果是数组集合
else if (asValueType == typeof(Object[]))
{
Type subtype = Assembly.GetAssembly(memberType).GetType(memberType.FullName.Replace("[]", ""), true);
object[] arrobj = (Object[])value;
var objData = Array.CreateInstance(subtype, arrobj.Length);
for (int nn = 0; nn < arrobj.Length; nn++)
{
FluorineFx.ASObject asData = (FluorineFx.ASObject)arrobj[nn];
var val = ConvertEntity(asData, subtype);
objData.SetValue(val, nn);
}
newValue = objData;
}
//基本类型
else
{
newValue = ConvertData(memberType, null, value);
}
if (field != null)
{
field.SetValue(tempObj, Convert.ChangeType(newValue, memberType));
}
else if (property != null && property.CanWrite)
{
property.SetValue(tempObj, Convert.ChangeType(newValue, memberType), null);
}
}
}
}
return tempObj;
}
catch (Exception ex)
{
return null;
}
}
}