빠른 시작: 네트워크 연결 정보 검색(JavaScript 및 HTML을 사용하는 Windows 스토어 앱)

이 항목에서는 장치에서 네트워크 연결에 대한 연결 세부 정보 및 사용 정보를 검색하는 방법을 보여 줍니다.

사전 요구 사항

다음 예는 JavaScript를 사용하며 네트워크 정보 샘플을 기반으로 합니다. 첫 번째 앱을 만드는 자세한 내용은 JavaScript를 사용하여 첫 번째 Windows 스토어 앱 만들기를 참조하세요.

Windows 스토어 앱에서 네트워크에 대비하려면 프로젝트 Package.appxmanifest 파일에서 기능을 설정해야 합니다. 각 네트워크 기능에 대한 정의는 네트워크 격리 기능을 구성하는 방법을 참조하세요.

연결 프로필이란?

ConnectionProfile은 장치에 설정된 단일 네트워크 연결을 나타냅니다. ConnectionProfile에서 액세스하는 정보를 사용하여 현재 연결 수준을 확인하거나, 데이터 사용량을 추적하거나, 연결을 유지 관리하는 데 사용되는 네트워크 어댑터를 식별할 수 있습니다. ConnectionProfile의 속성 변경에 대한 알림을 받도록 등록하면 연결된 Windows 스토어 앱에서 네트워크 환경의 변경에 대해 동작을 적용할 때 적절하게 선택할 수 있습니다. 이러한 변경에 대한 알림을 받도록 등록하는 방법에 대한 자세한 내용은 빠른 시작: 연결 이벤트 및 가용성 변경 관리를 참조하세요.

종종 로밍되고 네트워크 요금제에서 작동하는 모바일 장치에 대한 연결된 응용 프로그램과 같이 특별한 시나리오의 경우 ConnectionProfile은 예기치 않은 통신 회사 서비스 요금을 방지하는 데 사용할 수 있는 비용 및 데이터 요금제 정보를 제공합니다. 자세한 내용은 빠른 시작: 네트워크 요금제 비용 제약 조건 관리를 참조하세요.

연결 프로필 검색

몇 가지 변수를 정의합니다. 먼저 앱이 NetworkInformation을 검색하는 데 사용하는 메서드를 정의하는 ConnectionProfile의 인스턴스를 정의하고, 그런 다음 NetworkCostType에 표시된 가능한 네트워크 비용 유형을 정의하는 ConnectionProfile의 인스턴스를 정의합니다.

var networkInfo = Windows.Networking.Connectivity.NetworkInformation;
var networkCostInfo = Windows.Networking.Connectivity.NetworkCostType;

NetworkInformation 클래스는 ConnectionProfile 검색을 위한 두 가지 메서드를 정의합니다. GetInternetConnectionProfile 메서드는 인터넷 연결과 관련된 프로필만 반환합니다.

function DisplayInternetConnectionProfileInfo() {
    try {
        var internetProfile = networkInfo.getInternetConnectionProfile();
        mySample.displayStatus(GetConnectionProfileInfo(internetProfile));
    }
    catch (e) {
        mySample.displayError("Exception Caught: " + e + "\n\r");
    }
}

GetConnectionProfiles 메서드를 호출하면 인터넷 연결을 포함하여 현재 장치에 설정된 모든 연결에 대한 프로필이 검색됩니다.

function DisplayConnectionProfileList() {
    var profileList = "";
    try {
        var ConnectionProfiles = networkInfo.getConnectionProfiles();
        if (ConnectionProfiles.length != 0) {
            for (var i = 0; i < ConnectionProfiles.length; i++) {

                profileList += GetConnectionProfileInfo(ConnectionProfiles[i]);
                profileList += "----------------------------------------------------------------\n\r";
            }
            mySample.displayStatus(profileList);
        }
        else {
            mySample.displayStatus("No profiles found");
        }
    }

    catch (e) {
        mySample.displayError("Exception Caught: " + e + "\n\r");
    }
}

연결 프로필에서 정보 액세스

ConnectionProfile에서 다음 연결 정보에 액세스할 수 있습니다.

데이터 이 데이터를 제공하는 요소 설명

연결 비용

ConnectionCost

데이터 제한 및 로밍 정보를 포함하여 연결 비용 정보에 대한 자세한 내용을 제공합니다.

비용 유형

NetworkCostType

연결에서 현재 사용하는 네트워크의 비용 유형을 나타냅니다.

데이터 요금제 상태 및 사용량

