Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,42 @@ for more details.
You can also change the layout of an individual stream dynamically. Refer to
[working with Streams](#working-with-streams).

### Force disconnect
### Working with Connections

#### Listing Connections

You can retrieve a list of clients connected to a session using the `OpenTok::Connetions#list` method, passing in the Session ID of the session, for example:

```ruby
connection_list = opentok.connections.list('abcdefg12345')
```

This will return an iterable `ConnectionList` object with `total` and `session_id` getter methods:

```ruby
connection_list.total # => 5
connection_list.session_id # => 'abcdefg12345'
```

The items in the connection list are `Connection` objects, each representing a client connection to the session. You can access data on individual connection objects:

```ruby
connection_1 = connection_list.first
connection_1.connection_id # => "249b7640-1bcf-4f49-8fc7-3d7998ff218b"
connection_1.connection_state # => "Connected"
connection_1.created_at # => 1779287104931
```

#### Force disconnect

You can cause a client to be forced to disconnect from a session by using the
`opentok.connections.forceDisconnect(session_id, connection_id)` method.
`opentok.connections.forceDisconnect(session_id, connection_id)` method and passing in the Session ID and Connection ID.

You can also call the `Connection#force_disconnect` method on a `Connection` object returned when listing connections:

```ruby
connection_1.force_disconnect
```

### Forcing clients in a session to mute published audio

Expand Down
26 changes: 25 additions & 1 deletion lib/opentok/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def create_session(opts)
opts.extend(HashExtensions)
response = self.class.post("/session/create", {
:body => opts.camelize_keys!,
:headers => generate_headers
:headers => generate_headers("Content-Type" => "application/x-www-form-urlencoded")
})
case response.code
when (200..300)
Expand Down Expand Up @@ -411,6 +411,30 @@ def stop_live_captions(captions_id)

# Connections methods

def list_connections(session_id, offset, count)
query = Hash.new
query[:offset] = offset unless offset.nil?
query[:count] = count unless count.nil?
response = self.class.get("/v2/project/#{@api_key}/session/#{session_id}/connection", {
:query => query.empty? ? nil : query,
:headers => generate_headers
})
case response.code
when 200
response
when 400
raise ArgumentError, "Invalid request. This response may indicate that some parameter of your query is invalid."
when 403
raise OpenTokAuthenticationError, "Authentication failed while retrieving connections. API Key: #{@api_key}"
when 404
raise OpenTokConnectionError, "Either the OpenTok session could not be found, or no clients are actively connected to the session."
else
raise OpenTokConnectionError, "The connections could not be retrieved."
end
rescue StandardError => e
raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}"
end

def forceDisconnect(session_id, connection_id)
response = self.class.delete("/v2/project/#{@api_key}/session/#{session_id}/connection/#{connection_id}", {
:headers => generate_headers("Content-Type" => "application/json")
Expand Down
47 changes: 47 additions & 0 deletions lib/opentok/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "active_support/inflector"

module OpenTok
# Represents a connection in an OpenTok session.
#
# @attr [String] connection_id
# The ID of the connection.
#
# @attr [Integer] created_at
# The timestamp when the connection was created, expressed in milliseconds since the Unix epoch.
#
# @attr [String] connection_state
# The state of the connection.
#
class Connection
# @private
def initialize(interface, session_id, json)
@interface = interface
@session_id = session_id
# TODO: validate json fits schema
@json = json
end

# A JSON-encoded string representation of the connection.
def to_json
@json.to_json
end

# Forces the disconnection of this connection from the OpenTok session.
#
# A client must be actively connected to the OpenTok session for you to disconnect it.
def force_disconnect
# TODO: validate returned json fits schema
@json = @interface.forceDisconnect(@session_id, @json['connectionId'])
end

# @private ignore
def method_missing(method, *args, &block)
camelized_method = method.to_s.camelize(:lower)
if @json.has_key? camelized_method and args.empty?
@json[camelized_method]
else
super method, *args, &block
end
end
end
end
18 changes: 18 additions & 0 deletions lib/opentok/connection_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "opentok/connection"

module OpenTok
# A class for accessing an array of Connection objects.
class ConnectionList < Array

# The total number of connections.
attr_reader :total, :session_id

# @private
def initialize(interface, json)
@total = json['count']
@session_id = json['sessionId']
super json['items'].map { |item| Connection.new interface, session_id, item }
end

end
end
23 changes: 22 additions & 1 deletion lib/opentok/connections.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require "opentok/client"
require "opentok/connection"
require "opentok/connection_list"

module OpenTok
# A class for working with OpenTok connections.
class Connections
Expand All @@ -6,6 +10,23 @@ def initialize(client)
@client = client
end

# Returns a ConnectionList, which is an array of connections that are active in a session,
# for your API key.
#
# @param [String] session_id The session ID of the OpenTok session.
# @param [Hash] options A hash with keys defining which range of connections to retrieve.
# @option options [integer] :offset Optional. The index offset of the first connection. If you do not specify an offset, 0 is used.
# @option options [integer] :count Optional. The number of connections to be returned. The maximum
# number of connections returned is 1000. If you do not specify a count, 50 is used.
#
# @return [ConnectionList] A ConnectionList object, which is an array of Connection objects.
def list(session_id, options = {})
raise ArgumentError, "session_id not provided" if session_id.to_s.empty?
raise ArgumentError, "count must be between 1 and 1000" if options[:count] && (options[:count] < 1 || options[:count] > 1000)
connections_list_json = @client.list_connections(session_id, options[:offset], options[:count])
ConnectionList.new self, connections_list_json
end

# Force a client to disconnect from an OpenTok session.
#
# A client must be actively connected to the OpenTok session for you to disconnect it.
Expand All @@ -17,7 +38,7 @@ def initialize(client)
# @raise [OpenTokAuthenticationError] You are not authorized to disconnect the connection. Check your authentication credentials.
# @raise [OpenTokConnectionError] The client specified by the connection_id property is not connected to the session.
#
def forceDisconnect(session_id, connection_id )
def forceDisconnect(session_id, connection_id)
raise ArgumentError, "session_id not provided" if session_id.to_s.empty?
raise ArgumentError, "connection_id not provided" if connection_id.to_s.empty?
response = @client.forceDisconnect(session_id, connection_id)
Expand Down
2 changes: 2 additions & 0 deletions lib/opentok/websocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class WebSocket
# @option opts [Hash] :headers (optional) A hash of key-value pairs of headers to be sent to your WebSocket server with each message,
# with a maximum length of 512 bytes.
# @option opts [Boolean] :bidirectional (optional) Whether the WebSocket connection should be bidirectional.
# @option opts [Integer] :audio_rate (optional) A number representing the audio sampling rate in Hz for the WebSocket stream.
# - Must be one of: 8000, 16000, 24000
def connect(session_id, token, websocket_uri, opts = {})
response = @client.connect_websocket(session_id, token, websocket_uri, opts)
end
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading