Merge pull request #2 from Unity-Technologies/feature/doc-example

Feature/doc example
This commit is contained in:
Sebastien Phaneuf 2020-03-18 14:17:21 -04:00 коммит произвёл GitHub
Родитель a730b94260 8aa8935841
Коммит 6d5d695352
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 285 добавлений и 6 удалений

Просмотреть файл

@ -0,0 +1,165 @@
// #define COMMUNICATION_PUBLIC_API
#if COMMUNICATION_PUBLIC_API
using System;
using System.Text;
using Unity.MPE;
using UnityEditor;
using UnityEngine;
public static class ChannelCommunicationDocExample
{
[MenuItem("ChannelDoc/Step 1")]
static void StartChannelService()
{
if (!ChannelService.IsRunning())
{
ChannelService.Start();
}
Debug.Log($"[Step1] ChannelService Running: {ChannelService.GetAddress()}:{ChannelService.GetPort()}");
}
static int s_BinaryChannelId;
static int s_StringChannelId;
static Action s_DisconnectBinaryChannel;
static Action s_DisconnectStringChannel;
[MenuItem("ChannelDoc/Step 2")]
static void SetupChannelService()
{
if (s_DisconnectBinaryChannel == null)
{
s_DisconnectBinaryChannel = ChannelService.GetOrCreateChannel("custom_binary_ping_pong", HandleChannelBinaryMessage);
s_BinaryChannelId = ChannelService.ChannelNameToId("custom_binary_ping_pong");
}
Debug.Log($"[Step2] Setup channel_custom_binary id: {s_BinaryChannelId}");
if (s_DisconnectStringChannel == null)
{
s_DisconnectStringChannel = ChannelService.GetOrCreateChannel("custom_ascii_ping_pong", HandleChannelStringMessage);
s_StringChannelId = ChannelService.ChannelNameToId("custom_ascii_ping_pong");
}
Debug.Log($"[Step2] Setup channel_custom_ascii id: {s_StringChannelId}");
}
static void HandleChannelBinaryMessage(int connectionId, byte[] data)
{
var msg = "";
for (var i = 0; i < Math.Min(10, data.Length); ++i)
{
msg += data[i].ToString();
}
Debug.Log($"Channel Handling binary from connection {connectionId} - {data.Length} bytes - {msg}");
// Let's pong it back:
ChannelService.Send(connectionId, data);
}
static void HandleChannelStringMessage(int connectionId, byte[] data)
{
// We are receiving a new message. All message are always handled as bytes in a ChannelHandler.
// Since our clients are expecting string data, encode the data and send it back as a string:
var msgStr = Encoding.UTF8.GetString(data);
Debug.Log($"Channel Handling string from connection {connectionId} - {msgStr}");
// Let's pong it back:
ChannelService.Send(connectionId, msgStr);
}
static ChannelClient s_BinaryClient;
static Action s_DisconnectBinaryClient;
static ChannelClient s_StringClient;
static Action s_DisconnectStringClient;
[MenuItem("ChannelDoc/Step 3")]
static void SetupChannelClient()
{
const bool autoTick = true;
if (s_BinaryClient == null)
{
s_BinaryClient = ChannelClient.GetOrCreateClient("custom_binary_ping_pong");
s_BinaryClient.Start(autoTick);
s_DisconnectBinaryClient = s_BinaryClient.On(HandleClientBinaryMessage);
}
Debug.Log($"[Step3] Setup client for channel custom_binary_ping_pong. ClientId: {s_BinaryClient.clientId}");
if (s_StringClient == null)
{
s_StringClient = ChannelClient.GetOrCreateClient("custom_ascii_ping_pong");
s_StringClient.Start(autoTick);
s_DisconnectStringClient = s_StringClient.On(HandleClientStringMessage);
}
Debug.Log($"[Step3] Setup client for channel custom_ascii_ping_pong. ClientId: {s_StringClient.clientId}");
}
static void HandleClientBinaryMessage(byte[] data)
{
Debug.Log($"Receiving pong binary data: {data} for clientId: {s_BinaryClient.clientId} with channelName: {s_BinaryClient.channelName}");
}
static void HandleClientStringMessage(string data)
{
Debug.Log($"Receiving pong data: {data} for clientId: {s_StringClient.clientId} with channelName: {s_StringClient.channelName}");
}
[MenuItem("ChannelDoc/Step 4")]
static void ClientSendMessageToServer()
{
Debug.Log("[Step 4]: Clients are sending data!");
s_BinaryClient.Send(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 });
s_StringClient.Send("Hello world!");
}
[MenuItem("ChannelDoc/Step 5")]
static void CloseClients()
{
Debug.Log("[Step 5]: Closing clients");
s_DisconnectBinaryClient();
s_BinaryClient.Close();
s_DisconnectStringClient();
s_StringClient.Close();
}
[MenuItem("ChannelDoc/Step 6")]
static void CloseService()
{
Debug.Log("[Step 6]: Closing clients");
s_DisconnectBinaryChannel();
s_DisconnectStringChannel();
ChannelService.Stop();
}
}
/*
If you execute the 6 menu item one after the other, this will print the following
in the console:
[Step1] ChannelService Running: 127.0.0.1:64647
[Step2] Setup channel_custom_binary id: -1698345965
[Step2] Setup channel_custom_ascii id: -930064725
[Step3] Setup client for channel custom_binary_ping_pong. ClientId: -1698345965
[Step3] Setup client for channel custom_ascii_ping_pong. ClientId: -930064725
[Step 4]: Clients are sending data!
Channel Handling binary from connection 1 - 8 bytes - 01234567
Channel Handling string from connection 2 - Hello world!
Receiving pong binary data: System.Byte[] for clientId: -1698345965 with channelName: custom_binary_ping_pong
Receiving pong data: Hello world! for clientId: -930064725 with channelName: custom_ascii_ping_pong
[Step 5]: Closing clients
[Step 6]: Closing clients
*/
#endif