DataPlanStatus, DataPlanUsage

연결과 관련된 데이터 요금제에 대한 사용량 정보를 제공합니다.

로컬 사용량

DataUsage

로컬 연결 사용량 정보를 제공합니다.

네트워크 어댑터

NetworkAdapter

연결을 제공하는 네트워크 어댑터를 식별합니다.

 

ConnectionProfile을 전달하면 다음 예제 함수는 개체에서 정의한 메서드를 호출하여 연결 상태 및 사용량 정보를 검색합니다.

function GetConnectionProfileInfo(connectionProfile) {

    try {
        if (connectionProfile == null) {
            return "";
        }

        var returnString = "ProfileName: " + connectionProfile.profileName + "\n\r";
        returnString += "Connected: " + connectionProfile.connected + "\n\r";

        //Display Connection cost info
        returnString += "Connection Cost Information:\n\r";
        returnString += "===============\n\r";
        var connectionCost = connectionProfile.getConnectionCost();
        returnString += "Cost Type: " + GetCostType(connectionCost.networkCostType) + "\n\r";
        returnString += "Roaming: " + connectionCost.roaming + "\n\r";
        returnString += "Over Datalimit: " + connectionCost.overDataLimit + "\n\r";
        returnString += "Approaching Datalimit: " + connectionCost.approachingDataLimit + "\n\r";

        //Display Dataplan status info
        returnString += "Dataplan Status Information:\n\r";
        returnString += "===============\n\r";
        var dataPlanStatus = connectionProfile.getDataPlanStatus();
        if (dataPlanStatus.dataPlanUsage != null) {
            returnString += "Usage In Megabytes: " + dataPlanStatus.dataPlanUsage.megabytesUsed + "\n\r";
            returnString += "Last Sync Time: " + dataPlanStatus.dataPlanUsage.lastSyncTime + "\n\r";
        }
        else {
            returnString += "Dataplan Usage: " + "Not Defined" + "\n\r";
        }

        if (dataPlanStatus.InboundBitsPerSecond != null) {
            returnString += "Inbound Bits Per Second: " + dataPlanStatus.InboundBitsPerSecond + "\n\r";
        }
        else {
            returnString += "Inbound Bits Per Second: " + "Not Defined" + "\n\r";
        }

        if (dataPlanStatus.OutboundBitsPerSecond != null) {
            returnString += "Outbound Bits Per Second: " + dataPlanStatus.OutboundBitsPerSecond + "\n\r";
        }
        else {
            returnString += "Outbound Bits Per Second: " + "Not Defined" + "\n\r";
        }

        if (dataPlanStatus.dataLimitInMegabytes != null) {
            returnString += "Data Limit In Megabytes: " + dataPlanStatus.dataLimitInMegabytes + "\n\r";
        }
        else {
            returnString += "Data Limit In Megabytes: " + "Not Defined" + "\n\r";
        }

        if (dataPlanStatus.nextBillingCycle != null) {
            returnString += "Next Billing Cycle: " + dataPlanStatus.nextBillingCycle + "\n\r";
        }
        else {
            returnString += "Next Billing Cycle: " + "Not Defined" + "\n\r";
        }

        if (dataPlanStatus.maxDownloadFileSizeInMegabytes != null) {
            returnString += "Maximum Download File Size in Megabytes: " + dataPlanStatus.maxDownloadFileSizeInMegabytes + "\n\r";
        }
        else {
            returnString += "Maximum Download File Size in Megabytes: " + "Not Defined" + "\n\r";
        }
        returnString += "Cost Based Suggestions: " + CostBasedSuggestions(connectionCost) + "\n\r";
    }

    catch (e) {
        mySample.displayError("Exception Caught: " + e + "\n\r");
    }

    return returnString;
}

요약

이 항목에서는 연결 프로필 및 각 프로필에 포함된 연결 정보를 검색하는 방법을 살펴보았습니다. 이 정보를 사용하여 앱이 올바른 선택을 하도록 하는 것은 안정적인 연결 환경을 위해 필수적입니다.

연결 정보를 사용하여 네트워크 앱의 동작을 안내하는 자세한 내용 및 모범 사례를 보려면 빠른 시작: 연결 이벤트 및 가용성 변경 관리를 참조하세요.

관련 항목

네트워크 정보 샘플

빠른 시작: 연결 이벤트 및 가용성 변경 관리

특정 기간 동안의 연결 사용 데이터를 검색하는 방법

네트워크 어댑터 및 위치 정보를 검색하는 방법