Method: OpenTok::OpenTok#create_session
- Defined in:
- lib/opentok/opentok.rb
#create_session(opts = {}) ⇒ Session
Creates a new OpenTok session and returns the session ID, which uniquely identifies the session.
For example, when using the OpenTok JavaScript library, use the session ID when calling the OT.initSession()</a> method (to initialize an OpenTok session).
OpenTok sessions do not expire. However, authentication tokens do expire (see the generateToken() method). Also note that sessions cannot explicitly be destroyed.
A session ID string can be up to 255 characters long.
Calling this method results in an OpenTokException in the event of an error. Check the error message for details.
You can also create a session using the OpenTok REST API (see www.tokbox.com/opentok/api/#session_id_production) or at your OpenTok account page.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/opentok/opentok.rb', line 165 def create_session(opts={}) # normalize opts so all keys are symbols and only include valid_opts valid_opts = [ :media_mode, :location, :archive_mode, :archive_name, :archive_resolution, :e2ee ] opts = opts.inject({}) do |m,(k,v)| if valid_opts.include? k.to_sym m[k.to_sym] = v end m end # keep opts around for Session constructor, build REST params params = opts.clone # validate input combinations raise ArgumentError, "A session with always archive mode must also have the routed media mode." if (params[:archive_mode] == :always && params[:media_mode] == :relayed) raise ArgumentError, "A session with relayed media mode should not have e2ee set to true." if (params[:media_mode] == :relayed && params[:e2ee] == true) raise ArgumentError, "A session with always archive mode must not have e2ee set to true." if (params[:archive_mode] == :always && params[:e2ee] == true) # anything other than :relayed sets the REST param to "disabled", in which case we force # opts to be :routed. if we were more strict we could raise an error when the value isn't # either :relayed or :routed if params.delete(:media_mode) == :routed params["p2p.preference"] = "disabled" else params["p2p.preference"] = "enabled" opts[:media_mode] = :relayed end # location is optional, but it has to be an IP address if specified at all unless params[:location].nil? raise "location must be an IPv4 address" unless params[:location] =~ Resolv::IPv4::Regex end # archive mode is optional, but it has to be one of the valid values if present unless params[:archive_mode].nil? raise "archive mode must be either always or manual" unless ARCHIVE_MODES.include? params[:archive_mode].to_sym raise ArgumentError, "archive name and/or archive resolution must not be set if archive mode is manual" if params[:archive_mode] == :manual && (params[:archive_name] || params[:archive_resolution]) end response = client.create_session(params) Session.new api_key, api_secret, response['sessions']['Session']['session_id'], opts end |