void Awake() { if (!mInstance) { mInstance = this; } //otherwise, if we do, kill this thing else { Destroy(this.gameObject); return; } serverData = GetComponent <ServerData> (); msgClient = GetComponent <MsgClient> (); binClient = GetComponent <BinClient> (); //backendTex.SetActive (false); if (mainCamera == null) { mainCamera = Camera.main; } //DontDestroyOnLoad(this.gameObject); }
private void ConnectButton_Click(object sender, RoutedEventArgs e) { try { IMessageService proxy = MsgClient.CreateClientChannel("http://localhost:8080/MessageService"); SendButton.IsEnabled = true; ConnectButton.IsEnabled = false; } catch (Exception exception) { MessageBox.Show("{0}", exception.Message); } }
/* * 抓到socket发来的消息。反序列化并交给command处理 * **/ private void OnConnectGetData(MemoryStream dataStream) { try { MsgClient msgClient = Serializer.Deserialize <MsgClient>(dataStream); MemoryStream mems = new MemoryStream(msgClient.msg); Action <MemoryStream> handlerAction = NetMsgMap.instance.getActionById(msgClient.type); if (null != handlerAction) { handlerAction(mems); } } catch (System.Exception ex) { UnityEngine.Debug.Log("socket get message error!"); } }
private void ListenButton_Click(object sender, RoutedEventArgs e) { string local = TextBox1.Text; try { host = MsgClient.CreateServiceChannel("http://localhost:" + local + "/MessageService"); host.Open(); rcvThrd = new Thread(new ThreadStart(this.ThreadProc)); rcvThrd.IsBackground = true; rcvThrd.Start(); ConnectButton.IsEnabled = true; ListenButton.IsEnabled = false; } catch (Exception exception) { MessageBox.Show("{0}", exception.Message); } }
/// <summary> /// 连接服务器,并进行登录 /// </summary> /// <returns></returns> private bool Connection() { try { //连接服务器 client = new MsgClient(7000, "127.0.0.1"); client.Connect(); AddMsg("连接成功,正在发送登录请求..."); //接收服务器反馈 //client.DoServerHandler += Receive; //Task.Run(() => { client.Receive(); }); Task.Run(() => Receive(client.ClientSocket)); //登录 return(LoginIn()); } catch (Exception ex) { return(false); } }
//客户端向服务端发消息打包 private byte[] Parse <T>(T pMsg, uint type) where T : global::ProtoBuf.IExtensible { byte[] pByteMsg = null; using (MemoryStream _MS = new MemoryStream()) { Serializer.Serialize <T>(_MS, pMsg); pByteMsg = _MS.ToArray(); } MsgClient xMsg = new MsgClient(); xMsg.type = type; xMsg.msg = pByteMsg; MemoryStream ms = new MemoryStream(); Serializer.Serialize <MsgClient>(ms, xMsg); byte[] byteArr = ms.ToArray(); byte[] pMsgHeadLen = DataFormat.shortToByte((short)byteArr.Length); byte[] pTrueMsg = DataFormat.ConnectBytes(pMsgHeadLen, byteArr); return(pTrueMsg); }
public FileClient() { this.msgClient = new MsgClient(); this.msgClient.Encrypt = EncryptUtils.Encrypt; this.msgClient.Decrypt = EncryptUtils.Decrypt; }