Struct MessageEvent
Represents the Server-Sent Event message received from a stream.
Inherited Members
Namespace: LaunchDarkly.EventSource
Assembly: LaunchDarkly.EventSource.dll
Syntax
public struct MessageEvent
Remarks
An SSE event consists of an event name (defaulting to "message" if not specified), a data string, and an optional "ID" string that the server may provide.
The event name and ID properties are always stored as strings. By default, the
data property is also stored as a string. However, in some applications, it may
be desirable to represent the data as a UTF-8 byte array (for instance, if you are
using the System.Text.Json
API to parse JSON data).
Since strings in .NET use two-byte UTF-16 characters, if you have a large block of
UTF-8 data it is considerably more efficient to process it in its original form
rather than converting it to or from a string. EventSource stores
data as strings by default, but you set PreferDataAsUtf8Bytes(bool)
it can store the raw UTF-8 data instead. In either case, MessageEvent
will
convert types transparently so that you can read either Data
or DataUtf8Bytes.
Constructors
MessageEvent(string, Utf8ByteSpan, string, Uri)
Initializes a new instance of the MessageEvent class, providing the data as a UTF-8 byte span.
Declaration
public MessageEvent(string name, Utf8ByteSpan dataUtf8Bytes, string lastEventId, Uri origin)
Parameters
Type | Name | Description |
---|---|---|
string | name | the event name |
Utf8ByteSpan | dataUtf8Bytes | the data received in the server-sent event;
the |
string | lastEventId | the last event identifier, or null |
Uri | origin | the origin URI of the stream |
MessageEvent(string, string, string, Uri)
Initializes a new instance of the MessageEvent class.
Declaration
public MessageEvent(string name, string data, string lastEventId, Uri origin)
Parameters
Type | Name | Description |
---|---|---|
string | name | the event name |
string | data | the data received in the server-sent event |
string | lastEventId | the last event identifier, or null |
Uri | origin | the origin URI of the stream |
MessageEvent(string, string, Uri)
Initializes a new instance of the MessageEvent class.
Declaration
public MessageEvent(string name, string data, Uri origin)
Parameters
Type | Name | Description |
---|---|---|
string | name | the event name |
string | data | the data received in the server-sent event |
Uri | origin | the origin URI of the stream |
Remarks
The LastEventId will be initialized to null.
Fields
DefaultName
The default value of Name if the SSE stream did not specify an
event:
field.
Declaration
public const string DefaultName = "message"
Field Value
Type | Description |
---|---|
string |
Properties
Data
Gets the data received in the event as a string.
Declaration
public string Data { get; }
Property Value
Type | Description |
---|---|
string |
Remarks
This is the value of the data:
field in the SSE data; if there are multiple
data:
lines for a single event, they are concatenated with "\n"
.
If the data was originally stored as a string, the same string is returned. If it was stored as a UTF-8 byte array, the bytes are copied to a new string.
DataUtf8Bytes
Gets the data received in the event as a UTF-8 byte span.
Declaration
public Utf8ByteSpan DataUtf8Bytes { get; }
Property Value
Type | Description |
---|---|
Utf8ByteSpan |
Remarks
This is the value of the data:
field in the SSE data; if there are multiple
data:
lines for a single event, they are concatenated with "\n"
.
If the data was originally stored as UTF-8 bytes, the returned value refers to the same array, offset, and length (it is the caller's responsibility not to modify the byte array). If it was originally stored as a string, the string is copied to a new byte array.
IsDataUtf8Bytes
True if the event data is stored internally as UTF-8 bytes.
Declaration
public bool IsDataUtf8Bytes { get; }
Property Value
Type | Description |
---|---|
bool |
Remarks
The data can be accessed with either Data or DataUtf8Bytes regardless of the value of this property. The property only indicates the original format of the data, so, for instance, if it is true then reading Data will have more overhead (due to copying) than reading DataUtf8Bytes.
LastEventId
Gets the last event identifier received in the server-sent event.
Declaration
public string LastEventId { get; }
Property Value
Type | Description |
---|---|
string |
Remarks
This is the value of the id:
field in the SSE data. If there is no such
field, it is null. You can use a previously received LastEventId
value with LastEventId(string) when starting
a new EventSource to tell the server what the last event you
received was, although not all servers support this.
Name
The event name.
Declaration
public string Name { get; }
Property Value
Type | Description |
---|---|
string |
Remarks
This can be specified by the server in the event:
field in the SSE data, as in
event: my-event-name
. If there is no event:
field, the default name
is DefaultName.
Origin
Gets the origin URI of the stream that generated the server-sent event.
Declaration
public Uri Origin { get; }
Property Value
Type | Description |
---|---|
Uri |
Methods
Equals(object)
Determines whether the specified object is equal to this instance.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | the object to compare with this instance |
Returns
Type | Description |
---|---|
bool | true if the instances are equal |
Overrides
Remarks
This method is potentially inefficient and should be used only in testing.
GetHashCode()
Returns a hash code for this instance.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int | A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. This method is potentially inefficient and should be used only in testing. |