4using System.Threading.Tasks;
25 : base(
ResourceName, false, 1024 * 1024, 1024 * 1024,
"xmpp")
29 this.Connected += this.WebSocketClientResource_Connected;
36 streamId = this.xmppServer.GetRandomHexString(16)
39 e.Socket.Closed += this.Socket_Closed;
40 e.Socket.TextReceived += this.Socket_TextReceived;
41 e.Socket.Heartbeat += this.Socket_Heartbeat;
42 e.Socket.Disposed += this.Socket_Disposed;
44 return Task.CompletedTask;
49 WebSocketSession Session = (WebSocketSession)e.
Socket.
Tag;
50 Session.TouchConnection();
51 return Task.CompletedTask;
54 private async Task Socket_Disposed(
object Sender, EventArgs e)
57 await WebSocketSession.DisposeAsync();
62 WebSocketSession Session = (WebSocketSession)e.
Socket.
Tag;
64 if (!Session.streamOpen)
65 this.SendOpen(Session);
67 e.
Socket.
Send(
"<error xmlns=\"http://etherx.jabber.org/streams\">" +
68 ErrorXml +
"</error>");
75 this.StreamError(e,
"<invalid-xml xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>",
81 this.StreamError(e,
"<host-unknown xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>",
85 private Task SendOpen(WebSocketSession Session)
87 Session.streamOpen =
true;
89 StringBuilder Xml =
new StringBuilder();
91 Xml.Append(
"<open xmlns=\"");
93 Xml.Append(
"\" from=\"");
94 Xml.Append(Session.To);
95 Xml.Append(
"\" id=\"");
96 Xml.Append(Session.streamId);
97 Xml.Append(
"\" xml:lang=\"en\" version=\"1.0\"/>");
99 return Session.webSocket.Send(Xml.ToString());
106 WebSocketSession Session = (WebSocketSession)e.
Socket.
Tag;
108 if (!Session.streamOpen)
110 XmlDocument Doc =
new XmlDocument()
112 PreserveWhitespace =
true
122 this.StreamErrorInvalidXml(e);
126 Body = Doc.DocumentElement;
129 this.StreamErrorInvalidXml(e);
135 this.StreamErrorInvalidXml(e);
139 foreach (XmlAttribute Attribute
in Body.Attributes)
141 switch (Attribute.Name)
144 Session.From = Attribute.Value;
148 Session.To = Attribute.Value;
154 this.StreamErrorInvalidXml(e);
157 Session.Version = Version;
161 Session.Language = Attribute.Value;
169 if (!this.xmppServer.IsServerDomain(Session.To,
true) &&
170 (!
string.IsNullOrEmpty(
this.xmppServer.Domain) ||
171 !IPAddress.TryParse(Session.To, out IPAddress _)))
173 this.StreamErrorHostUnknown(e);
177 await this.SendOpen(Session);
179 await Session.InitStream();
182 await Session.ProcessFragment(e.
Payload);
192 if (e.
Socket.
Tag is WebSocketSession WebSocketSession)
193 await WebSocketSession.DisposeAsync();
201 private static string StripPort(
string s)
203 int i = s.LastIndexOf(
':');
207 return s.Substring(0, i);
Helps with parsing of commong data types.
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Static class managing the application event log. Applications and services log events on this static ...
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.
string RemoteEndPoint
Remote end-point.
string ResourceName
Name of resource.
Event arguments for websocket closed events.
Event arguments for websocket events.
WebSocket Socket
Web-socket
Class handling a web-socket.
Task Close()
Closes the connection.
object Tag
Applications can use this property to attach a value of any type to the websocket connection.
async Task Send(string Payload, int MaxFrameLength)
Sends a text payload, possibly in multiple frames.
HttpRequest HttpRequest
Original HTTP request made to upgrade the connection to a WebSocket connection.
HTTP resource implementing the WebSocket Protocol as defined in RFC 6455: https://tools....
Event arguments for websocket text events.
string Payload
Text payload.
Web Socket client interface
const string FramingNamespace
urn:ietf:params:xml:ns:xmpp-framing
WebSocketClientResource(XmppServer XmppServer, string ResourceName)
Web Socket client interface
WebSocketCloseStatus
Close status codes.