webOS Article/3. Web Service 개발하기

webOS 서비스에서 다른 서비스 호출하기

하이하정 2021. 9. 19. 21:55

  • webOS 서비스에서 다른 서비스 호출하기

 

webOS 서비스에서 다른 서비스 호출하기


지금까지는 Java Script만 사용하여 기본 Service를 만들었습니다.

 

서비스에서 다른 서비스를 호출해야하는 경우는 굉장히 빈번합니다. 이 챕터에서는 기존 service내에서 새로운 webOS Luna Service를 호출해봅니다. Luna Service에 포함된 toast나 clock 등 다양한 서비스를 호출할 수 있습니다. 이전 포스트에서도 사용한 적이 있는 toast를 호출해봅니다. 

 

1.  toast  service  호출 방법

index.html

  • callToast라는 이름으로 함수를 추가해주었습니다.
<head>
<title>Example Web App</title>
<script type="text/javascript">
    var bridge = new WebOSServiceBridge();

    function callHello() {
        console.log("call my service");
        const url = 'luna://com.cosmos.team2.app.service/hello';
        const params = {};
        bridge.onservicecallback = (msg) => {
            console.log(msg);
            let res = JSON.parse(msg);
            document.getElementById("txt_msg").innerHTML = res.Response;
        };

        bridge.call(url, JSON.stringify(params));
    }

    function callToast() {
        console.log("call my service");
        const url = 'luna://com.cosmos.team2.app.service/toast';
        const params = {};
        bridge.onservicecallback = (msg) => {
            console.log(msg);
            let res = JSON.parse(msg);
            document.getElementById("txt_msg").innerHTML = res.Response;
        };

        bridge.call(url, JSON.stringify(params));
    }
</script>
</head>
  • toast click 버튼을 생성해주었습니다. 
<body>
    <div>
        <h1 id="txt_msg">Hello, Web Application!!</h1>
        <button id="time" onclick="callHello()">hello</button>
        <button id="time" onclick="callToast()">toast</button>
    </div>
</body>

 

appinfo.json

  • service가 구현되는 app의 appinfo.json 파일에 requiredPermissions 요소를 추가해주었습니다.
    • 호출하고싶은 외부 service 모듈의 Access Control Group을 requiredPermissions에 추가시켜주어야합니다.
    • ACG는 webOS 개발자 사이트에서 확인하실 수 있습니다.

my_service.js

  • 위에서 작성한 my_service.js에 toast service를 생성하는 코드를 작성합니다. 
// call another service
service.register("toast", (msg)=> {
    console.log("hi");
    console.log(msg);

    service.call("luna://com.webos.notification/createToast", {message:"hello"}, function(m2) {
        console.log(logHeader, "SERVICE_METHOD_CALLED:com.webos.notification/createToast");
        msg.respond({
            returnValue: true,
            Response: JSON.stringify(m2.payload)
        });
    });
});

 

2.  toast  service  호출 결과

  • 기존에 만든 service에서 toast service가 호출된 것을 확인하실 수 있습니다.