收到本机消息,但响应失败 [英] Native Message received, but response failed

查看:90
本文介绍了收到本机消息,但响应失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个macOS应用程序,该应用程序可以通过本地消息传递与Google chrome扩展程序进行通信。



来自



通知:我尝试更改响应及其长度,但是错误在两种情况下都是相同的。

解决方案

您应该将字符串的长度信息输出到stdout:

  unsigned int len = outMsg.length(); 
std :: cout<< char(len& 0xFF)
<< char((((len>> 8)& 0xFF))
< char(((len>> 16)& 0xFF))
< char(((len>> 24)& 0xFF));

现在您可以将字符串数据写入stdout。


然后使用 std :: flush 立即刷新数据,即从我们的本机发送数据消息传递主机到目标扩展。我已经注意到,如果我们不这样做,那么该数据将不会立即发送到目的地。

  std :: cout< < outMsg.data()<< std :: flush; 


I am developing a macOS application which can communicate with google chrome extension through native messaging.

I used google official documentation from here, so I received data from extension successfully (as is shown in below).

But when I tried to answer, I always get an error. My response is in JSON format and it is:

{
 "text":
        "Client Started"
}

I use swift for my viewController and Objective-c++ for native messaging:

ViewController.swift:

let stream = StreamReader()
override func viewDidLoad() {
    super.viewDidLoad()
    stream.getStream()
    stream.writeStream("{\"text\":\"Client Started\"}")
}

StreamReader.m:

- (void)WriteStream:(NSString *)JSONResponse {
    NSString *JSONResponseArray = [NSString stringWithFormat:@"%@", JSONResponse];
    std::string outMsg = [JSONResponseArray UTF8String];
    unsigned int len = outMsg.length();
    std::cout.write(reinterpret_cast<const char *>(&len), 4);
    std::cout << outMsg.data() << std::flush;
}

Error:

Notice: I tried to change both response and its length, but the error is same in both situations.

解决方案

You should output the string's length info to stdout:

    unsigned int len = outMsg.length();
    std::cout<< char(len & 0xFF)
             << char(((len >> 8) & 0xFF))
             << char(((len >> 16) & 0xFF))
             << char(((len >> 24) & 0xFF));

Now you can write the string data to stdout.

Then use std::flush to immediately flush the data i.e. send the data from our native messaging host to destination extension. I have noticed that if we won't do so then this data won't be sent to the destination immediately.

std::cout << outMsg.data() << std::flush;

这篇关于收到本机消息,但响应失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