语言

Menu
Sites
Language
Remote Message Port is not working

var gServiceAppId = 'cFlc2IE9pQ.samplenativeapp',

    /**
     * Service port name.
     *
     * @type {string}
     */
    gServicePortName = 'NATIVE_PORT_SAMPLE',
    

    /**
     * Local message port name.
     *
     * @type {string}
     */
    gLocalMessagePortName = 'WEB_PORT_SAMPLE',

    /**
     * Local message port object.
     *
     * @type {LocalMessagePort}
     */
    gLocalMessagePort =null,

    /**
     * Remote message port object.
     *
     * @type {RemoteMessagePort}
     */
    gRemoteMessagePort = null,
 try {
        gRemoteMessagePort = tizen.messageport
            .requestRemoteMessagePort(gServiceAppId, gServicePortName);
        
    } catch (ex) {
        gRemoteMessagePort = null;
        console.log(ex.name);
    }
   
 

 


in catch block, console.log(ex), it is showing "NotFoundError" and i cannot send message from web to native. Any solution??

 

响应

5 回复
Ntobeko Sikithi

Hi Mahabub Zoraf 

i have encounted this while working with a hybrid app and what i found was that there was nothing wrong with the remote message port instead i had an error in the native service and after i fixed the error everything was working again.

Mahabub Zoraf
Hi Ntobeko Sikithi, actually i don't find any problem in my native service. Interesting fact is that, sometimes it works perfectly and sometimes it shows me the error that no remote message port is found.
Slawek Kowalski

Sometimes it works, sometims doesn't. When your Web app calls native app it is not started yet.

Armaan-Ul- Islam

I am sharing youu my sample code:

WebSender JS Code

function sendMsg(){
    var targetApplicationId = "org.example.messageportnativeservicereceiver";

    try {
        var remotePort = tizen.messageport.requestRemoteMessagePort(targetApplicationId, "CrossPort2");   	        
        console.log("Remote Port Requested Succesfully.");
	 } 
    catch (e) {   	
        console.log(e.name);   
    }
}

 

NativeServiceReceiver C Code

#include <message_port.h>
#define LOG_TAG "qwe"

void
message_port_cb(int local_port_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port,  
                      bundle*message, void *user_data){
    //Do code
}

void
receiveMsg(){
    char *local_port="CrossPort2";
    int port = message_port_register_local_port(local_port, message_port_cb, NULL);
	
    if (port < 0)
        dlog_print(DLOG_ERROR, LOG_TAG, "Port register error: %d", port);
    else
        dlog_print(DLOG_INFO, LOG_TAG, "Local Port Registered Successfully");
}

bool service_app_create(void *data)
{
    receiveMsg();
    return true;
}

 

N.B: Local port registration function on service app is placed on service_app_create function, so it will be invoked only once the app is launched/deployed. For this sample code you have to deploy the service app to emulator/device first and then check logs to make sure the local port is registered successfully.

Then Run>Debug as Web application to request Remote port.

Armaan-Ul- Islam

Hello Mahabub Zoraf,

How's the current Stat? Is the message port communication firm now?

 

In addition: You may already know, 'NotFoundError' exception is being thrown when the localPort of the first app ( in your case Native app/service) is not registered yet. Make a workarround to make sure the Native app/service is the one who registers the local Port first before the web app requesting for the remote port.