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) ->
@clients = 0
@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.
@url = 'https://' + @url if 0 != @url.indexOf('https://', 0)
@url += '/' if '/' != @url.substr -1
@ -61,8 +61,8 @@ class Broker
xhr.send @id
sendAnswer: (answer) ->
log @id + ' - Sending answer back to broker...\n'
log answer.sdp
dbg @id + ' - Sending answer back to broker...\n'
dbg answer.sdp
xhr = new XMLHttpRequest()
try
xhr.open 'POST', @url + 'answer'
@ -74,12 +74,12 @@ class Broker
return if xhr.DONE != xhr.readyState
switch xhr.status
when STATUS_OK
log 'Broker: Successfully replied with answer.'
log xhr.responseText
dbg 'Broker: Successfully replied with answer.'
dbg xhr.responseText
when STATUS_GONE
log 'Broker: No longer valid to reply with answer.'
dbg 'Broker: No longer valid to reply with answer.'
else
log 'Broker ERROR: Unexpected ' + xhr.status +
dbg 'Broker ERROR: Unexpected ' + xhr.status +
' - ' + xhr.statusText
Status.set ' failure. Please refresh.'
xhr.send JSON.stringify(answer)

View file

@ -32,16 +32,15 @@ class ProxyPair
if null == evt.candidate
# TODO: Use a promise.all to tell Snowflake about all offers at once,
# once multiple proxypairs are supported.
log 'Finished gathering ICE candidates.'
dbg 'Finished gathering ICE candidates.'
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
channel = dc.channel
log 'Data Channel established...'
dbg 'Data Channel established...'
@prepareDataChannel channel
@client = channel
@ -52,19 +51,19 @@ class ProxyPair
catch e
log 'Invalid SDP message.'
return false
log 'SDP ' + offer.type + ' successfully received.'
dbg 'SDP ' + offer.type + ' successfully received.'
true
prepareDataChannel: (channel) =>
channel.onopen = =>
log 'Data channel opened!'
log 'WebRTC DataChannel opened!'
snowflake.state = MODE.WEBRTC_READY
ui.setActive true
# This is the point when the WebRTC datachannel is done, so the next step
# is to establish websocket to the server.
@connectRelay()
channel.onclose = ->
log 'Data channel closed.'
log 'WebRTC DataChannel closed.'
ui.setStatus 'disconnected.'
snowflake.state = MODE.INIT
ui.setActive false
@ -75,11 +74,11 @@ class ProxyPair
# Assumes WebRTC datachannel is connected.
connectRelay: =>
log 'Connecting to relay...'
dbg 'Connecting to relay...'
@relay = makeWebsocket @relayAddr
@relay.label = 'websocket-relay'
@relay.onopen = =>
log '\nRelay ' + @relay.label + ' connected!'
log @relay.label + ' connected!'
ui.setStatus 'connected'
@relay.onclose = @onClose
@relay.onerror = @onError
@ -106,13 +105,13 @@ class ProxyPair
onClose: (event) =>
ws = event.target
log(ws.label + ': closed.')
log ws.label + ' closed.'
@flush()
@maybeCleanup()
onError: (event) =>
ws = event.target
log ws.label + ': error.'
log ws.label + ' error.'
@close()
# 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.

View file

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

View file

@ -13,23 +13,35 @@
body {
position: absolute;
width: 100%; height: 100%; top: 0; margin: 0 auto;
background-color: #424;
color: #000;
text-align: center;
font-size: 16px;
font-variant: small-caps;
background-color: #424; color: #000;
font-size: 10px; letter-spacing: 1px; font-variant: small-caps;
text-align: center; cursor: default;
margin: 0; padding: 0;
}
#badge {
margin: auto;
width: 88px; height: 31px;
margin: auto; padding: 0;
width: 88px; height: 16px;
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>
</head>
<body>
<div id="badge">
Snowflake
Internet Freedom
</div>
</body>
</html>

View file

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