native-app和chrome-extension之间的通信

时间:2013-11-26 15:03:48

标签: c++ c google-chrome google-chrome-extension chrome-native-messaging

我有一个用c ++和chrome扩展名编写的本机应用程序。

我正在使用'chrome native messaging'在他们之间进行交流。

Native-App代码:

int main(int argc, char* argv[]) {
 unsigned int a, c, i, t=0;
 std::string inp;  do {
 inp="";
 t=0;
 // Sum the first 4 chars from stdin (the length of the message passed).
  for (i = 0; i <= 3; i++) {
    t += getchar();
  }

  // Loop getchar to pull in the message until we reach the total
  //  length provided.
  for (i=0; i < t; i++) {
    c = getchar();
    inp += c;
  }

// Collect the length of the message
unsigned int len = inp.length();
//// We need to send the 4 btyes of length information
std::cout << char(((len>>0) & 0xFF))
          << char(((len>>8) & 0xFF))
          << char(((len>>16) & 0xFF))
          << char(((len>>24) & 0xFF));
//// Now we can output our message
std::cout << inp <<std::endl;
flushall();
}while(cnt < 2 );
return 0;  }

这里我正在阅读stdin上chrome-extension发送的消息。并通过在stdout上写回来发送相同的消息。

扩展使用PostMessage()

这是有效的......但是......

当我将程序置于连续循环时,流程只执行一次!

即port.postMessage({'text':'hello_1'})按预期回复,但如果我这样做

port.postMessage({'text':'hello_2'})它不会被回复。

我无法理解问题所在。它需要线程吗?

请帮忙!

谢谢!

3 个答案:

答案 0 :(得分:10)

Marc的答案包含一些错误(继承自问题),并且不适用于长度不适合一个字节的消息。

与本机应用通信时,Chrome的协议是:

  • 通过stdin
  • 接收对本机应用程序的请求
  • 对Chrome的回复是通过stdout
  • 发送的
  • Chrome不能很好地处理Windows样式\ r \ n所以请避免在消息中将stdin模式设置为二进制(这样您就可以正确读取请求len并且\ n不会'转'成\ r \ n)的:

    _setmode(_fileno(stdin),_O_BINARY);
    

请求和响应消息是带有4字节头(uint32)的JSON,其中包含消息的长度: [长度4字节标题] [消息]

阅读请求标题:

uint32_t reqLen = 0;
cin.read(reinterpret_cast<char*>(&reqLen) ,4);

编写响应标题:

cout.write(reinterpret_cast<char*>(&responseLen),4); 

答案 1 :(得分:2)

这对我有用:

 int main(int argc, char* argv[])
 {

 std::cout.setf( std::ios_base::unitbuf ); //instead of "<< eof" and "flushall"
 unsigned int a, c, i, t=0;
 std::string inp;  

 do {

 inp="";
 t=0;
 // Sum the first 4 chars from stdin (the length of the message passed).
  for (i = 0; i <= 3; i++) {
    t += getchar();
  }

  // Loop getchar to pull in the message until we reach the total
  //  length provided.
  for (i=0; i < t; i++) {
    c = getchar();
    inp += c;
  }

//Collect the length of the message
unsigned int len = inp.length();
//// We need to send the 4 btyes of length information
std::cout << char(((len>>0) & 0xFF))
          << char(((len>>8) & 0xFF))
          << char(((len>>16) & 0xFF))
          << char(((len>>24) & 0xFF));
//// Now we can output our message
std::cout << inp;
}

...

答案 2 :(得分:0)

字符串长度解码算法不正确。这是更正:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
       <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
        <property name="cacheConfiguration">
                <!-- SharedRDD cache example configuration (Atomic mode). -->
                <bean class="org.apache.ignite.configuration.CacheConfiguration">
                   <!-- Set a cache name. -->
            <property name="name" value="sharedRDD"/>

            <!-- Set cache mode. -->
            <property name="cacheMode" value="PARTITIONED"/>

                <!-- set atomicity -->
                <property name="atomicityMode" value="ATOMIC"/>
            <!-- Number of backup nodes. -->
                    <property name="backups" value="1"/> 
                      <property name="queryEntities">
                        <list>
                            <bean class="org.apache.ignite.cache.QueryEntity">
                                 <!-- Setting indexed type's key class -->
                                <property name="keyType" value="Integer"></property>

                                 <!-- Setting indexed type's value class -->
                                <property name="valueType" value="Integer"></property>

                                <property name="fields">
                                    <map>
                                        <entry key="number" value="Integer"/>
                                    </map>
                                </property>

                        <!--Enable Index on the field  -->          
                                <property name="indexes">

                    <list>

                       <bean class="org.apache.ignite.cache.QueryIndex">
                                              <constructor-arg value="number"/>
                                           </bean>

                    </list>                                   
                               </property>

                            </bean>
                        </list>
                    </property>
                </bean>
        </property>
        <!-- Explicitly configure TCP discovery SPI to provide list of initial nodes. -->
        <property name="discoverySpi">
            <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
                <property name="ipFinder">
                    <!--
                        Ignite provides several options for automatic discovery that can be used
                        instead os static IP based discovery. For information on all options refer
                        to our documentation: http://apacheignite.readme.io/docs/cluster-config
                    -->
                    <!-- Uncomment static IP finder to enable static-based discovery of initial nodes. -->
                    <!--<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">-->
                    <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder">
                        <property name="addresses">
                            <list>
                                <!-- In distributed environment, replace with actual host IP address. -->
                                <value>127.0.0.1:47500..47509</value>
                            </list>
                        </property>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
</beans>