Creación de sesiones
Visión general
Una sesión de Vonage Video es donde los usuarios se conectan para interactuar en tiempo real. Tu servidor de aplicaciones genera la sesión para que los clientes puedan unirse.
Este tutorial repasará:
- Crear una sesión
Antes de empezar
Como se mencionó anteriormente, necesitará un servidor de aplicaciones en ejecución para crear una sesión.
Crear una sesión
Al crear una sesión, hay algunas opciones que puede especificar en función de sus necesidades. Se trata del modo multimedia, la preferencia de archivo y la sugerencia de ubicación. Encontrará más información sobre estas opciones en la sección Crear una guía de sesión.
// Create a session that will attempt to transmit streams directly between
// clients. If clients cannot connect, the session uses the Vonage TURN server:
try {
const session = await vonage.video.createSession();
// save the sessionId
db.save("session", session.sessionId, done);
} catch(error) {
console.error("Error creating session: ", error);
}
// The session will use the Vonage Media Router:
try {
const session = await vonage.video.createSession({ mediaMode: MediaMode.ROUTED });
// save the sessionId
db.save("session", session.sessionId, done);
} catch(error) {
console.error("Error creating session: ", error);
}
// A Session with a location hint
try {
const session = await vonage.video.createSession({ location: "12.34.56.78" });
// save the sessionId
db.save("session", session.sessionId, done);
} catch(error) {
console.error("Error creating session: ", error);
}
// A Session with an automatic archiving
try {
const session = await vonage.video.createSession({ mediaMode: MediaMode.ROUTED, archiveMode: "always" });
// save the sessionId
db.save("session", session.sessionId, done);
} catch(error) {
console.error("Error creating session: ", error);
}
// A session that attempts to stream media directly between clients:
CreateSessionResponse session = videoClient.createSession();
// A session that uses the Vonage Video Media Router:
CreateSessionResponse session = videoClient.createSession(
CreateSessionRequest.builder().mediaMode(MediaMode.ROUTED).build()
);
// A Session with a location hint:
CreateSessionResponse session = videoClient.createSession(
CreateSessionRequest.builder().location("12.34.56.78").build()
);
// A session that is automatically archived (it must used the routed media mode)
CreateSessionResponse session = videoClient.createSession(
CreateSessionRequest.builder()
.mediaMode(MediaMode.ROUTED)
.archiveMode(ArchiveMode.ALWAYS)
.build()
);
// Store this sessionId in the database for later use:
String sessionId = session.getSessionId();
// Create a session that will attempt to transmit streams directly between clients
Result<CreateSessionRequest> request = CreateSessionRequest.Default;
// Or create a custom request
Result<CreateSessionRequest> request = CreateSessionRequest.Build()
.WithLocation("192.168.1.1")
.WithMediaMode(MediaMode.Routed)
.WithArchiveMode(ArchiveMode.Always)
.Create();
// Send the request to the API
Result<CreateSessionResponse> response = await client.SessionClient.CreateSessionAsync(request);
from vonage_video.models import VideoSession
# Create a session
session_info: VideoSession = vonage_client.video.create_session()
# A session that doesn't use the Vonage Media Router and instead sends
# information between peers directly
from vonage_video.models import SessionOptions
options = SessionOptions(media_mode='relayed')
session_info = vonage_client.video.create_session(options)
# An automatically archived session:
options = SessionOptions(media_mode='routed', archive_mode='always')
session_info = vonage_client.video.create_session(options)
# A session with a location hint
options = SessionOptions(location='12.34.56.78')
session_info = vonage_client.video.create_session(options)
# Extract the session ID to be stored in a database
session_id = session_info.session_id
Además de los SDK de servidor, se puede crear una sesión realizando una llamada API a un endpoint. Encontrará más información en la sección Referencia de Video API de Vonage
Se devolverá el identificador de sesión, que podrá pasarse al cliente para que los usuarios puedan conectarse e interactuar con sus tokens.