【问题标题】:use laravel echo server in react native client在反应本机客户端中使用 laravel echo 服务器
【发布时间】:2018-01-20 23:18:27
【问题描述】:

我想在 react native 应用中使用 laravel echo server 但我觉得有什么不对劲,我不知道是什么 我的日志中出现此错误

undefined 不是一个对象(评估 'this.connector.channel') 渠道 D:\react-native\taav\node_modules\laravel-echo\dist\echo.js:750:34 组件DidMount

这是我的 laravel

class updateStatus implements  ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
 * Create a new event instance.
 *
 * @return void
 */

public $activity;
public function __construct( Activity $a)
{
    //

    $this->activity=$a;

}

/**
 * Get the channels the event should broadcast on.
 *
 * @return Channel|array
 */
public function broadcastOn()
{
    return ['channel'];
}

}

我知道我的 laravel 服务器是正确的,因为我可以在浏览器中正确使用我的套接字

和我的反应原生代码:

import React, {Component} from 'react';

 import Echo from "laravel-echo"
  import io from 'socket.io-client/dist/socket.io';

 export default class Activities extends Component {

constructor(props) {
    super(props)
    this.btnadd = this.btnadd.bind(this)
    this.SearchMethod = this.SearchMethod.bind(this)
    this.socket = io('http://'.concat(server).concat(':6001'), {json: false})

}
 componentDidMount() {


    var echo = window.Echo = new Echo({
        broadcaster: 'io',
        host: 'http://'.concat(server).concat(':6001')
    });

    window.Echo.channel('channel')
        .listen('updateStatus', (e) => {
            // this.additem()

        })
}

【问题讨论】:

    标签: javascript laravel react-native laravel-echo


    【解决方案1】:

    尝试将您的广播器定义为“socket.io”,该错误应该会消失:

    var echo = window.Echo = new Echo({
        broadcaster: 'socket.io',
        host: 'http://'.concat(server).concat(':6001')
    });
    

    编辑: 看起来我还用 echo 库做了更多的魔法。从内存中,将 Echo 库导入到此处时出现问题来获取“io”。我最终做的是将 Echo 代码直接复制到我的项目中并对其进行编辑以使其工作。如果您将 echo.js 从 /node_modules/laravel-echo/dist/echo.js 复制到您自己的项目中并将其添加到文件的开头:

    window.navigator.userAgent = 'react-native';
    var io = require('socket.io-client');
    

    到文件末尾:

    export default Echo;
    

    然后找到上面写着的那行代码

    this.socket = io(this.options.host, {...this.options, ...{jsonp: false}});
    

    并将其替换为以下内容:

    this.socket = io(this.options.host, this.options);
    

    然后在上面的代码中,从项目中的这个版本而不是从 laravel-echo 中导入 Echo,希望你会有更好的运气。抱歉,我不记得开始的时候了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 2019-11-18
      • 2017-12-29
      • 1970-01-01
      • 2015-09-06
      相关资源
      最近更新 更多