ByteArray To String , String To ByteArray
Unity3D 2012. 11. 8. 21:29
using UnityEngine;
using System.Collections;
using Nettention.Proud;
using System;
public class CClient : MonoBehaviour {
private NetClient _netClient = new NetClient();
private tk2dTextMesh _label;
private tk2dTextMesh _label2;
private HostID _id;
void Awake(){
this._label = GameObject.Find("Label").GetComponent<tk2dTextMesh>();
this._label2 = GameObject.Find("Label2").GetComponent<tk2dTextMesh>();
Debug.Log("Awake");
}
void Start () {
this._netClient.JoinServerCompleteHandler = OnJoinServerComplete;
this._netClient.LeaveServerHandler = OnLeaveServerHandler;
this._netClient.P2PMemberJoinHandler = OnP2PMemberJoinHandler;
this._netClient.ReceivedUserMessageHandler = OnReceivedUserMessageHandler;
NetConnectionParam param = new NetConnectionParam();//"{0x7b7e9c20,0x309c,0x4364,{0xb4,0x9c,0xc6,0xc,0xcd,0x25,0xaf,0xa0}}"
param.protocolVersion = new Guid("{0x7b7e9c20,0x309c,0x4364,{0xb4,0x9c,0xc6,0xc,0xcd,0x25,0xaf,0xa0}}");
param.serverPort = 5000; //32222;
//param.serverIP = "10.211.55.4"; TCPConnectFailure
param.serverIP = "192.168.0.99"; //OK
ByteArray userData = new ByteArray();
int rand = UnityEngine.Random.Range(0, 1000);
userData.data = StrToByteArray("client" + rand);
Debug.Log(userData + "," + userData.data + "," + ByteArrayToString(userData.data));
param.userData= userData;
this._netClient.Connect(param);
}
byte[] StrToByteArray(string str){
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
// Update is called once per frame
void Update () {
this._netClient.FrameMove();
}
void OnJoinServerComplete(ErrorInfo info, ByteArray replyFromServer){
Debug.Log("OnJoinServerComplete");
Debug.Log(replyFromServer);
/*
byte [] dBytes = replyFromServer.data;
string str;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);
*/
}
void OnLeaveServerHandler(ErrorInfo errorInfo){
this._label.text = errorInfo.ToString();
this._label.Commit();
}
void OnApplicationQuit(){
this._netClient.Disconnect();
}
void OnP2PMemberJoinHandler(HostID memberHostID, HostID groupHostID, int memberCount, ByteArray customField)
{
Debug.Log(memberHostID);
if( this._id == HostID.None ){
this._id = memberHostID;
this._label.text = this._id.ToString();
this._label.Commit();
}
}
public void C2CSendMessage(string str){
RmiContext rc = new RmiContext();
rc.enableLoopback = true;
rc.reliability = MessageReliability.Reliable;
HostID[] hostId = new HostID[]{this._id};
Debug.Log("send success: " + _netClient.SendUserMessage(hostId, rc, StrToByteArray(str) ));
}
void OnReceivedUserMessageHandler(HostID sender, RmiContext context, ByteArray message)
{
Debug.Log("OnReceivedUserMessageHandler");
}
string ByteArrayToString(byte[] b){
byte [] dBytes = b;
string str;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);
return str;
}
}
'Unity3D' 카테고리의 다른 글
Smuggle Truck 2D Techniques Talk (0) | 2012.11.19 |
---|---|
유니티 안드로이드 인앱결제 (0) | 2012.11.16 |
유니티 앱 종료시 감지 (0) | 2012.11.08 |
유니티 멀티게임 테스트 - 1 (1) | 2012.11.08 |
HideInInspector (0) | 2012.11.02 |