Question Connect to Laravel Websocket

Valakik

Member
Joined
Nov 8, 2020
Messages
12
Programming Experience
Beginner
Hello
I am using this in the Laravel side: Laravel Websockets - Introduction
I tried connect to the websocket from C# desktop application but no luck. That websocket is clone of the Pusher service so I tried to use the Pusher dotnet client open source: GitHub - pusher/pusher-websocket-dotnet: Pusher Channels Client Library for .NET but it didn't work with the laravel websocket.

I tried this code too ( with this library GitHub - doghappy/socket.io-client-csharp: socket.io-client implemention for .NET ):
C#:
using Newtonsoft.Json;
using SocketIOClient.WebSocketClient;
using SocketIOClient;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.IO;
using System.Net;
using Websocket.Client;
using System.Threading;
using WebSocketSharp;

namespace Websocket
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
            var uri = new Uri("http://websocket.test:6001");

            var socket = new SocketIO(uri, new SocketIOOptions
            {
                Query = new Dictionary<string, string>
                {
                    {"key", "rgeqerg3134gr1ewg" },
                    {"forceTLS", "false" },
                    {"encrypted", "true" },
                    {"disableStats", "true" },
                    {"enabledTransports", "ws" }
                }
            });

            socket.OnConnected += Socket_OnConnected;
            socket.OnPing += Socket_OnPing;
            socket.OnPong += Socket_OnPong;
            socket.OnDisconnected += Socket_OnDisconnected;
            socket.OnReconnectAttempt += Socket_OnReconnecting;
            try
            {
                await socket.ConnectAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw;
            }

            socket.On("event-name", response =>
            {
                Console.WriteLine($"server: {response}");
            });

            Console.ReadLine();
        }

        private static async void Socket_OnConnected(object sender, EventArgs e)
        {
            Console.WriteLine("Socket_OnConnected");
            var socket = sender as SocketIO;

            await socket.EmitAsync("subscribe", new
            {
                channel = "channelName",
                auth = ""
            });
        }

        private static void Socket_OnPing(object sender, EventArgs e)
        {
            Console.WriteLine("Ping");
        }

        private static void Socket_OnPong(object sender, TimeSpan e)
        {
            Console.WriteLine("Pong: " + e.TotalMilliseconds);
        }

        private static void Socket_OnDisconnected(object sender, string e)
        {
            Console.WriteLine("disconnect: " + e);
        }

        private static void Socket_OnReconnecting(object sender, int e)
        {
            Console.WriteLine($"Reconnecting: attempt = {e}");
        }
    }
}

With this code I have problem too because it is trying just reconnecting.
Does someone know how connect to the laravel websocket?

Thank you for the answers in advance.
 
Last edited by a moderator:
Top Bottom