Пример #1
0
    void Awake()
    {
        instance = gameObject.GetComponent <NetworkController>();

        // facebook
        facebook = GameObject.Find("Play Controller").GetComponent <FacebookConnector>();
        facebook.EventCallback += new SnEventHandler(OnFacebookEvent);
        facebook.Init();

        // funapi network
        network = new FunapiNetwork(false);
        network.OnSessionInitiated += new FunapiNetwork.SessionInitHandler(OnSessionInitiated);
        network.OnSessionClosed    += new FunapiNetwork.SessionCloseHandler(OnSessionClosed);

        FunapiTcpTransport transport = new FunapiTcpTransport(kServerIp, 8022, FunEncoding.kProtobuf);

        transport.AutoReconnect = true;
        network.AttachTransport(transport);

        network.RegisterHandler("sc_spawn_self", this.OnSpawnSelf);
        network.RegisterHandler("sc_spawn_other", this.OnSpawnOther);
        network.RegisterHandler("sc_update", this.OnUpdate);
        network.RegisterHandler("sc_killed", this.OnKilled);
        network.RegisterHandler("sc_face", this.OnFaceUpdate);
    }
Пример #2
0
    private void Connect(TransportProtocol protocol)
    {
        DebugUtils.Log("-------- Connect --------\n{0}", DateTime.Now);

        if (network_ == null || !network_.SessionReliability)
        {
            network_ = new FunapiNetwork(with_session_reliability_);
            network_.ResponseTimeout = 10f;

            network_.OnSessionInitiated             += new FunapiNetwork.SessionInitHandler(OnSessionInitiated);
            network_.OnSessionClosed                += new FunapiNetwork.SessionCloseHandler(OnSessionClosed);
            network_.MaintenanceCallback            += new FunapiNetwork.MessageEventHandler(OnMaintenanceMessage);
            network_.StoppedAllTransportCallback    += new FunapiNetwork.NotifyHandler(OnStoppedAllTransport);
            network_.TransportConnectFailedCallback += new TransportEventHandler(OnTransportConnectFailed);
            network_.TransportDisconnectedCallback  += new TransportEventHandler(OnTransportDisconnected);

            network_.RegisterHandler("echo", this.OnEcho);
            network_.RegisterHandler("pbuf_echo", this.OnEchoWithProtobuf);

            //network_.SetMessageProtocol(TransportProtocol.kTcp, "echo");
            //network_.SetMessageProtocol(TransportProtocol.kUdp, "pbuf_echo");

            FunapiTransport transport = GetNewTransport(protocol);
            network_.AttachTransport(transport);
        }
        else
        {
            if (!network_.HasTransport(protocol))
            {
                FunapiTransport transport = GetNewTransport(protocol);
                network_.AttachTransport(transport);
            }

            network_.SetDefaultProtocol(protocol);
        }

        network_.Start();
    }