Outlawdlaw
Outlawdlaw4mo ago

How do I determine whether connecting to a room failed?

When I call realtime.Connect(), I need to determine if the method failed. There's already a didConnectToRoom event, but not one for if it fails.
10 Replies
kapa.ai
kapa.ai4mo ago
Hi @Outlawdlaw. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The Realtime class in NormalVR does not seem to provide a direct event for connection failure. However, you can infer a connection failure by checking the connected property of the Realtime instance. If the connected property is false after attempting to connect, it indicates that the connection has failed. Here's an example of how you might use this:
private void Start() {
// Notify us when Realtime successfully connects to the room
_realtime.didConnectToRoom += DidConnectToRoom;

// Connect to "My Room"
_realtime.Connect("My Room");

// Check if connection was successful
if (!_realtime.connected) {
Debug.Log("Failed to connect to room.");
}
}
private void Start() {
// Notify us when Realtime successfully connects to the room
_realtime.didConnectToRoom += DidConnectToRoom;

// Connect to "My Room"
_realtime.Connect("My Room");

// Check if connection was successful
if (!_realtime.connected) {
Debug.Log("Failed to connect to room.");
}
}
Please note that this is a simple example and might not cover all edge cases. For instance, the connection process might still be ongoing when the connected property is checked, leading to a false negative. You might need to implement additional logic to handle such scenarios. For more information, you can refer to the Realtime class documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.Realtime). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@290458313882337280> kapa.ai is still learning and improving, please let us know how it did by reacting below
Outlawdlaw
Outlawdlaw4mo ago
@Normcore Assistant does it still fire the didConnectToRoom event if it fails to connect?
kapa.ai
kapa.ai4mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- No, the didConnectToRoom event is only fired when the connection to the room is successful. If the connection fails, this event will not be triggered. Here's the description from the Realtime class documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.Realtime):
didConnectToRoomevent RealtimeEvent didConnectToRoom An event that is fired when Realtime is connected to a room and all realtime prefabs and realtime views in the scene have been successfully connected to the datastore.
So, if the connection fails, you won't receive this event. You'll need to implement your own logic to handle connection failures, such as checking the connected property of the Realtime instance after attempting to connect. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@290458313882337280> kapa.ai is still learning and improving, please let us know how it did by reacting below
Outlawdlaw
Outlawdlaw4mo ago
@Normcore Assistant when does the 'connected' property get set? Polling isn't great
kapa.ai
kapa.ai4mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The connected property of the Realtime instance in NormalVR is set to true when the instance is successfully connected to a room and all realtime prefabs and realtime views in the scene have been successfully connected to the datastore. However, the documentation does not specify exactly when this property is set during the connection process. It's likely set internally by the NormalVR library once the connection is established. While polling isn't ideal, you could consider using the didConnectToRoom event to trigger a method when a successful connection is made. If the connection fails, this event won't be triggered, and the connected property should remain false. Here's an example:
private void Start() {
// Notify us when Realtime successfully connects to the room
_realtime.didConnectToRoom += DidConnectToRoom;

// Connect to "My Room"
_realtime.Connect("My Room");
}

private void DidConnectToRoom(Realtime realtime) {
// Connection successful
Debug.Log("Connected to room.");
}
private void Start() {
// Notify us when Realtime successfully connects to the room
_realtime.didConnectToRoom += DidConnectToRoom;

// Connect to "My Room"
_realtime.Connect("My Room");
}

private void DidConnectToRoom(Realtime realtime) {
// Connection successful
Debug.Log("Connected to room.");
}
In this example, DidConnectToRoom will only be called if the connection is successful. If the connection fails, this method won't be called, and _realtime.connected should be false. For more information, refer to the Realtime class documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.Realtime). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@290458313882337280> kapa.ai is still learning and improving, please let us know how it did by reacting below
Mechabit
Mechabit4mo ago
you can register to room.connectionStateChanged and when it goes to Error or Disconnected then you'll know something went wrong
Outlawdlaw
Outlawdlaw4mo ago
Thank you