#Coroutine not triggering

1 messages · Page 1 of 1 (latest)

flint pasture
#
async void Start()
    {
        // Create a dictionary of id -> character
        foreach (Character c in character)
        {
            Debug.Log("Adding character " + c.characterId + " to dictionary");
            characterDictionary.Add(c.characterId, c);
        }

        // Set up the websocket
        websocket = new WebSocket("ws://localhost:8080");

        websocket.OnOpen += () =>
        {
            Debug.Log("Connection open!");
        };

        websocket.OnError += (e) =>
        {
            Debug.Log("Error! " + e);
        };

        websocket.OnClose += (e) =>
        {
            Debug.Log("Connection closed!");
        };

        websocket.OnMessage += (bytes) =>
        {
            // parse the message into a JSON object
            string message = System.Text.Encoding.UTF8.GetString(bytes);

            // Deserialize the message into JObject
            var jsonObject = JsonConvert.DeserializeObject<JObject>(message);
            var eventType = jsonObject["type"].Value<string>();

            switch (eventType)
            {
                case "ConnectionEstablished":
                    Debug.Log("Connection established!");
                    break;
                case "MoveEvent":
                    MoveEvent moveEvent = jsonObject["data"].ToObject<MoveEvent>();
                    OnMoveEvent(moveEvent);
                    break;
                case "ToolEvent":
                    ToolEvent toolEvent = jsonObject["data"].ToObject<ToolEvent>();
                    OnToolEvent(toolEvent);
                    break;
                case "SwitchToolEvent":
                    SwitchToolEvent switchToolEvent = jsonObject["data"].ToObject<SwitchToolEvent>();
                    OnSwitchToolEvent(switchToolEvent);
                    break;
                default:
                    throw new Exception("Unknown event type: " + eventType);
            }
        };

        // waiting for messages
        await websocket.Connect();

        StartCoroutine(SendWebSocketMessage());
    }```
#
IEnumerator SendWebSocketMessage()
    {
        Debug.Log("Attempting to send message");

        // Wait until the WebSocket connection is open
        yield return new WaitUntil(() => websocket.State == WebSocketState.Open);

        Debug.Log("Sending agent loop");
        websocket.SendText("{ \"type\": \"AgentLoop\", \"data\": { \"characterId\": \"1\" } }");

        // Wait for 15 seconds before sending the message again
        yield return new WaitForSeconds(15f);

        // Call the coroutine again to send the message repeatedly
        StartCoroutine(SendWebSocketMessage());
    }```
#

@sturdy gyro the first log does not print

#

supposedly it might do to my start being async?

sturdy gyro
#

I don't know what await websocket.Connect(); does but it could be stuck awaiting that?

flint pasture
#

that usually connects without issue

sturdy gyro
#

Put logs in the start function and see how far it reaches

flint pasture
#

i have logs showing the connection working

#

that's what the .onmessage does

#

my guess is to do with that fact that my start is async? and that messes with coroutine somehow?

sturdy gyro
#

Not sure, try switching it up a bit

#

Call an async method from a normal Start

flint pasture
#

I can't await it then, right?

sturdy gyro
#

Do you have to? It's not like you're awaiting Start currently either

flint pasture
#

i guess awaiting it before starting the coroutine seems to make sense, but since it has a 5s delay, that should be good enough

#

got it working

#

removed the async from start