question

UnknownSoul-8752 avatar image
0 Votes"
UnknownSoul-8752 asked MotoX80 answered

How connect to the Laravel websocket?

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-supported
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @UnknownSoul-8752,
For questions about Laravel websocket, please ask the question on GitHub and you can get more professional answer.
Best Regards,
Daniel Zhang


0 Votes 0 ·

1 Answer

MotoX80 avatar image
0 Votes"
MotoX80 answered

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?


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.