Connecting to a session
Once you have a session ID, and initialized a session object using it, the next step is to connect to the session.
Learn how to connect to sessions below by selecting your platform/language of choice:
Call the connect() method, passing in a token and a completion handler function:
An error object is passed into the completion handler of the connect event when the client fails to connect to the session. Otherwise, no error object is passed in, indicating that the client connected successfully to the session.
The Session object also dispatches a sessionConnected event when the client connects to the session. And the OT object dispatches an exception event when there is an error in connecting. However, it is simpler to check for success in connecting by passing a completion handler into the connect() method, as the last parameter.
When you add an OTSession component it automatically connects to the Vonage Video API session.
Replace your-api-key, your-session-id, and your-session-token with your API key, a session ID, and a token for the session.
Note that you add the OTPublisher and OTSubscriber components and children of the OTSession component.
You can pass an error and sessionConnected event handlers in the OTSession component. The error event handler is called if the client fails to connect to the session. And the sessionConnected event handler is called when the client connects to the session:
Call the Session.connect(token) method, passing in a valid token:
The Session.SessionListener.onConnected(Session session) method is called when the client connects to the Vonage Video session.
The Session.SessionListener.onError(Session session, OpentokError error) method is called when there is an error in connecting. See the documentation for the OpentokException.ErrorCode enum for descriptions of values of the code property of the error object.
Call the OTSession connect(withToken:error:) method, passing in the token for the client:
The OTSessionDelegate sessionDidConnect(_:) message is sent when the client connects to the Vonage Video session.
The OTSessionDelegate session(_:didFailWithError:) is sent when there is an error in connecting.
See the documentation for the OTSessionErrorCode enum for descriptions of values of the code property of the error object.
Monitoring the connection status (Swift)
You can get the connection status by checking the sessionConnectionStatus property of the OTSession object:
Valid values are defined in the OTSessionConnectionStatus enum.
You can use a key-value observer to monitor this property. However, the OTSessionDelegate sessionDidConnect(_:) and OTSessionDelegate sessionDidDisconnect(_:) messages are sent to the session's delegate when the session connects and disconnects.
Call the [OTSession connectWithToken:error:] method, passing in the token for the client:
The [OTSessionDelegate session:didConnect] message is sent when the client connects to the Vonage Video session.
The OTSessionDelegate session:didFailWithError:] is sent when there is an error in connecting. See the documentation for the OTSessionErrorCode enum for descriptions of values of the code property of the error object.
Monitoring the connection status (Objective C)
You can get the connection status by checking the sessionConnectionStatus property of the OTSession object:
Valid values are defined in the OTSessionConnectionStatus enum.
You can use a key-value observer to monitor this property. However, the [OTSessionDelegate sessionDidConnect:] and [OTSessionDelegate sessionDidDisconnect:] messages are sent to the session's delegate when the session connects and disconnects.
Call the Session.connect(token) method, passing in a valid OpenTok token:
The Session.Connected event is sent when the client connects to the OpenTok session.
The Session.Error event is sent when there is an error in connecting:
See the documentation for the OpenTok.ErrorCode enum for descriptions of values of the code property of the error object.
Call the otc_session_connect() function:
This function connects the client to the Vonage Video session. It takes two arguments:
- The
otc_sessionstructure instance. - The Vonage Video token string.
Upon successfully connecting, the on_connected callback function of the otc_session_callbacks struct is called. Upon error, the on_error callback function of the otc_session_callbacks struct is called.