Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ClientEventsWebSocket.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Content;
5using Waher.Events;
9
10namespace Waher.IoTGateway
11{
17 {
18 private static readonly string serverId = Hashes.BinaryToString(Gateway.NextBytes(32));
19
24 : base("/ClientEventsWS", true, 10 * 1024 * 1024, 10 * 1024 * 1024, "ls")
25 {
26 this.Accept += this.ClientEventsWebSocket_Accept;
27 this.Connected += this.ClientEventsWebSocket_Connected;
28 }
29
36 public override Task GET(HttpRequest Request, HttpResponse Response)
37 {
38 SetTransparentCorsHeaders(this, Request, Response);
39 return base.GET(Request, Response);
40 }
41
42 private Task ClientEventsWebSocket_Connected(object Sender, WebSocketEventArgs e)
43 {
44 e.Socket.Closed += this.Socket_Closed;
45 e.Socket.Disposed += this.Socket_Disposed;
46 e.Socket.TextReceived += this.Socket_TextReceived;
47
48 return Task.CompletedTask;
49 }
50
51 private async Task Socket_TextReceived(object Sender, WebSocketTextEventArgs e)
52 {
53 if (JSON.Parse(e.Payload) is Dictionary<string, object> Obj &&
54 Obj.TryGetValue("cmd", out object Value) && Value is string Command)
55 {
56 switch (Command)
57 {
58 case "Register":
59 if (Obj.TryGetValue("tabId", out object O1) && O1 is string TabID &&
60 Obj.TryGetValue("location", out object O2) && O2 is string Location)
61 {
62 e.Socket.Tag = new Info()
63 {
64 Location = Location,
65 TabID = TabID
66 };
67
68 try
69 {
70 await ClientEvents.RegisterWebSocket(e.Socket, Location, TabID);
71 }
72 catch (Exception ex)
73 {
74 Log.Exception(ex);
75 }
76
77 await ClientEvents.PushEvent(new string[] { TabID }, "CheckServerInstance", serverId, false);
78 }
79 break;
80
81 case "Unregister":
82 await this.Close(e.Socket);
83 break;
84
85 case "Ping":
86 if (e.Socket.Tag is Info Info)
87 ClientEvents.Ping(Info.TabID);
88 break;
89 }
90 }
91 }
92
93 private class Info
94 {
95 public string Location;
96 public string TabID;
97 }
98
99 private async Task Socket_Disposed(object Sender, EventArgs e)
100 {
101 if (Sender is WebSocket WebSocket)
102 await this.Close(WebSocket);
103 }
104
105 private Task Socket_Closed(object Sender, WebSocketClosedEventArgs e)
106 {
107 return this.Close(e.Socket);
108 }
109
110 private async Task Close(WebSocket Socket)
111 {
112 try
113 {
114 if (Socket.Tag is Info Info)
115 {
116 await ClientEvents.UnregisterWebSocket(Socket, Info.Location, Info.TabID);
117 Socket.Tag = null;
118 }
119 }
120 catch (Exception ex)
121 {
122 Log.Exception(ex);
123 }
124 }
125
126 private Task ClientEventsWebSocket_Accept(object Sender, WebSocketEventArgs e)
127 {
128 // Cross-domain use allowed.
129 //
130 //HttpFieldCookie Cookie;
131 //
132 //if ((Cookie = e.Socket.HttpRequest.Header.Cookie) is null ||
133 // string.IsNullOrEmpty(Cookie[HttpResource.HttpSessionID]))
134 //{
135 // throw new ForbiddenException(Request, "HTTP Session required.");
136 //}
137
138 return Task.CompletedTask;
139 }
140
144 public override bool HandlesSubPaths
145 {
146 get
147 {
148 return false;
149 }
150 }
151 }
152}
Helps with common JSON-related tasks.
Definition: JSON.cs:16
static object Parse(string Json)
Parses a JSON string.
Definition: JSON.cs:45
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Web-socket binding method for the ClientEvents class. It allows clients connect to the gateway using ...
override bool HandlesSubPaths
If the resource handles sub-paths.
ClientEventsWebSocket()
Resource managing asynchronous events to web clients.
override Task GET(HttpRequest Request, HttpResponse Response)
Executes the GET method on the resource.
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static byte[] NextBytes(int NrBytes)
Generates an array of random bytes.
Definition: Gateway.cs:4335
Represents an HTTP request.
Definition: HttpRequest.cs:22
static void SetTransparentCorsHeaders(HttpResource Resource, HttpRequest Request, HttpResponse Response)
Sets CORS headers for a resource, allowing it to be embedded in other sites.
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Class handling a web-socket.
Definition: WebSocket.cs:17
object Tag
Applications can use this property to attach a value of any type to the websocket connection.
Definition: WebSocket.cs:89
HTTP resource implementing the WebSocket Protocol as defined in RFC 6455: https://tools....
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:63