Snowflake badge works, and animates when serving client (issue #16) and some logging cleanups

This commit is contained in:
Serene Han 2016-02-03 21:37:21 -08:00
parent caba2cc8f8
commit 21c38a04ce
5 changed files with 44 additions and 32 deletions

View file

@ -24,7 +24,7 @@ class Broker
constructor: (@url) -> constructor: (@url) ->
@clients = 0 @clients = 0
@id = genSnowflakeID() @id = genSnowflakeID()
log 'Contacting Broker at ' + @url + '\nSnowflake ID: ' + @id dbg 'Contacting Broker at ' + @url + '\nSnowflake ID: ' + @id
# Ensure url has the right protocol + trailing slash. # Ensure url has the right protocol + trailing slash.
@url = 'https://' + @url if 0 != @url.indexOf('https://', 0) @url = 'https://' + @url if 0 != @url.indexOf('https://', 0)
@url += '/' if '/' != @url.substr -1 @url += '/' if '/' != @url.substr -1
@ -61,8 +61,8 @@ class Broker
xhr.send @id xhr.send @id
sendAnswer: (answer) -> sendAnswer: (answer) ->
log @id + ' - Sending answer back to broker...\n' dbg @id + ' - Sending answer back to broker...\n'
log answer.sdp dbg answer.sdp
xhr = new XMLHttpRequest() xhr = new XMLHttpRequest()
try try
xhr.open 'POST', @url + 'answer' xhr.open 'POST', @url + 'answer'
@ -74,12 +74,12 @@ class Broker
return if xhr.DONE != xhr.readyState return if xhr.DONE != xhr.readyState
switch xhr.status switch xhr.status
when STATUS_OK when STATUS_OK
log 'Broker: Successfully replied with answer.' dbg 'Broker: Successfully replied with answer.'
log xhr.responseText dbg xhr.responseText
when STATUS_GONE when STATUS_GONE
log 'Broker: No longer valid to reply with answer.' dbg 'Broker: No longer valid to reply with answer.'
else else
log 'Broker ERROR: Unexpected ' + xhr.status + dbg 'Broker ERROR: Unexpected ' + xhr.status +
' - ' + xhr.statusText ' - ' + xhr.statusText
Status.set ' failure. Please refresh.' Status.set ' failure. Please refresh.'
xhr.send JSON.stringify(answer) xhr.send JSON.stringify(answer)

View file

@ -32,16 +32,15 @@ class ProxyPair
if null == evt.candidate if null == evt.candidate
# TODO: Use a promise.all to tell Snowflake about all offers at once, # TODO: Use a promise.all to tell Snowflake about all offers at once,
# once multiple proxypairs are supported. # once multiple proxypairs are supported.
log 'Finished gathering ICE candidates.' dbg 'Finished gathering ICE candidates.'
if COPY_PASTE_ENABLED if COPY_PASTE_ENABLED
Signalling.send @pc.localDescription Signalling.send @pc.localDescription
else else
snowflake.broker.sendAnswer @pc.localDescription snowflake.broker.sendAnswer @pc.localDescription
# OnDataChannel triggered remotely from the client when connection succeeds. # OnDataChannel triggered remotely from the client when connection succeeds.
@pc.ondatachannel = (dc) => @pc.ondatachannel = (dc) =>
console.log dc
channel = dc.channel channel = dc.channel
log 'Data Channel established...' dbg 'Data Channel established...'
@prepareDataChannel channel @prepareDataChannel channel
@client = channel @client = channel
@ -52,19 +51,19 @@ class ProxyPair
catch e catch e
log 'Invalid SDP message.' log 'Invalid SDP message.'
return false return false
log 'SDP ' + offer.type + ' successfully received.' dbg 'SDP ' + offer.type + ' successfully received.'
true true
prepareDataChannel: (channel) => prepareDataChannel: (channel) =>
channel.onopen = => channel.onopen = =>
log 'Data channel opened!' log 'WebRTC DataChannel opened!'
snowflake.state = MODE.WEBRTC_READY snowflake.state = MODE.WEBRTC_READY
ui.setActive true ui.setActive true
# This is the point when the WebRTC datachannel is done, so the next step # This is the point when the WebRTC datachannel is done, so the next step
# is to establish websocket to the server. # is to establish websocket to the server.
@connectRelay() @connectRelay()
channel.onclose = -> channel.onclose = ->
log 'Data channel closed.' log 'WebRTC DataChannel closed.'
ui.setStatus 'disconnected.' ui.setStatus 'disconnected.'
snowflake.state = MODE.INIT snowflake.state = MODE.INIT
ui.setActive false ui.setActive false
@ -75,11 +74,11 @@ class ProxyPair
# Assumes WebRTC datachannel is connected. # Assumes WebRTC datachannel is connected.
connectRelay: => connectRelay: =>
log 'Connecting to relay...' dbg 'Connecting to relay...'
@relay = makeWebsocket @relayAddr @relay = makeWebsocket @relayAddr
@relay.label = 'websocket-relay' @relay.label = 'websocket-relay'
@relay.onopen = => @relay.onopen = =>
log '\nRelay ' + @relay.label + ' connected!' log @relay.label + ' connected!'
ui.setStatus 'connected' ui.setStatus 'connected'
@relay.onclose = @onClose @relay.onclose = @onClose
@relay.onerror = @onError @relay.onerror = @onError
@ -106,13 +105,13 @@ class ProxyPair
onClose: (event) => onClose: (event) =>
ws = event.target ws = event.target
log(ws.label + ': closed.') log ws.label + ' closed.'
@flush() @flush()
@maybeCleanup() @maybeCleanup()
onError: (event) => onError: (event) =>
ws = event.target ws = event.target
log ws.label + ': error.' log ws.label + ' error.'
@close() @close()
# we can't rely on onclose_callback to cleanup, since one common error # we can't rely on onclose_callback to cleanup, since one common error
# case is when the client fails to connect and the relay never starts. # case is when the client fails to connect and the relay never starts.

View file

@ -118,7 +118,7 @@ class Snowflake
@retries++ @retries++
recv.then (desc) => recv.then (desc) =>
offer = JSON.parse desc offer = JSON.parse desc
log 'Received:\n\n' + offer.sdp + '\n' dbg 'Received:\n\n' + offer.sdp + '\n'
@receiveOffer offer @receiveOffer offer
, (err) -> , (err) ->
countdown(err, DEFAULT_BROKER_POLL_INTERVAL / 1000) countdown(err, DEFAULT_BROKER_POLL_INTERVAL / 1000)
@ -133,7 +133,7 @@ class Snowflake
sendAnswer: => sendAnswer: =>
next = (sdp) => next = (sdp) =>
log 'webrtc: Answer ready.' dbg 'webrtc: Answer ready.'
@proxyPair.pc.setLocalDescription sdp @proxyPair.pc.setLocalDescription sdp
promise = @proxyPair.pc.createAnswer next promise = @proxyPair.pc.createAnswer next
promise.then next if promise promise.then next if promise
@ -170,7 +170,7 @@ class Snowflake
# Close all existing ProxyPairs and begin finding new clients from scratch. # Close all existing ProxyPairs and begin finding new clients from scratch.
reset: -> reset: ->
@cease() @cease()
log '\nSnowflake resetting...' log 'Snowflake resetting...'
@retries = 0 @retries = 0
@beginWebRTC() @beginWebRTC()
@ -178,7 +178,6 @@ snowflake = null
broker = null broker = null
ui = null ui = null
# Signalling channel - just tells user to copy paste to the peer. # Signalling channel - just tells user to copy paste to the peer.
# Eventually this should go over the broker. # Eventually this should go over the broker.
Signalling = Signalling =
@ -202,9 +201,11 @@ Signalling =
# Log to both console and UI if applicable. # Log to both console and UI if applicable.
log = (msg) -> log = (msg) ->
console.log msg console.log 'Snowflake: ' + msg
ui.log msg ui.log msg
dbg = (msg) -> log msg if ui.debug
init = -> init = ->
ui = new UI() ui = new UI()
log '== snowflake proxy ==' log '== snowflake proxy =='

View file

@ -13,23 +13,35 @@
body { body {
position: absolute; position: absolute;
width: 100%; height: 100%; top: 0; margin: 0 auto; width: 100%; height: 100%; top: 0; margin: 0 auto;
background-color: #424; background-color: #424; color: #000;
color: #000; font-size: 10px; letter-spacing: 1px; font-variant: small-caps;
text-align: center; text-align: center; cursor: default;
font-size: 16px; margin: 0; padding: 0;
font-variant: small-caps;
} }
#badge { #badge {
margin: auto; margin: auto; padding: 0;
width: 88px; height: 31px; width: 88px; height: 16px;
background-image: url('koch.jpg'); background-image: url('koch.jpg');
color: #fff; text-shadow: 0 0 5px #fef;
font-weight: 900;
}
.active {
-webkit-animation: bgScroll 8s linear infinite;
animation: bgScroll 8s linear infinite;
}
@-webkit-keyframes bgScroll {
from {background-position: 49% -4%;}
to {background-position: 49% 104%;}
}
@keyframes bgScroll {
from {background-position: 49% -4%;}
to {background-position: 49% 104%;}
} }
</style> </style>
</head> </head>
<body> <body>
<div id="badge"> <div id="badge">
Snowflake Internet Freedom
</div> </div>
</body> </body>
</html> </html>

View file

@ -37,7 +37,7 @@ class UI
if debug if debug
@$msglog.className = if connected then 'active' else '' @$msglog.className = if connected then 'active' else ''
else else
# magic @$badge.className = if connected then 'active' else ''
# Local input from keyboard into message window. # Local input from keyboard into message window.
acceptInput: => acceptInput: =>