示例#1
0
文件: Client.cs 项目: xxx-lgm/SFMF
        /**
         * Connect to a given server or start a server/client host
         * <param name="ip">The IP of the server to connect to</param>
         * <param name="port">The port of the server to connect to</param>
         **/
        public Client Connect(string ip = null, int port = 4444)
        {
            if (!Connected && !ConnectionMidState)
            {
                ConnectionMidState = true;

                if (Server != null)
                {
                    Server.Start(port);
                    nClient = ClientScene.ConnectLocalServer();
                }
                else
                {
                    nClient = new NetworkClient();
                    nClient.Connect(ip, port);
                }

                nClient.RegisterHandler(MsgType.Error, OnError);
                nClient.RegisterHandler(PacketType.PlayerConnected, OnPlayerConnected);
                nClient.RegisterHandler(PacketType.PlayerDisconnected, OnPlayerDisconnected);
                nClient.RegisterHandler(PacketType.PlayerData, OnPlayerData);
                nClient.RegisterHandler(PacketType.ClientConnect, OnClientConnect);
                nClient.RegisterHandler(PacketType.ClientDisconnect, OnClientDisconnect);
                nClient.RegisterHandler(PacketType.ServerShutdown, OnServerShutdown);
            }

            return(this);
        }
示例#2
0
 // Create a local client and connect to the local server
 public void SetupLocalClient()
 {
     myClient = ClientScene.ConnectLocalServer();
     myClient.RegisterHandler(MsgType.Connect, OnConnected);
     myClient.RegisterHandler(MyMsgType.test, OnMessage);
     isAtStartup = false;
 }
 public void SetupServer(bool launchClient = false)
 {
     connectionType = launchClient ? ConnectionType.Both : ConnectionType.Server;
     AddPacketActions();
     NetworkServer.Listen(8080);
     myClient = ClientScene.ConnectLocalServer();
 }
示例#4
0
    // Update is called once per frame
    void Update()
    {
        if (isAtStartUp)
        {
            if (Input.GetKeyDown(KeyCode.S))
            {
                NetworkServer.Listen(4444);
                isAtStartUp = false;

                text.text = "As Server";
            }
            else if (Input.GetKeyDown(KeyCode.C))
            {
                myClient = new NetworkClient();
                myClient.RegisterHandler(MsgType.Connect, OnConnected);
                myClient.Connect("127.0.0.1", 4444);
                isAtStartUp = false;

                text.text = "As Client";
            }
            else if (Input.GetKeyDown(KeyCode.B))
            {
                NetworkServer.Listen(4444);

                myClient = ClientScene.ConnectLocalServer();
                myClient.RegisterHandler(MsgType.Connect, OnConnected);
                isAtStartUp = false;

                text.text = "As Server & Client";
            }
        }
    }
示例#5
0
 private void HostLocal()
 {
     gameObject.AddComponent <Server>();
     Client = ClientScene.ConnectLocalServer();
     Client.Register(false);
     IsStartUp = false;
 }
示例#6
0
 // Create a local client and connect to the local server
 public void SetupLocalClient()
 {
     myClient = ClientScene.ConnectLocalServer();
     InitClient();
     videoChat.remoteView.GetComponent <CameraController>().StartLocalStream();
     Debug.Log("Started local client");
 }
示例#7
0
    public bool StartUNETServer()
    {
        if (SteamNetworkManager.Instance.LobbyConnectionState != SteamNetworkManager.SessionConnectionState.CONNECTED)
        {
            Debug.LogError("Not connected to lobby");
            return(false);
        }

        Debug.Log("Starting UNET server");

        // Listen for player spawn request messages
        NetworkServer.RegisterHandler(NetworkMessages.SpawnRequestMsg, OnSpawnRequested);

        // Start UNET server
        NetworkServer.Configure(SteamNetworkManager.hostTopology);
        NetworkServer.dontListen = true;
        NetworkServer.Listen(0);

        NetworkServer.SpawnObjects();

        // Create a local client-to-server connection to the "server"
        // Connect to localhost to trick UNET's ConnectState state to "Connected", which allows data to pass through TransportSend
        myClient = ClientScene.ConnectLocalServer();
        myClient.Configure(SteamNetworkManager.hostTopology);
        myClient.Connect("localhost", 0);
        myClient.connection.ForceInitialize();

        // Add local client to server's list of connections
        // Here we get the connection from the NetworkServer because it represents the server-to-client connection
        var serverToClientConn = NetworkServer.connections[0];

        AddConnection(serverToClientConn);

        // register networked prefabs
        SteamNetworkManager.Instance.RegisterNetworkPrefabs();

        // Spawn self
        ClientScene.Ready(serverToClientConn);

        // Hack trick to find and respawn NetworkIdentity objects to enable rpc
        NetworkIdentity[] objectsToSpawn = GameObject.FindObjectsOfType <NetworkIdentity>();
        foreach (NetworkIdentity uv in objectsToSpawn)
        {
            NetworkServer.UnSpawn(uv.gameObject);
            NetworkServer.Spawn(uv.gameObject);
        }

        NetworkServer.SetClientReady(serverToClientConn);

        if (inviteFriendOnStart)
        {
            // prompt to invite friend
            SteamNetworkManager.Instance.StartCoroutine(DoShowInviteDialogWhenReady());
        }

        return(true);
    }
