How connect to the Laravel websocket?

UnknownSoul 1 Reputation point
2021-08-03T10:45:28.397+00:00

Hello

I am using this in the Laravel side: https://beyondco.de/docs/laravel-websockets/getting-started/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 https://github.com/pusher/pusher-websocket-dotnet but it didn't work with the laravel websocket.

I tried this code too ( with this library https://github.com/doghappy/socket.io-client-csharp ):

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.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
39,573 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 34,346 Reputation points
    2021-08-04T17:48:19.58+00:00

    I have no idea what Laravel is or how it functions.

    For normal Windows sockets troubleshooting, there are some basic tests that you can do. Do you have access to the server that you are trying to connect to?

    If you have access, you should first verify that some process is listening on the port that you are trying to connect to. Open an admin command prompt and run this command.

    netstat -aon | findstr -i listen
    

    What port number does Laravel listen on? 6001? I see this line in your code.

    var uri = new Uri("http://websocket.test:6001");
    

    What is websocket.test? Is that the name of the server that you are trying to connect to? Or is that just some name that you used to hide the actual name of the server in this post?

    In the command output do you see something listening on port 6001? The firewall might be blocking aceess. Temporarily turn the Windows firewall off on the server.

    On the client open a Powershell prompt and run this test using the name or IP address of the server.

    Test-NetConnection -ComputerName YourServerName -port 6001
    

    Are you able to connect?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.