Просмотреть файл

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f559317f37ae4b144bfc0bcb36ab2723
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Просмотреть файл

@ -1,3 +1,4 @@
// #define REGISTER_CHANNEL_ON_STARTUP
using System;
using System.Collections;
using System.Collections.Generic;
@ -15,14 +16,20 @@ static class ChannelAPIExample
static Action s_DisconnectBinaryChannel;
static Action s_DisconnectStringChannel;
#if REGISTER_CHANNEL_ON_STARTUP
// This function is called each domain reload (i.e each time a script recompiles).
[InitializeOnLoadMethod]
static void RegisterChannelOnLoad()
{
RegisterChannelService();
}
#endif
static void RegisterChannelService()
{
if (!ChannelServiceAPI.IsRunning())
ChannelServiceAPI.StartChannelService();
Debug.Log($"ChannelService Running: {ChannelServiceAPI.GetAddress()}:{ChannelServiceAPI.GetPort()}");
if (s_DisconnectBinaryChannel == null)
@ -40,7 +47,7 @@ static class ChannelAPIExample
}
}
[MenuItem("Tools/Register new channels")]
[MenuItem("ChannelServiceAPI/Register new channels")]
static void RegisterMenu()
{
RegisterChannelService();
@ -66,7 +73,5 @@ static class ChannelAPIExample
// Let's pong it back:
ChannelServiceAPI.Send(connectionId, msgStr);
}
}

Просмотреть файл

@ -0,0 +1,87 @@
// #define COMMUNICATION_PUBLIC_API
#if COMMUNICATION_PUBLIC_API
using UnityEditor;
using UnityEngine;
using Unity.MPE;
using System;
public static class EventServiceDocExample
{
static Action s_CustomLogEventDisconnect;
static Action s_PingPongEventDisconnect;
[MenuItem("EventServiceDoc/Step 0")]
static void StartChannelService()
{
if (!ChannelService.IsRunning())
{
ChannelService.Start();
}
Debug.Log($"[Step 0] ChannelService Running: {ChannelService.GetAddress()}:{ChannelService.GetPort()}");
}
[MenuItem("EventServiceDoc/Step 1")]
static void SetupEventServiceHandlers()
{
Debug.Log("[Step 1] Setup handlers");
s_CustomLogEventDisconnect = EventService.On("custom_log", (eventType, args) => {
Debug.Log($"Log a {eventType} {args[0]}");
});
s_PingPongEventDisconnect = EventService.On("pingpong", (eventType, args) =>
{
Debug.Log($"Receive a {eventType} {args[0]}");
return "pong!";
});
}
[MenuItem("EventServiceDoc/Step 2")]
static void EmitMessage()
{
Debug.Log("[Step 2] Emitting a custom log");
EventService.Emit("custom_log", "Hello world!", -1, EventDataSerialization.JsonUtility);
}
[MenuItem("EventServiceDoc/Step 3")]
static void SendRequest()
{
Debug.Log("[Step 3] Sending a request");
EventService.Request("pingpong", (err, data) =>
{
Debug.Log($"Request fulfilled: {data[0]}");
},
"ping", -1, EventDataSerialization.JsonUtility);
}
[MenuItem("EventServiceDoc/Step 4")]
static void CloseHandlers()
{
Debug.Log("[Step 4] Closing all Event handlers");
s_CustomLogEventDisconnect();
s_PingPongEventDisconnect();
}
}
/*
If you execute the 5 menu item one after the other, this will print the following
in the console:
[Step 0] ChannelService Running: 127.0.0.1:65000
[Step 1] Setup handlers
[Step 2] Emitting a custom log
Log a custom_log Hello world!
[Step 3] Sending a request
Receive a pingpong ping
Request fulfilled: pong!
[Step 4] Closing all Event handlers
*/
#endif

Просмотреть файл

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1f8e14691b0516543a3614c54f867c9c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Просмотреть файл

@ -4,7 +4,7 @@ using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class EventServiceExample : EditorWindow
public class EventServiceExampleWindow : EditorWindow
{
const string k_WebEmit1 = "webEmit1";
const string k_WebRequest1 = "webRequest1";
@ -17,7 +17,7 @@ public class EventServiceExample : EditorWindow
[MenuItem("Tools/Open EventServiceExample Window")]
static void Init()
{
GetWindow<EventServiceExample>();
GetWindow<EventServiceExampleWindow>();
}
void OnEnable()