示例#8
0
 public void SetUpLocalDummyClient()
 {
     myClient = ClientScene.ConnectLocalServer();
     ClientScene.ClearSpawners();
     foreach (GameObject spawnable in spawns)
     {
         ClientScene.RegisterPrefab(spawnable);
     }
 }
示例#9
0
 // Create a local client and connect to the local server
 public void SetupLocalClient()
 {
     myClient = ClientScene.ConnectLocalServer();
     myClient.RegisterHandler(MsgType.Connect, OnConnected);
     myClient.RegisterHandler(sendMoveMessageID, RecieveMove);
     myClient.RegisterHandler(sendSeedID, ClientAcceptsSeed);
     myClient.RegisterHandler(sendCharSheet, ServerAcceptsCharSheet);
     isAtStartup = false;
 }
    void setupLocalClient()
    {
        //registerPrefabs ();
        //Spawn ();

        myClient = ClientScene.ConnectLocalServer();
        myClient.RegisterHandler(MsgType.Connect, OnConnectedLocalPlayer);
        myClient.RegisterHandler(MsgType.Error, OnErrorLocalPlayer);
    }
示例#11
0
        public void ConnectLocalHost()
        {
            if (!NetworkServer.active)
            {
                Debug.LogError("Start server first");
                return;
            }

            _client = ClientScene.ConnectLocalServer();
        }
示例#12
0
    public void SetupLocalClient()
    {
        myClient = new NetworkClient();
        SceneManager.LoadScene("Scenes/Main/Main");
        myClient.Connect("127.0.0.1", 7777);
        myClient = ClientScene.ConnectLocalServer();

        myClient.RegisterHandler(MsgType.Connect, OnConnected);
        isAtStartup = false;
    }
示例#13
0
    // Create a local client and connect to the local server
    public void SetupLocalClient()
    {
        myClient = ClientScene.ConnectLocalServer();
        myClient.RegisterHandler(MsgType.Connect, OnConnected);
        isAtStartup = false;
        netManager.StartClient();
        GameObject tempCam = GameObject.FindWithTag("tmp");

        GameObject.Destroy(tempCam);
    }
 public void OnServerMatchJoined(JoinMatchResponse matchJoin)
 {
     if (matchJoin.success)
     {
         _clientConnection = ClientScene.ConnectLocalServer();
     }
     else
     {
         Debug.LogError("Join match failed");
     }
 }
示例#15
0
    public void SetUpLocalClient()
    {
        myClient = ClientScene.ConnectLocalServer();
        myClient.RegisterHandler(MsgType.Connect, OnConnected);
        //ClientScene.ClearSpawners();
        //foreach (GameObject spawnable in spawns) {
        //    ClientScene.RegisterPrefab(spawnable);
        //}

        isAtStartup = false;
    }
示例#16
0
    //啟動Server [/UI/StartMenu/HostBtn]
    public void StartHost()
    {
        if (NetworkServer.Listen(listen_port))
        {
            client = ClientScene.ConnectLocalServer();

            GetComponent <ChatroomMessenger>().RegisterMsgHandler();

            StartMenu.SetActive(false);
            Chatroom.SetActive(true);
        }
    }
 public void host(int port)
 {
     CleanUpClient();
     CleanUpHost();
     NetworkServer.Listen(port);
     NetworkServer.SpawnObjects();
     Debug.Log("Hosting " + port);
     server.init();
     myClient = ClientScene.ConnectLocalServer();
     myClient.RegisterHandler(MsgType.Connect, OnClientConnect);
     myClient.RegisterHandler(MsgType.Disconnect, OnClientDisconnect);
 }
示例#18
0
    void SetupServer()
    {
        NetworkServer.Listen(this.broadcastPort + 3);
        NetworkServer.RegisterHandler(MyNetworkMessageClass.CREATE_GAME, blastToAll);
        NetworkServer.RegisterHandler(MyNetworkMessageClass.WAIT_FOR_READY, blastToAll);
        NetworkServer.RegisterHandler(MyNetworkMessageClass.CONFIRM_READY, blastToAll);
        NetworkServer.RegisterHandler(MyNetworkMessageClass.FINISH_GAME, blastToAll);
        NetworkServer.RegisterHandler(MyNetworkMessageClass.BACK_TO_IDLE, blastToAll);

        myClient = ClientScene.ConnectLocalServer();
        RegisterAllClientEvent();

        Debug.Log("Server started at port " + (broadcastPort + 3));
    }
示例#19
0
 private void ClientDisconnect()
 {
     if (numClientConnects == 2)
     {
         myClient.Disconnect();
         localClient = ClientScene.ConnectLocalServer();
         localClient.RegisterHandler(MsgType.Connect, OnClient1Connected);
         localClient.RegisterHandler(MsgType.Disconnect, OnClient1Disconnected);
     }
     else //for numClientConnects == 3
     {
         localClient.Disconnect();
     }
 }
