- Home /
Question by
yeurandom92 · Apr 18, 2019 at 08:10 AM ·
c#sockets
Cant received message from server via MessageWebSocket
From the UWP App for PC I could send data to node js server I would like to received this message from the server on Unity programm which later on would be deployed to Hololens
The Code seem to work with the connect part because I could see my hololens is connecting to the server and the server received messaged from the UWP App. However I do not received any message to the hololens, which I could not understand Anyone could help ? I did all all internet net work setting part btw
here is the code
public void Connect(string uri)
{
#if !UNITY_EDITOR
try
{
Connector(uri);
}
catch (Exception ex)
{
Debug.Log(ex.Message);
}
#endif
}
#if !UNITY_EDITOR
private async void Connector(string uri)
{
try
{
_state = State.CONNECTING;
messageWebSocket = new MessageWebSocket();
messageWebSocket.Control.MessageType = SocketMessageType.Utf8;
messageWebSocket.MessageReceived += WebSocket_MessageArrived;
messageWebSocket.Closed += WebSocket_Closed;
_uri = new Uri(uri);
await messageWebSocket.ConnectAsync(_uri);
_state = State.CONNECTED;
dataWriter = new DataWriter(messageWebSocket.OutputStream);
}
catch (Exception ex)
{
Debug.Log(ex.Message);
throw;
}
}
#endif
//Disconnect
public void Disconnect()
{
#if !UNITY_EDITOR
try
{
_state = State.DISCONNECTING;
dataWriter.DetachStream();
dataWriter = null;
messageWebSocket.Dispose();
messageWebSocket = null;
_state = State.DISCONNECTED;
}
catch (Exception ex)
{
Debug.Log(ex.Message);
}
#endif
}
#if !UNITY_EDITOR
//Message Arrived Handler
private void WebSocket_MessageArrived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
{
try
{
if (OnReceiveMessenger == null)
{
return;
}
using (DataReader dataReader = args.GetDataReader())
{
if (dataReader != null)
{
dataReader.UnicodeEncoding = UnicodeEncoding.Utf8;
string message = dataReader.ReadString(dataReader.UnconsumedBufferLength);
//OnReceiveMessenger?.Invoke(message);
// Sending the received message to the public function OnMessageArrived_Handler for further processing ...
OnMessageArrived_Handler(message);
// Here I do not see any message in this function
}
}
}
catch (Exception ex)
{
Debug.Log(ex.Message);
}
}
#endif
Comment