示例#1
0
        public Connect(string ip, int port, NetworkProtocol protocol)
        {
            IPAddress address;

            if (!IPAddress.TryParse(ip, out address))
            {
                throw new InvalidCastException("IP 格式錯誤,請輸入正確的 IPV4 IP ex : 35.123.123.123");
            }
            string ServerIP = address.ToString() + ":" + port;

            NetworkProtocol = protocol;
            gameService     = new GameNetworkService(NetworkProtocol);

            //websocket 的地址,對應到伺服器 websocketsharp 開出的地址
            if (NetworkProtocol == NetworkProtocol.WebSocket)
            {
                gameService.Address = string.Format("ws://{0}/WebSocket", ServerIP);
            }
            else
            {
                gameService.Address = ServerIP;
            }
            _system = new _System();
            AddCallBackHandler(255, _system);
        }
示例#2
0
        public Connect(string ip, int port, NetworkProtocol protocol)
        {
            IPAddress address  = tryParseIP(ip);
            string    ServerIP = address.ToString() + ":" + port;

            NetworkProtocol = protocol;
            gameService     = new GameNetworkService(NetworkProtocol);

            //websocket 的地址,對應到伺服器 websocketsharp 開出的地址
            if (NetworkProtocol == NetworkProtocol.WebSocket)
            {
                gameService.Address = string.Format("ws://{0}/WebSocket", ServerIP);
            }
            else
            {
                gameService.Address = ServerIP;
            }

            IPAddress tryParseIP(string checkTarget)
            {
                try
                {
                    IPAddress result;
                    if (!IPAddress.TryParse(checkTarget, out result))
                    {
                        IPAddress[] addressArray = Dns.GetHostAddresses(ip);
                        result = addressArray.First();
                    }
                    if (result == null || result.AddressFamily != AddressFamily.InterNetwork)
                    {
                        throw new InvalidCastException("IP 格式錯誤,請輸入正確的 IPV4 IP ex : 35.123.123.123");
                    }
                    else
                    {
                        return(result);
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }