Skip to content

2) Quickstart

Nicholas Ventimiglia edited this page Jun 20, 2016 · 3 revisions

Initialize

// Construct object	
_ortc = OrtcFactory.Create();

// Handlers
_ortc.OnConnected += ortc_OnConnected;
_ortc.OnDisconnected += ortc_OnDisconnected;
_ortc.OnReconnecting += ortc_OnReconnecting;
_ortc.OnReconnected += ortc_OnReconnected;
_ortc.OnSubscribed += ortc_OnSubscribed;
_ortc.OnUnsubscribed += ortc_OnUnsubscribed;
_ortc.OnException += ortc_OnException;

Connect

public string URL = "http://ortc-developers.realtime.co/server/2.1";
public string URLSSL = "https://ortc-developers.realtime.co/server/ssl/2.1";

// Use cluster unless server is private
if (ClientIsCluster)
{
	_ortc.ClusterUrl = URL;
}
else
{
	_ortc.Url = URL;
}

// Metadata should be a username or user id
_ortc.ConnectionMetadata = ClientMetaData;
_ortc.HeartbeatActive = Heartbeat;
_ortc.Connect("ApplicationKey", "AuthToken");

Subscribe

_ortc.Subscribe("Channel", true, OnMessageCallback);
_ortc.Unsubscribe("Channel");

private void OnMessageCallback(string channel, string message)
{
}

Send

_ortc.Send("Channel", "Message");

Authenticate

If authentication is turned on, you will need to request a key to connect. You may request this key from the client or on a webserver you control.

AuthenticationClient.PostAuthentication(URLSSL,
	ClientIsCluster, AuthToken, AuthTokenIsPrivate, ApplicationKey,
	AuthTTL, PrivateKey, perms, (exception, s) =>
	{
		if (exception)
			Log("Error !");
		else
			Log("Permissions posted");
	});

Presence

First you must enable presence

PresenceClient.EnablePresence(URLSSL, ClientIsCluster, ApplicationKey, PrivateKey, Channel, true,
(exception, s) =>
{
	if (exception)
		// Error !
	else
		// Success !
});

Then you may request presence

PresenceClient.GetPresence(URLSSL, ClientIsCluster, AuthToken, ApplicationKey, Channel,
(exception, presence) =>
{
	if (exception)
	{
		TerminalModel.LogError(String.Format("Error: {0}", exception.Message));
	}
	else
	{
		var result  = presence;

		Log(String.Format("Subscriptions {0}", result.Subscriptions));

		if (result.Metadata != null)
		{
			foreach (var metadata in result.Metadata)
			{
				Log(metadata.Key + " - " + metadata.Value);
			}
		}
	}
});

Subchannels

If presence is enabled, you may subscribe to sub channels and receive announcments of when user connect, disconnect, subscribe or unsubscribe !


protected const string OrtcDisconnected = "ortcClientDisconnected";
protected const string OrtcConnected = "ortcClientConnected";
protected const string OrtcSubscribed = "ortcClientSubscribed";
protected const string OrtcUnsubscribed = "ortcClientUnsubscribed";

_ortc.Subscribe(OrtcConnected, true, OnMessageCallback);
_ortc.Subscribe(OrtcDisconnected, true, OnMessageCallback);
_ortc.Subscribe(OrtcSubscribed, true, OnMessageCallback);
_ortc.Subscribe(OrtcUnsubscribed, true, OnMessageCallback);

The message signature looks like this :

protected class OrtcAnnouncement
{
	// User Metadata
	public string cm;
}
Clone this wiki locally