Hey everyone! I am kinda new to all this so please bear with me and my questions
I have created a networking project between Unity and Hololens 2. In the project, my goal is to send any data to hololens and when the data is received I want to see a change in the scene. The code for the projects work like this:
-We see 2 cubes in the scene. As the code run, the first cube turns green. Then we have an event (with an action assigned to it)in the code. When the event happens, the same cube turns red. When the action happens, both of the cubes should turn yellow. However, this is the part I get the problem. The action part (ClientDatagramSocket_MessageReceived) of the code doesnโt work.
I am sharing the receiver code, so if anyone can see the problem and help me I would be grateful!
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if !UNITY_EDITOR
using Windows.Networking;
using Windows.Networking.Sockets;
#endif
public class CubeControllerUWP : MonoBehaviour
{
public int listenPort = 5001;
private Vector3 targetPosition;
public GameObject cube;
public GameObject cube2;
private Material m;
void Start()
{
m = new Material(cube.GetComponent<Renderer>().material);
m.color = Color.green;
#if !UNITY_EDITOR
StartClient();
#endif
}
#if !UNITY_EDITOR
private async void StartClient()
{
m.color = Color.green;
cube.GetComponent<Renderer>().material = m;
try
{
// Create the DatagramSocket and establish a connection to the echo server.
var clientDatagramSocket = new Windows.Networking.Sockets.DatagramSocket();
clientDatagramSocket.MessageReceived += ClientDatagramSocket_MessageReceived;
await clientDatagramSocket.BindServiceNameAsync("12345");
m.color = Color.red;
cube.GetComponent<Renderer>().material = m;
}
catch (Exception ex)
{
Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult);
}
}
#endif
#if !UNITY_EDITOR
private async void ClientDatagramSocket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
{
m.color = Color.yellow;
cube2.GetComponent<Renderer>().material = m;
cube.GetComponent<Renderer>().material = m;
sender.Dispose();
/* string response;
using (DataReader dataReader = args.GetDataReader())
{
response = dataReader.ReadString(dataReader.UnconsumedBufferLength).Trim();
}
*/
// await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.clientListBox.Items.Add(string.Format("client received the response: \"{0}\"", response)));
// await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.clientListBox.Items.Add("client closed its socket"));
}
#endif
/* private Vector3 ParseDataToPosition(string data)
{
string[] parts = data.Split(',');
float x = float.Parse(parts[0]);
float y = float.Parse(parts[1]);
float z = float.Parse(parts[2]);
return new Vector3(x, y, z);
}
void Update()
{
transform.position = targetPosition;
}
void OnDestroy()
{
// udpClient.Close();
}
*/
}