public Form1() { InitializeComponent(); m_socket = new SocketIOSocket(); m_socket.Debug = Console.WriteLine; m_socket.Info = Console.WriteLine; m_socket.Warning = Console.WriteLine; m_socket.Error = Console.WriteLine; m_socket.On("open", Console.WriteLine); m_socket.On("message", Console.WriteLine); m_socket.On("close", Console.WriteLine); m_socket.On("error", Console.WriteLine); m_socket.Start(); m_ping = new PingPong(m_socket); m_timer = new Timer(); m_timer.Interval = 33; m_timer.Tick += (o, e) => { m_socket.Update(); }; m_timer.Start(); }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { if(m_ping!= null) { m_ping.Abort(); m_ping = null; } if(m_socket!= null) { m_socket.Abort(); m_socket = null; } }
public PingPong(SocketIOSocket sio) { m_sio = sio; sio.On("pong", e => { v_thPong = true; v_thPinging = false; }); int reconnectDelay = 5; float pingInterval = 25f; float pingTimeout = 60f; int timeoutMilis = (int)Math.Floor(pingTimeout * 1000); int intervalMilis = (int)Math.Floor(pingInterval * 1000); Start(() => { if (!m_sio.wsConnected) { Thread.Sleep(reconnectDelay); } else { v_thPinging = true; v_thPong = false; m_sio.EmitPacket(new Packet(EnginePacketType.PING)); var pingStart = DateTime.Now; // Pongを待つ while (m_sio.wsConnected && v_thPinging && (DateTime.Now - pingStart).TotalSeconds < timeoutMilis) { Thread.Sleep(200); } // Pongが来なかった if (!v_thPong) { Warning("ping timeout. disconnect"); m_sio.Disconnect(); } Thread.Sleep(intervalMilis); } }); }