4using System.Threading.Tasks;
27 : base(
ResourceName, false, 1024 * 1024, 1024 * 1024,
"xmpp")
31 this.Connected += this.WebSocketClientResource_Connected;
39 streamId = this.xmppServer.GetRandomHexString(16)
42 e.Socket.Closed += this.Socket_Closed;
43 e.Socket.TextReceived += this.Socket_TextReceived;
44 e.Socket.Heartbeat += this.Socket_Heartbeat;
45 e.Socket.Disposed += this.Socket_Disposed;
47 return Task.CompletedTask;
52 WebSocketSession Session = (WebSocketSession)e.
Socket.
Tag;
53 Session.TouchConnection();
54 return Task.CompletedTask;
57 private async Task Socket_Disposed(
object Sender, EventArgs e)
60 await WebSocketSession.DisposeAsync();
65 WebSocketSession Session = (WebSocketSession)e.
Socket.
Tag;
67 if (!Session.streamOpen)
68 this.SendOpen(Session);
70 e.
Socket.
Send(
"<error xmlns=\"http://etherx.jabber.org/streams\">" +
71 ErrorXml +
"</error>");
78 this.StreamError(e,
"<invalid-xml xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>",
84 this.StreamError(e,
"<host-unknown xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>",
88 private Task SendOpen(WebSocketSession Session)
90 Session.streamOpen =
true;
92 StringBuilder Xml =
new StringBuilder();
94 Xml.Append(
"<open xmlns=\"");
96 Xml.Append(
"\" from=\"");
97 Xml.Append(Session.To);
98 Xml.Append(
"\" id=\"");
99 Xml.Append(Session.streamId);
100 Xml.Append(
"\" xml:lang=\"en\" version=\"1.0\"/>");
102 return Session.webSocket.Send(Xml.ToString());
109 WebSocketSession Session = (WebSocketSession)e.
Socket.
Tag;
111 if (!Session.streamOpen)
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();
Helps with parsing of commong data types.
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Helps with common XML-related tasks.
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
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< bool > 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.