C#Websocket.io不断与Socket.io nodejs服务器断开连接

时间:2019-05-24 14:37:39

标签: c# node.js websocket socket.io

我可以使用带有以下代码的socket.io接收来自我的nodejs服务器的发射。但是,连接保持关闭状态。我如何使其永远打开?

      static async Task Main(string[] args) {
           RunSocketClient();
       }    

   public static void RunSocketClient(Host host) {
            String myJson2 = JsonConvert.SerializeObject(host);

            var ws = new WebSocket("ws://127.0.0.1:3005/socket.io/?EIO=2&transport=websocket");

            ws.OnOpen += (sender, e) =>
            {
                Console.WriteLine("Connected to Socket.IO Server");
            };

            ws.OnClose += (sender, e) =>
            {
                Console.WriteLine("Socket.io Connection closed.");
                while (!ws.IsAlive)
                {
                    Console.WriteLine("Reconnecting once again...");
                    Task.Delay(10000).Wait();
                    ws.Connect();
                    Console.WriteLine("Press Enter to exit...");
                    Console.ReadKey(true);
                }
            };

            ws.OnMessage += (sender, e) =>
            {
                Console.WriteLine(e.Data);
                ws.SendAsync(myJson2, success =>
                {
                    Console.WriteLine("sending scuccessful");
                });
            };


            ws.Connect();
            Console.WriteLine("Press Enter to exit...");
            Console.ReadKey(true);
            ws.Close();



        }

0 个答案:

没有答案