示例#20
0
        // CONSTRUCTOR
        public Client(NetworkingInfo netinfo)
        {
            networkingInfo = netinfo;
            if (networkingInfo.port == -1)
            {
                networkingInfo.port = NetworkingInfo.defaultPort;
            }
            if (networkingInfo.address == "")
            {
                networkingInfo.address = NetworkingInfo.defaultAddress;
            }
            // TODO: Add some kind of DNS pre check for IP?

            connectedPlayers = new List <Player>();

            config              = new ConnectionConfig();
            Channels.priority   = config.AddChannel(QosType.AllCostDelivery);
            Channels.reliable   = config.AddChannel(QosType.ReliableSequenced);
            Channels.unreliable = config.AddChannel(QosType.UnreliableSequenced);
            Channels.fragmented = config.AddChannel(QosType.ReliableFragmented);
            Channels.update     = config.AddChannel(QosType.StateUpdate);
            hostconfig          = new HostTopology(config, maxConcurrentConnectedUsers);

            if (networkingInfo.isServer)
            {
                myClient = ClientScene.ConnectLocalServer();
                myClient.Configure(hostconfig);

                myClient.RegisterHandler(MsgType.Connect, OnConnected);
                myClient.RegisterHandler(MsgType.Disconnect, ConnectionFailed);
                registerAllCallbacks(myClient, cbClientHandler);
            }
            else
            {
                myClient = new NetworkClient();
                myClient.Configure(hostconfig);

                myClient.RegisterHandler(MsgType.Connect, OnConnected);
                myClient.RegisterHandler(MsgType.Error, ConnectionError);
                myClient.RegisterHandler(MsgType.Disconnect, ConnectionFailed);
                registerAllCallbacks(myClient, cbClientHandler);

                common.printConsole(tag, "Setup complete", true);

                myClient.Connect(networkingInfo.address, networkingInfo.port);
                common.printConsole(tag, "Connecting to " + networkingInfo.address + ":" + networkingInfo.port, true);
            }
        }
    // host服务器按钮回调
    // 开启主机
    public void InitHost()
    {
        // 开启监听
        NetworkServer.Listen(ButtonCallbackMethodData._instant.GetPort());
        // 添加服务器监听到客服端的回调
        NetworkServer.RegisterHandler(MsgType.Connect, OnServerListenClient);
        // 添加服务器监听到客户端创建玩家的回调
        NetworkServer.RegisterHandler(MsgType.AddPlayer, OnServerListenAddPlayer);
        // 添加服务器监听到客户端断开连接的回调
        NetworkServer.RegisterHandler(MsgType.Disconnect, OnServerListenClientDisConnect);

        // 创建客服端 连接本地 从而形成主机
        client = ClientScene.ConnectLocalServer();
        // 客服端绑定回调
        ClientBindFunction();
    }
    public void OnClientConnected(NetworkMessage netMsg)
    {
        Assert.IsTrue(remoteClient.isConnected);

        if (localClient == null)
        {
            localClient = ClientScene.ConnectLocalServer();
            Assert.IsTrue(localClient.isConnected);

            remoteClient.Disconnect();
            localClient.Disconnect();

            Assert.IsFalse(remoteClient.isConnected);
            Assert.IsFalse(localClient.isConnected);

            isTestDone = true;
        }
    }
示例#23
0
    public void SetupServer()
    {
        packetActions = new Dictionary <PacketIDs, Action <PacketBase> >();
        AddPacketActions();

        ConnectionConfig config = new ConnectionConfig();

        config.AddChannel(QosType.ReliableSequenced);
        config.AddChannel(QosType.Unreliable);
        NetworkServer.Configure(config, maxPlayers);
        NetworkServer.Listen(8080);

        myClient = ClientScene.ConnectLocalServer();

        _disconnect.SetActive(true);
        _hostButton.SetActive(false);

        ServerLogic.instance.SetAllToDisconected();
    }
示例#24
0
    public IEnumerator ConnectLocalClientWorksTest()
    {
        NetworkClient.ShutdownAll();
        NetworkServer.Reset();

        NetworkServer.RegisterHandler(MsgType.Connect, OnServerConnected);
        NetworkServer.RegisterHandler(MsgType.AddPlayer, OnAddPlayer);
        NetworkServer.RegisterHandler(kMsgTest, OnServerTestMsg);

        NetworkServer.Listen(9999);
        NetworkClient client = ClientScene.ConnectLocalServer();

        client.RegisterHandler(MsgType.Connect, OnClientConnected);
        client.RegisterHandler(MsgType.Disconnect, OnClientDisconnected);
        client.RegisterHandler(kMsgTest, OnClientTestMsg);
        client.RegisterHandler(kMsgTest2, OnClientTestMsg2);

        while (!isTestDone)
        {
            yield return(null);
        }
    }