openvidu-basic-ruby to LiveKit
This commit is contained in:
parent
a154f555b3
commit
a6e9dba601
@ -3,4 +3,5 @@ source 'https://rubygems.org'
|
||||
gem 'sinatra'
|
||||
gem 'sinatra-cors'
|
||||
gem 'faraday'
|
||||
gem 'json'
|
||||
gem 'json'
|
||||
gem 'livekit-server-sdk'
|
||||
@ -1,24 +1,36 @@
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
faraday (2.7.4)
|
||||
base64 (0.1.1)
|
||||
faraday (2.7.11)
|
||||
base64
|
||||
faraday-net_http (>= 2.0, < 3.1)
|
||||
ruby2_keywords (>= 0.0.4)
|
||||
faraday-net_http (3.0.2)
|
||||
google-protobuf (3.24.4-x86_64-linux)
|
||||
json (2.6.3)
|
||||
jwt (2.7.1)
|
||||
livekit-server-sdk (0.5.6)
|
||||
google-protobuf (>= 3.21.0, < 4.0)
|
||||
jwt (>= 2.2.3, < 3.0)
|
||||
rack (>= 2.2.3, < 3.0)
|
||||
twirp (>= 1.10.0, < 2.0)
|
||||
mustermann (3.0.0)
|
||||
ruby2_keywords (~> 0.0.1)
|
||||
rack (2.2.6.4)
|
||||
rack-protection (3.0.5)
|
||||
rack
|
||||
rack (2.2.8)
|
||||
rack-protection (3.1.0)
|
||||
rack (~> 2.2, >= 2.2.4)
|
||||
ruby2_keywords (0.0.5)
|
||||
sinatra (3.0.5)
|
||||
sinatra (3.1.0)
|
||||
mustermann (~> 3.0)
|
||||
rack (~> 2.2, >= 2.2.4)
|
||||
rack-protection (= 3.0.5)
|
||||
rack-protection (= 3.1.0)
|
||||
tilt (~> 2.0)
|
||||
sinatra-cors (1.2.0)
|
||||
tilt (2.1.0)
|
||||
tilt (2.3.0)
|
||||
twirp (1.10.0)
|
||||
faraday (< 3)
|
||||
google-protobuf (~> 3.0, >= 3.7.0)
|
||||
|
||||
PLATFORMS
|
||||
x86_64-linux
|
||||
@ -26,8 +38,9 @@ PLATFORMS
|
||||
DEPENDENCIES
|
||||
faraday
|
||||
json
|
||||
livekit-server-sdk
|
||||
sinatra
|
||||
sinatra-cors
|
||||
|
||||
BUNDLED WITH
|
||||
2.4.10
|
||||
2.4.12
|
||||
|
||||
@ -4,12 +4,14 @@ require 'sinatra'
|
||||
require 'sinatra/cors'
|
||||
require 'faraday'
|
||||
require 'json'
|
||||
require 'livekit'
|
||||
require './env.rb'
|
||||
|
||||
# Load env variables
|
||||
SERVER_PORT = ENV['SERVER_PORT']
|
||||
OPENVIDU_URL = ENV['OPENVIDU_URL']
|
||||
OPENVIDU_SECRET = ENV['OPENVIDU_SECRET']
|
||||
LIVEKIT_URL = ENV['LIVEKIT_URL']
|
||||
LIVEKIT_API_KEY = ENV['LIVEKIT_API_KEY']
|
||||
LIVEKIT_API_SECRET = ENV['LIVEKIT_API_SECRET']
|
||||
|
||||
set :port, SERVER_PORT
|
||||
|
||||
@ -18,47 +20,26 @@ set :allow_origin, "*"
|
||||
set :allow_methods, "POST,OPTIONS"
|
||||
set :allow_headers, "content-type"
|
||||
|
||||
post '/api/sessions' do
|
||||
begin
|
||||
body = request.body.read
|
||||
response = Faraday.post do |req|
|
||||
req.url "#{OPENVIDU_URL}openvidu/api/sessions"
|
||||
req.headers['Content-Type'] = 'application/json'
|
||||
req.headers['Authorization'] = "Basic #{Base64.encode64("OPENVIDUAPP:#{OPENVIDU_SECRET}").strip}"
|
||||
req.body = body
|
||||
end
|
||||
if response.success?
|
||||
(JSON.parse response.body)['sessionId']
|
||||
else
|
||||
if response.status == 409
|
||||
# Session already exists in OpenVidu
|
||||
(JSON.parse body)['customSessionId']
|
||||
else
|
||||
status response.status
|
||||
body response.body
|
||||
end
|
||||
end
|
||||
rescue Faraday::Error => err
|
||||
err.response
|
||||
end
|
||||
end
|
||||
post '/token' do
|
||||
|
||||
post '/api/sessions/:sessionId/connections' do
|
||||
begin
|
||||
body = request.body.read
|
||||
response = Faraday.post do |req|
|
||||
req.url "#{OPENVIDU_URL}openvidu/api/sessions/#{params['sessionId']}/connection"
|
||||
req.headers['Content-Type'] = 'application/json'
|
||||
req.headers['Authorization'] = "Basic #{Base64.encode64("OPENVIDUAPP:#{OPENVIDU_SECRET}").strip}"
|
||||
req.body = body
|
||||
end
|
||||
if response.success?
|
||||
(JSON.parse response.body)['token']
|
||||
else
|
||||
status response.status
|
||||
body response.body
|
||||
end
|
||||
rescue Faraday::Error => err
|
||||
err.response
|
||||
content_type :json
|
||||
|
||||
body = JSON.parse(request.body.read)
|
||||
room_name = body['roomName']
|
||||
participant_name = body['participantName']
|
||||
|
||||
if room_name.nil? || participant_name.nil?
|
||||
status 400
|
||||
return 'roomName and participantName are required'
|
||||
end
|
||||
end
|
||||
|
||||
# By default, a token expires after 6 hours.
|
||||
# You may override this by passing in ttl when creating the token. ttl is expressed in seconds.
|
||||
token = LiveKit::AccessToken.new(api_key: LIVEKIT_API_KEY, api_secret: LIVEKIT_API_SECRET)
|
||||
token.identity = participant_name
|
||||
token.name = participant_name
|
||||
token.add_grant(roomJoin: true, room: room_name)
|
||||
token.metadata = { "livekitUrl" => LIVEKIT_URL}
|
||||
|
||||
token.to_jwt
|
||||
end
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
ENV['SERVER_PORT'] = '5000'
|
||||
ENV['OPENVIDU_URL'] = 'http://localhost:4443/'
|
||||
ENV['OPENVIDU_SECRET'] = 'MY_SECRET'
|
||||
ENV['LIVEKIT_URL'] = 'ws://localhost:7880/'
|
||||
ENV['LIVEKIT_API_KEY'] = 'devkey'
|
||||
ENV['LIVEKIT_API_SECRET'] = 'secret'
|
||||
Loading…
x
Reference in New Issue
Block a user