Imports System
Imports WebSocketSharp
Imports WebSocketSharp.Server
Imports Newtonsoft.Json
Imports System.Timers
Imports System.Collections.Generic


Module Module1
    Dim server As WebSocketServer
    Dim clients As New List(Of WebSocket)()
    Dim timer As Timer
    Dim say As Integer = 0
    Dim tetik As Integer = 1
    Sub Main()
        StartWebSocketServer()

        Using ws = New WebSocket("ws://0.0.0.0:5001")
            AddHandler ws.OnMessage, Sub(sender, e)
                                         Try
                                             Dim jsonData As Object = JsonConvert.DeserializeObject(e.Data)
                                             Console.WriteLine(jsonData)
                                             Dim nereye As String = jsonData.nereye
                                         Catch ex As Exception
                                             Console.WriteLine("Error while parsing JSON: " + ex.Message)
                                         End Try
                                     End Sub ' Lambda ifadesinin sonu

            ws.Connect()
        End Using

        Timer = New Timer()
        Timer.Interval = 2000 ' 2 saniyede bir çalışacak
        AddHandler Timer.Elapsed, AddressOf TimerElapsed
        Timer.Start()

        Console.ReadKey(True)

    End Sub

    Sub StartWebSocketServer()
        server = New WebSocketServer("ws://0.0.0.0:5001")
        server.AddWebSocketService(Of MyWebSocket)("/")
        server.Start()
        Console.WriteLine("WebSocket server started on ws://0.0.0.0:5001")
    End Sub

    Sub TimerElapsed(sender As Object, e As ElapsedEventArgs)
        For Each session In server.WebSocketServices("/").Sessions.Sessions
            If session.State = WebSocketState.Open Then
                Console.WriteLine("Gonder:")
                Try
                    Console.WriteLine(session.ID)
                    Dim message As String = $"{{""nereye"":""outespc"",""cihaz_id"":""1001"",""out"": ""out{say}"",""out_value"":{tetik},""gecikme"":0}}"
                    session.Context.WebSocket.Send(message)
                Catch ex As Exception
                    Console.WriteLine("Error while sending message to client: " + ex.Message)
                End Try
            End If
        Next

        say += 1
        If say > 4 Then
            say = 0
            If tetik = 0 Then
                tetik = 1
            Else
                tetik = 0
            End If
        End If
    End Sub
    Class MyWebSocket
        Inherits WebSocketBehavior

        Protected Overrides Sub OnOpen()
            ' Console.WriteLine("Client connected: " & Context.UserEndPoint)
            clients.Add(Me.Context.WebSocket)
        End Sub

        Protected Overrides Sub OnMessage(ByVal e As MessageEventArgs)
            Try
                Dim jsonData As Object = JsonConvert.DeserializeObject(e.Data)
                Dim message As String = "Received: " & jsonData.ToString()
                Console.WriteLine(message)

                ' Echo message back to client
                Send(message)
            Catch ex As Exception
                Console.WriteLine("Error while sending message to client: " + ex.Message)
            End Try
        End Sub

        Protected Overrides Sub OnClose(ByVal e As CloseEventArgs)
            ' Console.WriteLine("Client disconnected: " & Context.UserEndPoint)
            clients.Remove(Me.Context.WebSocket)
        End Sub
    End Class

End Module

