Method: ORTC.enable_presence
- Defined in:
- lib/ortc.rb
.enable_presence(url, is_cluster, application_key, private_key, channel, metadata, &block) ⇒ Object
Enables presence for the specified channel with first 100 unique metadata if true.
Note: This method will send your Private Key over the Internet. Make sure to use secure connection.
-
url - Server containing the presence service
-
is_cluster - Indicates whether the url is in a cluster.
-
application_key - Application key with access to presence service
-
private_key - The private key provided when the ORTC service is purchased.
-
channel - Channel to activate presence.
-
metadata - Defines if to collect first 100 unique metadata.
-
&block - Callback with error and result parameters.
Usage: ORTC.enable_presence(ortc_url, true, ortc_app_key, ortc_private_key, channel, true) { |error, result|
if error.to_s.empty?
puts "result: #{result}"
else
puts "error: #{error}"
end
}
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ortc.rb', line 37 def self.enable_presence(url, is_cluster, application_key, private_key, channel, , &block) if url.to_s.empty? block.call('URL is null or empty', nil) elsif application_key.to_s.empty? block.call('Application Key is null or empty', nil) elsif private_key.to_s.empty? block.call('Private key is null or empty', nil) elsif channel.to_s.empty? block.call('Channel is null or empty', nil) elsif not channel =~ /^[\w\-:\/.]+$/ block.call('Channel has invalid characters', nil) else begin r_thread = Thread.new { server = '' if is_cluster server = _get_cluster(url) begin block.call('Can not connect with the server', nil) r_thread.exit end if server == '' else server = url.clone end server = server << (server.match(/\/$/) ? 'presence' : '/presence') server = server << "/enable/#{application_key}/#{channel}" body = "privatekey=#{private_key}&metadata=" << ( ? '1' : '0') uri = URI.parse(server) begin if http = Net::HTTP.new(uri.host, uri.port) if server.match /^https/ http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end req = Net::HTTP::Post.new(uri.request_uri) req.body = body res = http.request(req) if res.code == '200' block.call(nil, res.body) else Block.call(res.body, nil) end end rescue => e block.call(e, nil) r_thread.exit end } r_thread.run end end end |