public static void Enviar_Mensagem_Network(string Code, string Message) { SendStructure sendStructure = new SendStructure(Code, Message); MemoryStream memStream = new MemoryStream(); IFormatter formatter = new BinaryFormatter(); formatter.Serialize(memStream, sendStructure); //Array data to send over network byte[] serializedStructure = memStream.ToArray(); //m_Writer.SeekZero(); //m_Writer.Write(m_NetworkMessage); byte error; for (int i = 0; i < m_ConnectionIds.Count; ++i) { NetworkTransport.Send(m_HostId, m_ConnectionIds[i], 0, serializedStructure, serializedStructure.Length, out error); if ((NetworkError)error != NetworkError.Ok) { Debug.Log("Failed to send message"); } } }
void OnGUI() { if (string.IsNullOrEmpty(Application.cloudProjectId)) { GUILayout.Label("You must set up the project first. See the Multiplayer tab in the Service Window"); } else { GUILayout.Label("Cloud Project ID: " + Application.cloudProjectId); } if (m_MatchJoined) { GUILayout.Label("Match joined '" + m_MatchName + "' on Matchmaker server"); } else if (m_MatchCreated) { GUILayout.Label("Match '" + m_MatchName + "' created on Matchmaker server"); } GUILayout.Label("Connection Established: " + m_ConnectionEstablished); if (m_MatchCreated || m_MatchJoined) { GUILayout.Label("Relay Server: " + m_MatchInfo.address + ":" + m_MatchInfo.port); GUILayout.Label("NetworkID: " + m_MatchInfo.networkId + " NodeID: " + m_MatchInfo.nodeId); GUILayout.BeginHorizontal(); GUILayout.Label("Outgoing message:"); m_NetworkMessage = GUILayout.TextField(m_NetworkMessage); GUILayout.EndHorizontal(); GUILayout.Label("Last incoming message: " + m_LastReceivedMessage); if (m_ConnectionEstablished && GUILayout.Button("Send message")) { SendStructure sendStructure = new SendStructure("enviar_mensagem", "Conteudo da Mensagem"); MemoryStream memStream = new MemoryStream(); IFormatter formatter = new BinaryFormatter(); formatter.Serialize(memStream, sendStructure); //Array data to send over network byte[] serializedStructure = memStream.ToArray(); //m_Writer.SeekZero(); //m_Writer.Write(m_NetworkMessage); byte error; for (int i = 0; i < m_ConnectionIds.Count; ++i) { NetworkTransport.Send(m_HostId, m_ConnectionIds[i], 0, serializedStructure, serializedStructure.Length, out error); if ((NetworkError)error != NetworkError.Ok) { Debug.LogError("Failed to send message: " + (NetworkError)error); } } } if (GUILayout.Button("Shutdown")) { m_NetworkMatch.DropConnection(m_MatchInfo.networkId, m_MatchInfo.nodeId, 0, OnConnectionDropped); } } else { if (GUILayout.Button("Create Room")) { m_NetworkMatch.CreateMatch(m_MatchName, 4, true, "", "", "", 0, 0, OnMatchCreate); } if (GUILayout.Button("Join first found match")) { m_NetworkMatch.ListMatches(0, 1, "", true, 0, 0, (success, info, matches) => { if (success && matches.Count > 0) { m_NetworkMatch.JoinMatch(matches[0].networkId, "", "", "", 0, 0, OnMatchJoined); } }); } if (GUILayout.Button("List rooms")) { m_NetworkMatch.ListMatches(0, 20, "", true, 0, 0, OnMatchList); } if (m_MatchList.Count > 0) { GUILayout.Label("Current rooms:"); } foreach (var match in m_MatchList) { if (GUILayout.Button(match.name)) { m_NetworkMatch.JoinMatch(match.networkId, "", "", "", 0, 0, OnMatchJoined); } } } }
void Update() { if (m_HostId == -1) { return; } var networkEvent = NetworkEventType.Nothing; int connectionId; int channelId; int receivedSize; byte error; // Get events from the relay connection networkEvent = NetworkTransport.ReceiveRelayEventFromHost(m_HostId, out error); if (networkEvent == NetworkEventType.ConnectEvent) { Debug.Log("Relay server connected"); } if (networkEvent == NetworkEventType.DisconnectEvent) { Debug.Log("Relay server disconnected"); } do { // Get events from the server/client game connection networkEvent = NetworkTransport.ReceiveFromHost(m_HostId, out connectionId, out channelId, m_ReceiveBuffer, (int)m_ReceiveBuffer.Length, out receivedSize, out error); if ((NetworkError)error != NetworkError.Ok) { Debug.LogError("Error while receiveing network message: " + (NetworkError)error); } switch (networkEvent) { case NetworkEventType.ConnectEvent: { Debug.Log("Connected through relay, ConnectionID:" + connectionId + " ChannelID:" + channelId); m_ConnectionEstablished = true; m_ConnectionIds.Add(connectionId); break; } case NetworkEventType.DataEvent: { Debug.Log("Data event, ConnectionID:" + connectionId + " ChannelID: " + channelId + " Received Size: " + receivedSize); m_Reader = new NetworkReader(m_ReceiveBuffer); //De-Serialize recevied byte array into memory MemoryStream stream = new MemoryStream(m_ReceiveBuffer, 0, receivedSize); IFormatter formatter = new BinaryFormatter(); stream.Seek(0, SeekOrigin.Begin); //Received data now stored as SendStructure SendStructure receivedStructure = formatter.Deserialize(stream) as SendStructure; //m_LastReceivedMessage = m_Reader.ReadString(); m_LastReceivedMessage = receivedStructure.Message + " " + receivedStructure.Code; break; } case NetworkEventType.DisconnectEvent: { Debug.Log("Connection disconnected, ConnectionID:" + connectionId); break; } case NetworkEventType.Nothing: break; } } while (networkEvent != NetworkEventType.Nothing); }
void Receber_Inf() { if (m_HostId == -1) { return; } var networkEvent = NetworkEventType.Nothing; int connectionId; int channelId; int receivedSize; byte error; // Get events from the relay connection networkEvent = NetworkTransport.ReceiveRelayEventFromHost(m_HostId, out error); if (networkEvent == NetworkEventType.ConnectEvent) { Debug.Log("Relay server connected"); } if (networkEvent == NetworkEventType.DisconnectEvent) { Debug.Log("Relay server disconnected"); } do { // Get events from the server/client game connection networkEvent = NetworkTransport.ReceiveFromHost(m_HostId, out connectionId, out channelId, m_ReceiveBuffer, (int)m_ReceiveBuffer.Length, out receivedSize, out error); if ((NetworkError)error != NetworkError.Ok) { Debug.Log("Error while receiveing network message"); } switch (networkEvent) { case NetworkEventType.ConnectEvent: { Debug.Log("Connected through relay, ConnectionID:" + connectionId + " ChannelID:" + channelId); SceneManager.LoadScene("game"); m_ConnectionEstablished = true; m_ConnectionIds.Add(connectionId); break; } case NetworkEventType.DataEvent: { Debug.Log("Data event, ConnectionID:" + connectionId + " ChannelID: " + channelId + " Received Size: " + receivedSize); //m_Reader = new NetworkReader(m_ReceiveBuffer); //m_LastReceivedMessage = m_Reader.ReadString(); //De-Serialize recevied byte array into memory MemoryStream stream = new MemoryStream(m_ReceiveBuffer, 0, receivedSize); IFormatter formatter = new BinaryFormatter(); stream.Seek(0, SeekOrigin.Begin); //Received data now stored as SendStructure SendStructure receivedStructure = formatter.Deserialize(stream) as SendStructure; if (receivedStructure.Code == "enviar_mensagem_chat") { GameObject.Find("fala_2_text").GetComponent <Text> ().text = receivedStructure.Message.ToString().Replace("\"", ""); } if (receivedStructure.Code == "escrevendo_mensagem_chat") { inicio.Efeito_Escrevendo_Start(); } if (receivedStructure.Code == "destribuir_pedras_oponente") { inicio.Destribuir_Pedras_Oponente(receivedStructure.Message); } if (receivedStructure.Code == "enviar_retirar_opositor_cova_dados") { inicio.Enviar_Retirar_Opositor_Cova_Dados(receivedStructure.Message); } if (receivedStructure.Code == "partilhar_nome") { GameObject.Find("Jogador_2_Nome").GetComponent <Text> ().text = receivedStructure.Message; } if (receivedStructure.Code == "oponente_saiu") { GameObject Quem_Somos = Instantiate(Resources.Load("Oponente_Saiu")) as GameObject;; Quem_Somos.transform.SetParent(GameObject.Find("Canvas").transform); Quem_Somos.transform.localPosition = Vector3.zero; Quem_Somos.transform.localScale = Vector3.one; } break; } case NetworkEventType.DisconnectEvent: { Debug.Log("Connection disconnected, ConnectionID:" + connectionId); break; } case NetworkEventType.Nothing: break; } } while (networkEvent != NetworkEventType.Nothing); }