L
L
Luigi2021-09-30 11:38:55
Laravel
Luigi, 2021-09-30 11:38:55

How to connect to a channel via Websocket?

I'm making a simple chat application in Xamarin (client) and Laravel (server)
Set up websockets on the server using laravel websockets library and pusher

Here is my event NewMessage

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

    public $message;

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

    public function broadcastOn()
    {
        return new Channel('chat-room');
    }

    public function broadcastWith(): array
    {
        return [
            "message" => $this->message
        ];
    }
}

I registered a chat-room channel
Broadcast::channel('chat-room', function ($message) {
    return true;
});

I can successfully connect from my client via sockets using the WebSocketSharp (C#) library.
However, I only receive this in the onMessage method
data: "{"socket_id":"249965922.74383293","activity_timeout":30}"

Notifying me that I have connected to the server, and I need to connect to the chat-room further, but the documentation for the library does not describe how to do this. Is it possible ?
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using WebSocketSharp;

namespace ConsoleApp1
{
    class Program
    {
        static  void Main(string[] args)
        {
            Con1();
        }

        private static async Task Con1()
        {
            using (WebSocketSharp.WebSocket web = new WebSocketSharp.WebSocket("ws://192.168.0.105:6001/app/ABCDEFG"))
            {
                web.OnMessage += WsOnMEssage;
                web.Connect();
                web.Ping();
                Console.ReadKey();
            }
        }

        private static void WsOnMEssage(object sender, MessageEventArgs e)
        {
            Console.WriteLine(e.Data);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question