Once that setup has been completed, you can very easily call all the necessary API requests to connect to a cloudlet with your application deployed. Here is some example code using the MobiledgeXIntegration class that comes with the package:
Getting Edge Connection Url
MobiledgeX SDK uses the device Location and the device's MCC-MNC if avaliable (https://developers.mobiledgex.com/getting-started/connecting-client-app#distributed-matching-engine) to connect you to the closest Edge cloudlet where you application instance is deployed.
If your carrier is not supported yet by MobiledgeX the SDK will throw a DmeDnsException. You can catch this exception and instead use WifiOnly(true) to connect to the wifi dme which will connect you to the closest regional DME.
using DistributedMatchEngine;
using UnityEngine;
using System.Collections;
public class YourClassName : MonoBehaviour
{
IEnumerator Start()
{
GetEdgeConnection();
}
async void GetEdgeConnection()
{
MobiledgeXIntegration mxi = new MobiledgeXIntegration();
try
{
await mxi.RegisterAndFindCloudlet();
}
catch(DmeDnsException)
{
mxi.UseWifiOnly(true);
await mxi.RegisterAndFindCloudlet();
}
mxi.GetAppPort(LProto.L_PROTO_HTTP);
string url = mxi.GetUrl("http");
}
}
Definition: LocationService.cs:29
static IEnumerator EnsureLocation()
EnsureLocation Confirm that user location is valid, user location is essential for MobiledgeX service...
Definition: LocationService.cs:51
Definition: CarrierInfoIntegration.cs:27
If your device doesn't have MCC-MNC ID (no sim card - for ex. Oculus device), Please use UseWifiOnly before RegisterAndFindCloudlet.
use mxi.UseWifiOnly(true);
await mxi.RegisterAndFindCloudlet();
In UnityEditor
While developing in Unity Editor (Location is not used), the fallback location by default is San Jose, CA.
If you wish to change the fallback Location, use SetFallbackLocation() before you call RegisterAndFindCloudlet().
mxi.SetFallbackLocation(testLongtiude, testLatitude);
await mxi.RegisterAndFindCloudlet();
By default in Unity Editor you will connect with the Wifi DME, which is specified using the TestCarrierInfoClass in the CarrierInfoIntegration script.
REST Example
For full example code, Please check RunTime/Scripts/ExampleRest.cs
async void GetEdgeConnection()
{
MobiledgeXIntegration mxi = new MobiledgeXIntegration();
await mxi.RegisterAndFindCloudlet();
mxi.GetAppPort(LProto.L_PROTO_HTTP);
string url = mxi.GetUrl("http");
StartCoroutine(RestExample(url));
RestExampleHttpClient(url);
}
IEnumerator RestExample(string url)
{
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.SendWebRequest();
if (www.isHttpError || www.isNetworkError)
{
Debug.Log(www.error);
}
else
{
Debug.Log(www.downloadHandler.text);
byte[] results = www.downloadHandler.data;
}
}
async Task<HttpResponseMessage> RestExampleHttpClient(string url)
{
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(url);
return await httpClient.GetAsync("?q=x");
}
WebSockets Example
MobiledgeX Unity Package comes with WebSocket Implementation (MobiledgeXWebSocketClient).
For Using MobiledgeXWebSocketClient:
- Start the WebSocket
- Handle received messages from your Edge server.
- Send messages. (Text or Binary)
For full example code, Please check RunTime/Scripts/ExampleWebSocket.cs
async void GetEdgeConnection()
{
mxi = new MobiledgeXIntegration();
await mxi.RegisterAndFindCloudlet();
mxi.GetAppPort(LProto.L_PROTO_TCP);
string url = mxi.GetUrl("ws");
await StartWebSocket(url);
wsClient.Send("WebSocketMsg");
}
async Task StartWebSocket(string url)
{
wsClient = new MobiledgeXWebSocketClient();
if (wsClient.isOpen())
{
wsClient.Dispose();
wsClient = new MobiledgeXWebSocketClient();
}
Uri uri = new Uri(url);
await wsClient.Connect(uri);
}
private void Update()
{
if (wsClient == null)
{
return;
}
var cqueue = wsClient.receiveQueue;
string msg;
while (cqueue.TryPeek(out msg))
{
cqueue.TryDequeue(out msg);
Debug.Log("WebSocket Received messgae : " + msg);
}
}
UDP Example
MobiledgeX Unity Package comes with UDP Client Implementation (MobiledgeXUDPClient).
For Using MobiledgeXUDPClient :
- Start the UDP Connection
- Handle received messages from your Edge server.
- Send messages. (Text or Binary)
For full example code, Please check RunTime/Scripts/ExampleUDP.cs
async void GetEdgeConnection()
{
mxi = new MobiledgeXIntegration();
await mxi.RegisterAndFindCloudlet();
int udpSendPort = mxi.GetAppPort(LProto.L_PROTO_UDP).public_port;
int udpReceivePort = 5000;
udpHost = mxi.GetHost();
SendUDPMessage("Hi, From client to server", udpHost, udpSendPort, udpReceivePort);
}
void SendUDPMessage(string message, string udpHost, int udpSendPort, int udpReceivePort)
{
udpClient = new MobiledgeXUDPClient(udpHost, udpSendPort, udpReceivePort);
udpClient.Connect();
udpClient.Send(message);
}
void Update()
{
if (udpClient == null)
{
return;
}
byte[] udpMsg;
var udp_queue = udpClient.receiveQueue;
while (udp_queue.TryPeek(out udpMsg))
{
udp_queue.TryDequeue(out udpMsg);
string udpReceivedMsg = Encoding.UTF8.GetString(udpMsg);
print("Received UDP Message : " + udpReceivedMsg);
}
}