With the following four blocks of code you can trigger events over an open connection through ASP.net (without core) to EventSource Javascript objects or .NET Think clients. Below is an example on how I tackled this issue.
This code is responsible for accepting connections both from HTTP, JS EventSource Listeners and .NET Think clients.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
Public Class JSEventHandler Inherits System.Web.UI.Page Public Shared CurrentConnections As New Dictionary(Of String, HttpResponse) Public Shared ConsoleConnections As New Dictionary(Of String, HttpResponse) Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim CurrentConnectionIndex As Integer = CurrentConnections.Count Debug.WriteLine(Request.QueryString("param")) If Request.UserAgent = "SomeValue" Or Request.QueryString("param") = "SomeValue" Then ConsoleConnections.Add(CurrentConnectionIndex, Response) Else If Request.QueryString("param") = "AnotherValue" Then CurrentConnections.Add(CurrentConnectionIndex, Response) End If If Request.QueryString("param") = "ThirdValue" Then CurrentConnections.Add(CurrentConnectionIndex, Response) End If End If Debug.WriteLine("Connection Added:" & CurrentConnectionIndex) Response.Clear() Dim FromNumber = Request.QueryString("FromNumber") Response.StatusCode = 200 Response.ContentType = "text/event-stream" Response.CacheControl = "no-cache" Response.Headers.Add("Access-Control-Allow-Origin", "*") Dim Tracker As Integer = 0 While Response.IsClientConnected Tracker += 1 System.Threading.Thread.Sleep(200) Response.Flush() 'If this is not used then the OpenMessage will not send If Tracker Mod 5 = 0 Then 'Debug.WriteLine("IsClientConnected: " & Response.IsClientConnected) End If End While If Request.UserAgent = "" Or Request.QueryString("param") = "SomeValue" Then CurrentConnections.Remove(CurrentConnectionIndex) Else If Request.QueryString("param") = "OtherValue" Then CurrentConnections.Remove(CurrentConnectionIndex) End If End If Debug.WriteLine("Connection Removed:" & CurrentConnectionIndex) Response.Close() End Sub End Class |
The code below is used to trigger from an external source to send data to both JS EventSource listeners and Raw .NET thickclients using .Net WebClient. Below is for Server -> to JS Event Source & .NETClient
This can be triggered from a web event or from a method call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<WebMethod()> Public Function PushEventTest() As String Dim sentTo As String = "" For Each MyConnection In JSEventHandler.CurrentConnection Dim Response As HttpResponse = CType(MyConnection.Value, HttpResponse) Response.Write("id:eventidPushed" & vbLf) Response.Write("event:message" & vbLf) Response.Write("data: " & Newtonsoft.Json.JsonConvert.SerializeObject(Now) & vbLf) Response.Write(vbLf) Response.Flush() Response.Write("id: EOFEVENT" & vbLf & vbLf) Response.Flush() sentTo &= MyConnection.Key & ", " Next For Each MyConnection In JSEventHandler.ConsoleConnection Dim Response As HttpResponse = CType(MyConnection.Value, HttpResponse) Response.BinaryWrite({&HFF}) Response.BinaryWrite({&H1}) Response.BinaryWrite(BitConverter.GetBytes(Now.ToString.Length)) Response.Write(Now) Response.Flush() Next Return Now & " Send to Connection #: " & sentTo End Function Public Function PushJSEvent(ByVal ClientIDUserAgent As String, data As String) As String Dim sentTo As String = "" For Each MyConnection In JSEventHandler.CurrentConnections Dim myResponse As HttpResponse = CType(MyConnection.Value, HttpResponse) If True Then 'Debug.WriteLine(Request.UserAgent) End If myResponse.Write("id:eventidPushed" & vbLf) myResponse.Write("event:message" & vbLf) myResponse.Write("data: " & data.Replace(vbLf, "<br>") & vbLf) 'Newtonsoft.Json.JsonConvert.SerializeObject() myResponse.Write(vbLf) myResponse.Flush() myResponse.Write("id: EOFEVENT" & vbLf & vbLf) myResponse.Flush() sentTo &= MyConnection.Key & ", " Next Return Now & " Send to Conection #: " & sentTo End Function Private Function PushDCAClientStringData(ByVal ClientIDUserAgent As String, data As String) As Boolean For Each MyConnection In JSEventHandler.ConsoleConnections Dim Response As HttpResponse = CType(MyConnection.Value, HttpResponse) Response.BinaryWrite({&HFF}) Response.BinaryWrite({&H1}) Response.BinaryWrite(BitConverter.GetBytes(data.Length)) Response.Write(data) Response.Flush() Next Return True End Function |
The code below allows the server to handle incoming connections from a .NET Think Client sending POST EventTriggering Data
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Public Class JSStreamReader Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsNothing(Request.QueryString("SomeParam")) Or (Request.UserAgent = "SomeAgent") Then Debug.WriteLine(Request.QueryString("SomeParam") & " " & Request.UserAgent) Dim reader As New System.IO.StreamReader(HttpContext.Current.Request.InputStream) Dim requestFromPost As String = reader.ReadToEnd() Debug.WriteLine(requestFromPost) 'Take any action wanted based on what is sent End If End Sub End Class |
The code below allows the client to both send and receive event data from WebClient connections.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
Private Sub SendDataToServer() Try Dim myClient = New Net.WebClient() myClient.Headers.Add("User-Agent", "SomeAgentValue") Dim MyBytes() As Byte While True Dim Cmd As String = Console.ReadLine() MyBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(Cmd) myClient.UploadData("http://localhost:4858/JSStreamReader.aspx?SomeGetValue=SomeVarToActOn", "POST", MyBytes) End While Catch End Try End Sub Private Sub ReadDataFromServer() Try Dim myClient = New Net.WebClient() myClient.Headers.Add("User-Agent", "SomeAgentValue") Dim FullByteMessage() As Byte = {} Dim FullStringMessage As String = "" Dim MyBytes(0) As Byte Dim DataType(0) As Byte Dim MessageLength(3) As Byte Dim ResponseStrean As IO.Stream = myClient.OpenRead("http://localhost:4858/JSEventHandler.aspx?param=SomeParamValueToActOn") 'Use DCA for Raw Stream or you will get JSEventData While True ResponseStrean.Read(MyBytes, 0, MyBytes.Length) If MyBytes(0) = &HFF Then ResponseStrean.Read(DataType, 0, DataType.Length) ResponseStrean.Read(MessageLength, 0, MessageLength.Length) Dim MaxData As Integer = BitConverter.ToInt32(MessageLength) For i = 0 To MaxData - 1 ResponseStrean.Read(MyBytes, 0, MyBytes.Length) FullByteMessage = FullByteMessage.Concat(MyBytes).ToArray Next End If If DataType(0) = &H1 Then Dim Data As String = System.Text.ASCIIEncoding.ASCII.GetString(FullByteMessage) Debug.WriteLine(Data) Console.WriteLine(Data) End If FullByteMessage = {} End While ResponseStrean.Close() Catch ex As Exception Debug.WriteLine(ex.Message) End Try End Sub |