Changeset 499 for TFP-WebServer/WebServer/src/SSE
- Timestamp:
- Jul 23, 2024, 6:49:56 PM (4 months ago)
- Location:
- TFP-WebServer/WebServer/src/SSE
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
TFP-WebServer/WebServer/src/SSE/AbsEvent.cs
r487 r499 36 36 37 37 openClients.Add (_client); 38 39 logConnectionState ("Connection opened", _client); 38 40 } 39 41 … … 64 66 bytesToSend = Encoding.UTF8.GetBytes (output, 0, output.Length, buf, 0); 65 67 } catch (ArgumentException e) { 66 logError ("Exception while encoding data for output, most likely exceeding buffer size:", false);68 Log.Error ($"[Web] [SSE] '{Name}': Exception while encoding data for output, most likely exceeding buffer size:"); 67 69 Log.Exception (e); 68 70 return; … … 79 81 private void sendBufToListeners (byte[] _bytes, int _bytesToSend) { 80 82 for (int i = openClients.Count - 1; i >= 0; i--) { 81 ESseClientWriteResult writeResult = openClients [i].Write (_bytes, _bytesToSend); 82 if (writeResult == ESseClientWriteResult.Ok) { 83 continue; 84 } 85 86 currentlyOpen--; 87 totalClosed++; 88 89 if (writeResult == ESseClientWriteResult.Error) { 90 logError ("Can not write to endpoint, closing", true); 91 } 83 openClients [i].Write (_bytes, _bytesToSend); 92 84 } 93 }94 95 protected void logError (string _message, bool _printConnections) {96 Log.Error (_printConnections97 ? $"[Web] [SSE] '{Name}': {_message} (Left open: {currentlyOpen}, total opened: {totalOpened}, closed: {totalClosed})"98 : $"[Web] [SSE] '{Name}': {_message}");99 85 } 100 86 101 87 public virtual int DefaultPermissionLevel () => 0; 102 88 89 private void logConnectionState (string _message, SseClient _client) { 90 Log.Out ($"[Web] [SSE] '{Name}': {_message} from {_client.RemoteEndpoint} (Left open: {currentlyOpen}, total opened: {totalOpened}, closed: {totalClosed})"); 91 } 92 103 93 public void ClientClosed (SseClient _client) { 104 openClients.Remove (_client); 94 if (openClients.Remove (_client)) { 95 currentlyOpen--; 96 totalClosed++; 97 98 logConnectionState ("Closed connection", _client); 99 } 105 100 } 106 101 } -
TFP-WebServer/WebServer/src/SSE/SseClient.cs
r487 r499 1 1 using System; 2 2 using System.IO; 3 using System.Net; 3 4 using System.Net.Sockets; 4 5 using System.Text; 5 using SpaceWizards.HttpListener;6 6 using Webserver.UrlHandlers; 7 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; 7 8 8 9 namespace Webserver.SSE { … … 17 18 private static readonly byte[] keepAliveData = Encoding.UTF8.GetBytes (": KeepAlive\n\n"); 18 19 20 public readonly IPEndPoint RemoteEndpoint; 19 21 private readonly SseHandler parent; 20 22 private readonly HttpListenerResponse response; 21 23 private DateTime lastMessageSent = DateTime.Now; 22 24 23 public SseClient (SseHandler _parent, HttpListenerResponse _response) {25 public SseClient (SseHandler _parent, RequestContext _context) { 24 26 parent = _parent; 25 response = _response; 27 response = _context.Response; 28 RemoteEndpoint = _context.Request.RemoteEndPoint; 26 29 27 30 // Keep the request open 28 _response.SendChunked = true;31 response.SendChunked = true; 29 32 30 _response.AddHeader ("Content-Type", "text/event-stream");31 _response.OutputStream.Flush ();33 response.AddHeader ("Content-Type", "text/event-stream"); 34 response.OutputStream.Flush (); 32 35 } 33 36
Note:
See TracChangeset
for help on using the changeset viewer.