public void SendChat() { inputString = inputMsg.GetComponent <InputField>().text; if (inputString == "") { return; } // 聊天数据包 Chat.ChatProto proto = new Chat.ChatProto(); proto.userName = this.inputName; proto.chatMsg = inputString; NetPacket p = new NetPacket(); p.BeginWrite("chat"); p.WriteObject <Chat.ChatProto>(proto); p.EncodeHeader(); clientPeer.Send(p); // 清空消息 if (queue.Count > 14) { queue.Dequeue(); } queue.Enqueue(proto.userName + ": " + inputString + "\n"); Que2Str(); hisMsgTextField.text = hisMsg; inputMsg.GetComponent <InputField>().text = ""; }
//send chat message public void SendChat() { //pack message in a packet and send Chat.ChatProto proto = new Chat.ChatProto(); proto.userName = "******"; proto.chatMsg = inputString; NetPacket p = new NetPacket(); p.BeginWrite("chat"); p.WriteObject <Chat.ChatProto>(proto); p.EncodeHeader(); clientPeer.send(p); inputString = ""; }
// 处理聊天消息 public void OnChat(NetPacket packet) { Debug.Log("收到服务器消息"); Chat.ChatProto proto = packet.ReadObject <Chat.ChatProto>(); revString = proto.userName + ":" + proto.chatMsg; if (proto.userName != this.inputName) { if (queue.Count > 14) { queue.Dequeue(); } queue.Enqueue(proto.userName + ": " + proto.chatMsg + "\n"); Que2Str(); hisMsgTextField.text = hisMsg; } }
/* * void OnGUI() * { * // 显示收到的聊天记录 * GUI.Label(new Rect(5, 5, 200, 30), revString); * * // 输入聊在消息 * inputString = GUI.TextField(new Rect(Screen.width * 0.5f - 200, Screen.height * 0.5f - 20, 400, 40), inputString); * * // 发送聊天消息 * if ( GUI.Button( new Rect( Screen.width*0.5f-100,Screen.height*0.65f,200,30),"发送消息") ) * { * // 向服务器发送聊天消息 * SendChat(); * } * } */ // 发送聊天消息 void SendChat() { // 聊天数据包 Chat.ChatProto proto = new Chat.ChatProto(); proto.userName = "******"; proto.chatMsg = inputText.text; NetPacket p = new NetPacket(); p.BeginWrite("chat"); p.WriteObject <Chat.ChatProto>(proto); p.EncodeHeader(); clientPeer.Send(p); //清空输入框 inputText.text = ""; }
public void SendMessage(string msg) { Chat.ChatProto proto = new Chat.ChatProto(); proto.userName = "******"; proto.chatMsg = msg; NetPacket p = new NetPacket(); p.BeginWrite("chat"); p.WriteObject <Chat.ChatProto>(proto); p.EncodeHeader(); foreach (Socket sk in peerList) { server.Send(sk, p); } }
// 处理聊天消息 public void OnChat(NetPacket packet) { Debug.Log("收到服务器的消息"); Chat.ChatProto proto = packet.ReadObject <Chat.ChatProto>(); revTxt.text = proto.userName + ":" + proto.chatMsg; // 显示收到的消息 }
//display received message public void OnChat(NetPacket packet) { Debug.Log("message received"); Chat.ChatProto proto = packet.ReadObject <Chat.ChatProto>(); recvString = proto.userName + ": " + proto.chatMsg; }