void OnMultiPeerHostReceiveMessage(string param) { if (MultiplayerManager.instance.Hosting) { // cache host if (hostId == "") { string localId = MultiPeer.getLocalPeerId(); SendMessageToAll("SetHost", localId); hostId = localId; } NetworkMessage message = StringToMessage(param); // this is very much a hack // the host needs to know what type of message it's receiving (ftr the type is "HostAddMessage") // but it also needs to know the name of the message to trigger // so i'm combining the type and name in the first parameter, then splitting the string // upon the message being received down here if (message.name.Substring(0, 14) == "HostAddMessage") { NetworkMessage newMessage = new NetworkMessage(message.name.Substring(14), message.message1, message.message2, message.val); AddMessage(newMessage); } else if (message.name == "ConfirmClientReceived") { HostReceiveConfirmation(message.message1); } else { Events.instance.Raise(new HostReceiveMessageEvent(message.name, message.message1, message.message2)); } } }
void OnMultiPeerDeciderReceiveMessage(string param) { if (Player.instance.IsDecider) { // cache decider if (deciderId == "") { string localId = MultiPeer.getLocalPeerId(); SendMessageToAll("SetDecider", localId); deciderId = localId; } NetworkMessage message = StringToMessage(param); Events.instance.Raise(new DeciderReceiveMessageEvent(message.name, message.message1, message.message2)); } }
void OnGUI() { beginColumn(); GUILayout.Label("First we need to advertise ourself"); if (GUILayout.Button("Advertise Device")) { MultiPeer.advertiseCurrentDevice(true, "prime31-MyGame"); } if (GUILayout.Button("Advertise Device (no UI)")) { MultiPeer.advertiseCurrentDeviceWithNearbyServiceAdvertiser(true, "prime31-MyGame"); } GUILayout.Label("Then we can use Apple's built in UI"); if (GUILayout.Button("Show Peer Picker")) { MultiPeer.showPeerPicker(); } GUILayout.Label("Or no UI at all"); if (GUILayout.Button("Start Service Browser")) { MultiPeer.startNearbyServiceBrowser(); } if (GUILayout.Button("Stop Service Browser")) { MultiPeer.stopNearbyServiceBrowser(); } GUILayout.Label("Once connected, we can do more"); if (GUILayout.Button("Get Connected Peers")) { var peers = MultiPeer.getConnectedPeers(); Utils.logObject(peers); } if (GUILayout.Button("Get Local PeerID")) { Debug.Log("local peerID: " + MultiPeer.getLocalPeerId()); } if (GUILayout.Button("Disconnect and End Session")) { MultiPeer.disconnectAndEndSession(); } endColumn(true); GUILayout.Label("Here are the different ways to send data"); if (GUILayout.Button("Send Time to All Peers")) { var result = MultiPeer.sendMessageToAllPeers("ui", "multiPeerMessageReceiver", Time.timeSinceLevelLoad.ToString()); Debug.Log("send result: " + result); } if (GUILayout.Button("Send Time to First Peer")) { var peers = MultiPeer.getConnectedPeers(); if (peers.Count == 0) { Debug.Log("aborting send since there are no connected peers"); return; } var result = MultiPeer.sendMessageToPeers(new string[] { peers[0] }, "ui", "multiPeerMessageReceiver", Time.timeSinceLevelLoad.ToString()); Debug.Log("send result: " + result); } if (GUILayout.Button("Send Raw Message to All Peers")) { // we will just send some text across the wire encoded into a byte array for demonstration purposes var theStr = "im a string sent by MultiPeer magic"; var bytes = System.Text.UTF8Encoding.UTF8.GetBytes(theStr); var result = MultiPeer.sendRawMessageToAllPeers(bytes); Debug.Log("send result: " + result); } if (GUILayout.Button("Send Raw Message to First Peers")) { var peers = MultiPeer.getConnectedPeers(); if (peers.Count == 0) { Debug.Log("aborting send since there are no connected peers"); return; } // we will just send some text across the wire encoded into a byte array for demonstration purposes var theStr = "im a string sent by MultiPeer magic"; var bytes = System.Text.UTF8Encoding.UTF8.GetBytes(theStr); var result = MultiPeer.sendRawMessageToPeers(new string[] { peers[0] }, bytes); Debug.Log("send result: " + result); } endColumn(false); }