React Native 使用 Laravel Echo 和 Socket.io 无响应

React Native with Laravel Echo and Socket.io no response

我正在构建一个基本应用程序,我想在其中从 Laravel Echo 实时获取事件数据。我正在使用 stack.io。 laravel redis 和 运行 一切正常,没有任何问题。但是,我没有从 React Native 获取我的事件的控制台日志,也没有警告或错误。这是我的代码

React Native App.js

import React from 'react';
import {
  StyleSheet,
  Text,
} from 'react-native';

import Echo from 'laravel-echo';
import socketio from 'socket.io-client';

const echo = new Echo({
  host: 'projectb.io:6001', //tried 127.0.0.1, localhost, ws:// etc...
  broadcaster: 'socket.io',
  client: socketio,
});

echo
    .channel('home')
    .listen('NewMessage', ev => console.log(ev));

const App: () => React$Node = () => {
  return (
    <>
      <Text>The usual stuff</Text>
    </>
  );
};

const styles = StyleSheet.create({

});

export default App;

Laravel根,laravel-echo-server.json:

{
    "authHost": "http://localhost",
    "authEndpoint": "/broadcasting/auth",
    "clients": [],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "secureOptions": 67108864,
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": false,
        "allowOrigin": "",
        "allowMethods": "",
        "allowHeaders": ""
    }
}

新消息事件:

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

    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('home');
    }
}

.env:

BROADCAST_DRIVER=redis
QUEUE_CONNECTION=redis

我正在使用 laravel tinker 来简单地做一个 event(new App\Event\NewMessage("Hello Stack Overflow"))。它确实立即显示在我的队列和我的 laravel 回显服务器上,但在我的本机反应中,我没有收到任何控制台日志。

我正在使用 'RedisManager' => Illuminate\Support\Facades\Redis::class, 而不是 'Redis' => Illuminate\Support\Facades\Redis::class, 因为有一个错误指出没有找到 redis ext。现在 REDIS_CLIENT 设置为 predis

已解决。当我将主机 IP 作为服务器 IP 地址时,它工作正常。希望这对某人有帮助