Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,78 +1,78 @@
// Copyright 2019 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.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
namespace Nakama.Snippets
{
public class RejoinChatAfterDisconnect : MonoBehaviour
{
private const string RoomName = "heroes";
private readonly IClient _client = new Client("defaultkey");
private ISocket _socket;
private async void Start()
{
var deviceId = SystemInfo.deviceUniqueIdentifier;
var session = await _client.AuthenticateDeviceAsync(deviceId);
Debug.LogFormat("Session user id: '{0}'", session.UserId);
var roomUsers = new List<IUserPresence>(10);
_socket = _client.NewSocket();
_socket.Connected += () => Debug.Log("Socket connected.");
_socket.ReceivedError += Debug.LogError;
_socket.Closed += () =>
{
Debug.Log("Socket closed.");
roomUsers.Clear();
};
_socket.ReceivedChannelPresence += presenceEvent =>
{
foreach (var presence in presenceEvent.Leaves)
{
roomUsers.Remove(presence);
}
roomUsers.AddRange(presenceEvent.Joins);
Debug.LogFormat("Room users: [{0}]", string.Join(",\n ", roomUsers));
};
_socket.ReceivedChannelMessage += message => Debug.LogFormat("Received message: '{0}'", message);
await _socket.ConnectAsync(session);
// Join chat channel.
var channel = await _socket.JoinChatAsync(RoomName, ChannelType.Room);
roomUsers.AddRange(channel.Presences);
// Simulate a disconnect.
await Task.Delay(TimeSpan.FromSeconds(3));
await _socket.CloseAsync();
await Task.Delay(TimeSpan.FromSeconds(3));
// Reconnect and rejoin chat channel(s).
await _socket.ConnectAsync(session);
var channel2 = await _socket.JoinChatAsync(RoomName, ChannelType.Room);
roomUsers.AddRange(channel2.Presences);
Debug.Log("Rejoined chat!");
}
private void OnApplicationQuit()
{
_socket?.CloseAsync();
}
}
}
// Copyright 2019 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.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

namespace Nakama.Snippets
{
public class RejoinChatAfterDisconnect : MonoBehaviour
{
private const string RoomName = "heroes";

private readonly IClient _client = new Client("defaultkey");
private ISocket _socket;

private async void Start()
{
var deviceId = SystemInfo.deviceUniqueIdentifier;
var session = await _client.AuthenticateDeviceAsync(deviceId);
Debug.LogFormat("Session user id: '{0}'", session.UserId);

var roomUsers = new List<IUserPresence>(10);
_socket = _client.NewSocket();
_socket.Connected += () => Debug.Log("Socket connected.");
_socket.ReceivedError += Debug.LogError;
_socket.Closed += () =>
{
Debug.Log("Socket closed.");
roomUsers.Clear();
};
_socket.ReceivedChannelPresence += presenceEvent =>
{
foreach (var presence in presenceEvent.Leaves)
{
roomUsers.Remove(presence);
}

roomUsers.AddRange(presenceEvent.Joins);
Debug.LogFormat("Room users: [{0}]", string.Join(",\n ", roomUsers));
};
_socket.ReceivedChannelMessage += message => Debug.LogFormat("Received message: '{0}'", message);
await _socket.ConnectAsync(session);

// Join chat channel.
var channel = await _socket.JoinChatAsync(RoomName, ChannelType.Room);
roomUsers.AddRange(channel.Presences);

// Simulate a disconnect.
await Task.Delay(TimeSpan.FromSeconds(3));
await _socket.CloseAsync();
await Task.Delay(TimeSpan.FromSeconds(3));

// Reconnect and rejoin chat channel(s).
await _socket.ConnectAsync(session);
var channel2 = await _socket.JoinChatAsync(RoomName, ChannelType.Room);
roomUsers.AddRange(channel2.Presences);
Debug.Log("Rejoined chat!");
}

private void OnApplicationQuit()
{
_socket?.CloseAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@

// Copyright 2023 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.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Satori.Snippets
{
public class SatoriExample : MonoBehaviour
{
private const string ApiKey = "bb4b2da1-71ba-429e-b5f3-36556abbf4c9";
private IClient _testClient;
private async void Awake()
{
_testClient = new Client("http", "localhost", 7450, ApiKey, UnityWebRequestAdapter.Instance);
Debug.Log("authenticating satori");
try
{
var session = await _testClient.AuthenticateAsync($"{Guid.NewGuid()}");
await _testClient.GetExperimentsAsync(session, Array.Empty<string>());
var experiments = await _testClient.GetAllExperimentsAsync(session);
Debug.Log("num experiments is " + experiments.Experiments.Count());
await _testClient.AuthenticateLogoutAsync(session);
Debug.Log("logged out of satori");
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}
}

// Copyright 2023 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.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Satori.Snippets
{
public class SatoriExample : MonoBehaviour
{
private const string ApiKey = "bb4b2da1-71ba-429e-b5f3-36556abbf4c9";

private IClient _testClient;

private async void Awake()
{
_testClient = new Client("http", "localhost", 7450, ApiKey, UnityWebRequestAdapter.Instance);
Debug.Log("authenticating satori");
try
{
var session = await _testClient.AuthenticateAsync($"{Guid.NewGuid()}");
await _testClient.GetExperimentsAsync(session, Array.Empty<string>());
var experiments = await _testClient.GetAllExperimentsAsync(session);
Debug.Log("num experiments is " + experiments.Experiments.Count());
await _testClient.AuthenticateLogoutAsync(session);
Debug.Log("logged out of satori");

}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}
}

}
Loading