Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

I want to use WebSocket and PuppeteerSharp to create a server and multiple clients…

  1. User

    I want to use WebSocket and PuppeteerSharp to create a server and multiple clients system. What I want is for the server to listen for incoming connections.

    When a connection is made, the server signals the connected client to open a new Puppeteer browser and go to google.com. (on the client side, so there is no need to open a browser on the VPS server)

    The client sends back the response to the server.

  2. Assistant

    To achieve this, you can use WebSocket in combination with PuppeteerSharp. First, you need to install PuppeteerSharp. Open the terminal and type the following command.

    dotnet add package PuppeteerSharp
    

    Now, let's create a WebSocket server and multiple clients system.

    Server

    Create a new C# console application and install the following package:

    dotnet add package Fleck
    

    Add the following code to the Program.cs:

    using System;
    using Fleck;
    
    class Program
    {
        static void Main(string[] args)
        {
            FleckLog.Level = LogLevel.Info;
            var server = new WebSocketServer("ws://0.0.0.0:8181");
            server.Start(socket =>
            {
                socket.OnOpen = () => Console.WriteLine("Client connected");
                socket.OnClose = () => Console.WriteLine("Client disconnected");
                socket.OnMessage = async message =>
                {
                    Console.WriteLine("Received message: " + message);
                    if (message == "OpenBrowser")
                    {
                        await socket.Send("GoToGoogle");
                    }
                };
            });
    
            Console.ReadLine();
        }
    }
    

    Client

    Create a new C# console application and install the following package:

    dotnet add package WebSocketSharp
    

    Add the following code to the Program.cs:

    using System;
    using PuppeteerSharp;
    using WebSocketSharp;
    
    class Program
    {
        static void Main(string[] args)
        {
            var url = "ws://localhost:8181";
            using (var ws = new WebSocket(url))
            {
                ws.OnMessage += async (sender, e) =>
                {
                    Console.WriteLine("Message received: " + e.Data);
                    if (e.Data == "GoToGoogle")
                    {
                        await RunPuppeteer();
                    }
                };
    
                ws.Connect();
    
                ws.Send("OpenBrowser");
    
                Console.ReadKey(true);
            }
        }
    
        private static async Task RunPuppeteer()
        {
            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
            using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });
            using var page = await browser.NewPageAsync();
            await page.GoToAsync("https://www.google.com");
    
            // Send the response back to the server
            Console.WriteLine("Google opened");
        }
    }
    

    Now run the server, then run one or more client instances. Each client will open a new Puppeteer browser and go to google.com when it receives a message from the server.