示例#1
0
        IEnumerator RestartBroadcasting()
        {
            yield return(null);

            NetworkTransport.StartBroadcastDiscovery(host, port, 1, 1, 1, new byte[1], 1, 2000, out error);
            PhoneServer.TestError(error);
        }
示例#2
0
        // Update is called once per frame
        private void Update()
        {
            int    rConn;
            int    rChan;
            int    rSize;
            Server server;

            var rec = NetworkTransport.ReceiveFromHost(host, out rConn, out rChan, data, data.Length, out rSize,
                                                       out error);

            PhoneServer.TestError(error);
            ms.Position = 0;

            switch (rec)
            {
            case NetworkEventType.DataEvent:
                if (rChan == reliable)
                {
                    server = servers.FirstOrDefault(s => s.connection == rConn);
                    if (server == null)
                    {
                        break;
                    }
                    var key = reader.ReadString();
                    switch (key)
                    {
                    case "name":
                        server.name = reader.ReadString();
                        break;

                    case "vibrate":
                        server.vibrate = reader.ReadSingle();
                        break;
                    }
                }
                break;

            case NetworkEventType.ConnectEvent:
                server      = new Server(rConn);
                server.send = !isConnected && string.IsNullOrEmpty(filter);
                servers.Add(server);
                UpdateConnected();
                break;

            case NetworkEventType.DisconnectEvent:
                server = servers.FirstOrDefault(s => s.connection == rConn);
                servers.Remove(server);
                UpdateConnected();
                break;

            case NetworkEventType.Nothing:
                break;

            case NetworkEventType.BroadcastEvent:
                break;
            }

#if UNITY_ANDROID || UNITY_IOS
            if (servers.Count > 0 && servers.Max(s => s.send ? s.vibrate : 0) > 0.5f)
            {
                Handheld.Vibrate();
            }
#endif

            if (Time.time > timer && isConnected)
            {
                timer       = Time.time + 1f / frequency;
                ms.Position = 0;
                writer.Write(number);
                WriteStatus();
                for (int i = 0; i < servers.Count; i++)
                {
                    if (servers[i].send)
                    {
                        NetworkTransport.Send(host, servers[i].connection, state, data, (int)ms.Position, out error);
                        PhoneServer.TestError(error);
                    }
                }
            }

            if (text != null)
            {
                string str = "Gyro: " + Input.gyro.attitude +
                             "\nCompas: "******"F1") +
                             "\nAcc: " + Input.acceleration +
                             "\nSending #" + number + " with " + frequency + "Hz to:";
                for (int i = 0; i < servers.Count; i++)
                {
                    if (servers[i].send)
                    {
                        str += "\n" + servers[i].name;
                    }
                }
                text.text = str;
            }

            if (Input.GetKeyDown(KeyCode.Escape))
            {
                Application.Quit();
            }
        }