首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react native无法订阅mercure后端通知

react native无法订阅mercure后端通知
EN

Stack Overflow用户
提问于 2020-01-06 17:45:06
回答 2查看 871关注 0票数 0

我正在与React Native和Symfony合作开发一个通知系统。我使用docker配置了Mercure,以处理对实时更新的前端订阅:.env文件:

代码语言:javascript
复制
###> symfony/mercure-bundle ###
# See https://symfony.com/doc/current/mercure.html#configuration
MERCURE_PUBLISH_URL=http://mercure.localhost/.well-known/mercure
# The default token is signed with the secret key: !ChangeMe!
MERCURE_JWT_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiKiJdfX0.9-LscFk3QfdDtZ4nyg6BiH4_qDm6pbYHydURBMsDgEc
###< symfony/mercure-bundle ###

mercure.yaml:

代码语言:javascript
复制
mercure:
    enable_profiler: '%kernel.debug%'
    hubs:
        default:
            url: '%env(MERCURE_PUBLISH_URL)%'
            jwt: '%env(MERCURE_JWT_TOKEN)%'

我在我的React Native应用程序中创建了一个通知组件,并在symfony mercure的官方文档中使用了代码:https://symfony.com/doc/4.3/mercure.html#subscribing Notifications.js代码:

代码语言:javascript
复制
import React, { Component } from 'react';
import { View, Text } from 'react-native';

export default class Notifications extends Component {
    constructor(props) {
        super(props);
        this.state = {
        };
    }

    componentDidMount() {
        const url = new URL('http://mercure.localhost/.well-known/mercure');
        url.searchParams.append('topic', 'http://dev.api.partage-mandats.proprietes-privees.com/mandate_shares/{id}');

        url.searchParams.append('topic', 'http://example.com/books/1');
        const eventSource = new EventSource(url);

        eventSource.onmessage = e => console.log(e);
    }

    render() {
        return (
            <View>
                <Text> Notifications </Text>
            </View>
        );
    }
}

我用Postan进行了测试,得到了有效的响应:

我尝试了html文件中的javaScript代码进行测试:

代码语言:javascript
复制
<script type="text/javascript">

const url = new URL('http://mercure.localhost/.well-known/mercure');
url.searchParams.append('topic', 'http://dev.api.partage-mandats.proprietes-privees.com/mandate_shares/{id}');

url.searchParams.append('topic', 'http://example.com/books/1');
const eventSource = new EventSource(url);

eventSource.onmessage = e => console.log(e); 
</script>
</html>

当我用Postman运行request时,我得到了一个实时响应:

我的问题是URL、searchParams和eventSource与react native不兼容。如何将此javaScript代码转换为react本机有效代码?我试着安装了一些库:什么is url,buffer,react-native-event-source,我现在的代码是:

代码语言:javascript
复制
import React, { Component } from 'react';
import { View, Text } from 'react-native';

import { URL, URLSearchParams } from 'whatwg-url';
import { Buffer } from 'buffer';
import RNEventSource from 'react-native-event-source';

export default class Notifications extends Component {
  constructor(props) {
    super(props);
    this.state = {
    };
  }

  componentDidMount() {
    global.Buffer = Buffer;
    global.URL = URL;
    global.URLSearchParams = URLSearchParams;
    global.EventSource = RNEventSource;

    const url = new global.URL('http://mercure.localhost/.well-known/mercure');
    url.global.URLSearchParams.append('topic', 'http://dev.api.partage-mandats.proprietes-privees.com/mandate_shares/{id}');

    url.global.URLSearchParams.append('topic', 'http://example.com/books/1');
    const eventSource = new global.EventSource(url);

    eventSource.onmessage = e => console.log(e); 
  }

  render() {
    return (
      <View>
        <Text> Notifications </Text>
      </View>
    );
  }
}

我得到这个错误:未定义的不是一个对象(计算'url.global.URLSearchParams')

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-26 21:24:58

我修复了这个问题,它是关于发布url的,我不能使用本地url,所以我创建了一个主机,并像这样配置url:

http://mercure.preprod.oryx-immobilier.com/.well-known/mercure

现在我可以从后台接收实时响应

票数 0
EN

Stack Overflow用户

发布于 2020-11-01 22:54:32

你可以像下面这样用->代替URLSearchparams

代码语言:javascript
复制
const url = `http://blablabla.com/?topic=<your_topic1>&
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59609805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档