public void DisplayData(OnSubscriptionDataReceived subscriptionDataReceived) { Debug.Log("I was called"); subscriptionDisplay.text = HttpHandler.FormatJson(subscriptionDataReceived.data); }
//Call GetWsReturn to wait for a message from a websocket. GetWsReturn has to be called for each message static async void GetWsReturn(ClientWebSocket cws) { ArraySegment <byte> buf = new ArraySegment <byte>(new byte[1024]); buf = WebSocket.CreateClientBuffer(1024, 1024); WebSocketReceiveResult r; string result = ""; do { r = await cws.ReceiveAsync(buf, CancellationToken.None); result += Encoding.UTF8.GetString(buf.Array ?? throw new ApplicationException("Buf = null"), buf.Offset, r.Count); } while (!r.EndOfMessage); if (String.IsNullOrEmpty(result)) { return; } JObject obj = new JObject(); try{ obj = JObject.Parse(result); } catch (JsonReaderException e) { throw new ApplicationException(e.Message); } string subType = (string)obj["type"]; switch (subType) { case "connection_ack": { Debug.Log("init_success, the handshake is complete"); OnSubscriptionHandshakeComplete subscriptionHandshakeComplete = new OnSubscriptionHandshakeComplete(); subscriptionHandshakeComplete.FireEvent(); GetWsReturn(cws); break; } case "error": { throw new ApplicationException("The handshake failed. Error: " + result); } case "connection_error": { throw new ApplicationException("The handshake failed. Error: " + result); } case "data": { OnSubscriptionDataReceived subscriptionDataReceived = new OnSubscriptionDataReceived(result); subscriptionDataReceived.FireEvent(); GetWsReturn(cws); break; } case "ka": { GetWsReturn(cws); break; } case "subscription_fail": { throw new ApplicationException("The subscription data failed"); } } }
private void OnDisable() { OnSubscriptionDataReceived.UnregisterListener(DisplayData); }