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;
2using System.Collections.Generic;
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 if (Socket.Tag is Info Info)
113 {
114 await ClientEvents.UnregisterWebSocket(Socket, Info.Location, Info.TabID);
115 Socket.Tag = null;
116 }
117 }
118
119 private Task ClientEventsWebSocket_Accept(object Sender, WebSocketEventArgs e)
120 {
121 // Cross-domain use allowed.
122 //
123 //HttpFieldCookie Cookie;
124 //
125 //if ((Cookie = e.Socket.HttpRequest.Header.Cookie) is null ||
126 // string.IsNullOrEmpty(Cookie[HttpResource.HttpSessionID]))
127 //{
128 // throw new ForbiddenException("HTTP Session required.");
129 //}
130
131 return Task.CompletedTask;
132 }
133
137 public override bool HandlesSubPaths
138 {
139 get
140 {
141 return false;
142 }
143 }
144 }
145}
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static object Parse(string Json)
Parses a JSON string.
Definition: JSON.cs:43
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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:1647
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:126
static byte[] NextBytes(int NrBytes)
Generates an array of random bytes.
Definition: Gateway.cs:3534
Represents an HTTP request.
Definition: HttpRequest.cs:18
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:21
Class handling a web-socket.
Definition: WebSocket.cs:100
object Tag
Applications can use this property to attach a value of any type to the websocket connection.
Definition: WebSocket.cs:146
HTTP resource implementing the WebSocket Protocol as defined in RFC 6455: https://tools....
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:65