answer successfully roundtripped back from snowflake proxy through broker to client (#1)

This commit is contained in:
Serene Han 2016-01-21 13:02:46 -08:00
parent 7081e6328c
commit c9013b2f80
5 changed files with 127 additions and 70 deletions

View file

@ -6,19 +6,25 @@ to get assigned to clients.
###
STATUS_OK = 200
STATUS_GONE = 410
STATUS_GATEWAY_TIMEOUT = 504
genSnowflakeID = ->
Math.random().toString(36).substring(2)
# Represents a broker running remotely.
class Broker
clients: 0
id: null
# When interacting with the Broker, snowflake must generate a unique session
# ID so the Broker can keep track of which signalling channel it's speaking
# to.
constructor: (@url) ->
log 'Using Broker at ' + @url
clients = 0
@clients = 0
@id = genSnowflakeID()
log 'Contacting Broker at ' + @url + '\nSnowflake ID: ' + @id
# Snowflake registers with the broker using an HTTP POST request, and expects
# a response from the broker containing some client offer.
@ -27,7 +33,8 @@ class Broker
new Promise (fulfill, reject) =>
xhr = new XMLHttpRequest()
try
xhr.open 'POST', @url
xhr.open 'POST', @url + 'proxy'
xhr.setRequestHeader('X-Session-ID', @id)
catch err
###
An exception happens here when, for example, NoScript allows the domain
@ -47,10 +54,29 @@ class Broker
else
log 'Broker ERROR: Unexpected ' + xhr.status +
' - ' + xhr.statusText
xhr.send 'snowflake-testing'
log "Broker: polling for client offer..."
xhr.send @id
log @id + " - polling for client offer..."
sendAnswer: (answer) ->
log 'Sending answer to broker.'
log answer
log @id + ' - Sending answer back to broker...\n'
log answer.sdp
xhr = new XMLHttpRequest()
try
xhr.open 'POST', @url + 'answer'
xhr.setRequestHeader('X-Session-ID', @id)
catch err
log 'Broker: exception while connecting: ' + err.message
return
xhr.onreadystatechange = ->
return if xhr.DONE != xhr.readyState
log xhr
switch xhr.status
when STATUS_OK
log 'Broker: Successfully replied with answer.'
log xhr.responseText
when STATUS_GONE
log 'Broker: No longer valid to reply with answer.'
else
log 'Broker ERROR: Unexpected ' + xhr.status +
' - ' + xhr.statusText
xhr.send JSON.stringify(answer)

View file

@ -32,7 +32,10 @@ class ProxyPair
# TODO: Use a promise.all to tell Snowflake about all offers at once,
# once multiple proxypairs are supported.
log 'Finished gathering ICE candidates.'
Signalling.send @pc.localDescription
if COPY_PASTE_ENABLED
Signalling.send @pc.localDescription
else
snowflake.broker.sendAnswer @pc.localDescription
# OnDataChannel triggered remotely from the client when connection succeeds.
@pc.ondatachannel = (dc) =>
console.log dc

View file

@ -8,7 +8,7 @@ Assume that the webrtc client plugin is always the offerer, in which case
this must always act as the answerer.
###
DEFAULT_WEBSOCKET = '192.81.135.242:9901'
DEFAULT_BROKER = 'https://snowflake-reg.appspot.com/proxy'
DEFAULT_BROKER = 'https://snowflake-reg.appspot.com/'
COPY_PASTE_ENABLED = false
DEFAULT_PORTS =
http: 80
@ -104,8 +104,8 @@ class Snowflake
poll = =>
recv = broker.getClientOffer()
recv.then((desc) =>
log 'Received:\n\n' + desc + '\n'
offer = JSON.parse desc
log 'Received:\n\n' + offer.sdp + '\n'
@receiveOffer offer
, (err) ->
log err
@ -113,13 +113,6 @@ class Snowflake
)
poll()
# if @proxyPairs.length >= MAX_NUM_CLIENTS * CONNECTIONS_PER_CLIENT
# setTimeout(@proxyMain, @broker_poll_interval * 1000)
# return
# params = [['r', '1']]
# params.push ['transport', 'websocket']
# params.push ['transport', 'webrtc']
# Receive an SDP offer from client plugin.
receiveOffer: (desc) =>
sdp = new RTCSessionDescription desc