/**
* Copyright 2018 The Nakama Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Nakama
{
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
///
/// A batch of joins and leaves on the low level stream.
///
///
/// Streams are built on to provide abstractions for matches, chat channels, etc. In most cases you'll never need to
/// interact with the low level stream itself.
///
public interface IStreamPresenceEvent
{
///
/// Presences of users who joined the stream.
///
IEnumerable Leaves { get; }
///
/// Presences of users who left the stream.
///
IEnumerable Joins { get; }
///
/// The identifier for the stream.
///
IStream Stream { get; }
}
///
/// A state change received from a stream.
///
public interface IStreamState
{
///
/// The user who sent the state change. May be null.
///
IUserPresence Sender { get; }
///
/// The contents of the state change.
///
string State { get; }
///
/// The identifier for the stream.
///
IStream Stream { get; }
}
///
/// A realtime socket stream on the server.
///
public interface IStream
{
///
/// The descriptor of the stream. Used with direct chat messages and contains a second user id.
///
string Descriptor { get; }
///
/// Identifies streams which have a context across users like a chat channel room.
///
string Label { get; }
///
/// The mode of the stream.
///
int Mode { get; }
///
/// The subject of the stream. This is usually a user id.
///
string Subject { get; }
}
///
internal class StreamPresenceEvent : IStreamPresenceEvent
{
public IEnumerable Leaves => _leaves ?? new List(0);
[DataMember(Name="leaves")]
public List _leaves { get; set; }
public IEnumerable Joins => _joins ?? new List(0);
[DataMember(Name="joins")]
public List _joins { get; set; }
public IStream Stream => _stream;
[DataMember(Name="stream")]
public Stream _stream { get; set; }
public override string ToString()
{
var leaves = string.Join(", ", Leaves);
var joins = string.Join(", ", Joins);
return $"StreamPresenceEvent(Leaves={leaves}, Joins={joins}, Stream={Stream})";
}
}
///
internal class StreamState : IStreamState
{
public IUserPresence Sender => _sender;
[DataMember(Name="sender")]
public UserPresence _sender { get; set; }
public string State => _state;
[DataMember(Name="data")]
public string _state { get; set; }
public IStream Stream => _stream;
[DataMember(Name="stream")]
public Stream _stream { get; set; }
public override string ToString()
{
//var state = System.Text.Encoding.UTF8.GetString(State);
return $"StreamState(Sender={Sender}, State={State}, Stream={Stream})";
}
}
///
internal class Stream : IStream
{
[DataMember(Name="descriptor")]
public string Descriptor { get; set; }
[DataMember(Name="label")]
public string Label { get; set; }
[DataMember(Name="mode")]
public int Mode { get; set; }
[DataMember(Name="subject")]
public string Subject { get; set; }
public override string ToString()
{
return $"Stream(Descriptor={Descriptor}, Label={Label}, Mode={Mode}, Subject={Subject})";
}
}
}