コード例 #1
0
        public void Disconnect()
        {
            if(tcpServer != null)
            {
                tcpServer.Stop();
                tcpServer = null;
            }
            if(tcpClient != null)
            {
                tcpClient.Stop();
                tcpClient = null;
            }

            Epigene.GAME.GameManager.Instance.Event(
                "NETWORKLAYER",
                "connected",
                "false");
        }
コード例 #2
0
        /// <summary>
        /// Connect to server to able to send message to server
        /// and create listener running in background for receiving messages.
        /// The protocol will define what type of network mode you want to use: TCP/HTTP/..
        /// Return true if connected or false if not
        /// </summary>
        public bool Connect(string myIP, 
		                    string gameServerIP, 
		                    int serverPort, 
		                    int listenerPort, 
		                    Protocol type, 
		                    ProcessServerFunction func)
        {
            //TODO in case of active conection Connected == true:
            //should we release actual connection or return with error?

            if(Connected)
            {
                //we are already connected, don't brake it
                return true;
            }

            this.myIP = myIP;
            this.gameServerIP = gameServerIP;
            this.serverPort = serverPort;
            this.listenerPort = listenerPort;
            this.type = type;
            this.processServerMessage = func;

            switch (this.type)
            {
                case Protocol.TCP:
                    Debug.Log("Starting TCP client connection");

                    if(tcpClient == null)
                    {
                        tcpClient = new TCPClient(this.gameServerIP, this.serverPort);
                        tcpClient.Start();

                    }

                    //create only once
                    if(tcpServer == null)
                    {
                        Debug.Log("Starting TCP server at "+this.myIP+":"+this.listenerPort);
                        tcpServer = new TCPServer(this.myIP, this.listenerPort, func);
                        tcpServer.Start();
                    }
                    break;

                case Protocol.HTTP:
                    http = new HTTP("http://192.168.1.104:2000");
                    break;

                // case Protocol.UDP:
                // 	break;

                default:
                    Debug.LogWarning("Not implemented yet!!");
                    break;
            }

            if (tcpClient != null)
            {
                WebPlayerDebugManager.addOutput("Connected!", 4);
                Epigene.GAME.GameManager.Instance.Event(
                    "NETWORKLAYER",
                    "connected",
                    "true");
            }

            return tcpClient != null;
        }