Socket.IO客户端Java(Android)无法从laravel-echo-server接收消息

时间:2018-07-24 11:47:05

标签: php android laravel laravel-echo

我试图在Android上使用laravel-echo-server和Socket.IO-client制作推送通知通道。

这些是我的配置:

web.php

Route::get('event', function(){
    event(new NotificationEvent('testestsetsetes'));
});

channels.php

Broadcast::channel('everyone', function () {
    return true;
});
事件文件夹中的

NotificationEvent.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

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

    public $msg = '';


    public function __construct($msg)
    {
        $this->msg = $msg;
    }

    public function broadcastOn()
    {
        return new Channel('everyone');
    }

}

我已经使用Redis成功安装了laravel-echo-server。

在Android方面,这是我的SocketListener类:

class SocketListener : WebSocketListener() {
    fun start() {
        Log.e("jsonString : ", "ECHO START...")
        try {
            var socket : Socket
            socket = IO.socket("http://10.0.2.2:6001/")
            socket.on(Socket.EVENT_CONNECT, Emitter.Listener
            {
                Log.e("jsonString : ", "ECHO CONNECTED") }).on(
                    "NotificationEvent",
                    Emitter.Listener { Log.e("jsonString : ", "ECHO NOTIFICATION CREATED") }
               ).on(Socket.EVENT_DISCONNECT, Emitter.Listener {
                Log.e("jsonString : ", "ECHO DISCONNECTED")
            }).on("newMessage", onNewMessage)
            socket.connect()
            val `object` = JSONObject()
            val auth = JSONObject()
            val headers = JSONObject()
            try {
                `object`.put("channel", "everyone")
            } catch (e: JSONException) {
                e.printStackTrace()
            }

            socket.emit("subscribe", `object`, Ack {
                Log.e("jsonString : ", "ECHO SUBSCRIBED") // This event never occurs. I don't know why...
            })
        } catch (e: URISyntaxException) {
            Log.e("jsonString : ", "ECHO ERROR")
            e.printStackTrace()
        }

    }
    private val onNewMessage = Emitter.Listener { args ->
            val data = args[0] as JSONObject
            val username: String
            val message: String
            try {
                username = data.getString("username")
                message = data.getString("message")
            } catch (e: JSONException) {
                Log.e("error", e.message)

            }
    }
} 

当我在android日志中启动我的项目时,显示它已成功连接到套接字服务,如下所示:

07-24 11:34:53.746 26627-26679/com.test.test E/jsonString :: ECHO START...
07-24 11:34:53.976 26627-26688/com.test.test E/jsonString :: ECHO CONNECTED

同时在我的laravel回声面板中显示:

enter image description here

但是在ive这样从浏览器运行我的事件类之后:

http://localhost:8050/event

没有调用adnoird上的onNewMessage事件(另一端没有消息)...

0 个答案:

没有答案