Skip to content

Commit

Permalink
I dont even remember
Browse files Browse the repository at this point in the history
  • Loading branch information
azram19 committed May 25, 2012
1 parent 16e2cda commit ff9ad07
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
7 changes: 3 additions & 4 deletions dev/client/views/lobby.coffee
Expand Up @@ -15,12 +15,12 @@ class LobbyView extends Backbone.View
#Listen to the server #Listen to the server
@communicator.on 'message:new', @handleServerMessage @communicator.on 'message:new', @handleServerMessage


KeyboardJS.bind.key 'enter', @newMessage KeyboardJS.bind.key 'enter', @newMessage, () ->
@$el.find( 'textarea:focus' ).val ''


#Add new message to the chat #Add new message to the chat
addMessage: ( model ) => addMessage: ( model ) =>
console.log "Lobby: New message" console.log "Lobby: New message"
console.debug msg


msg = @messageTemplate message: model.toJSON() msg = @messageTemplate message: model.toJSON()
@$el.find( '.chat ul' ).append msg @$el.find( '.chat ul' ).append msg
Expand Down Expand Up @@ -55,8 +55,7 @@ class LobbyView extends Backbone.View


render: => render: =>
msgs = @messagesTemplate messages: @collection.toJSON() msgs = @messagesTemplate messages: @collection.toJSON()
console.debug msgs

@$el.find( '.chat ul' ).html msgs @$el.find( '.chat ul' ).html msgs


window['LobbyView'] = LobbyView window['LobbyView'] = LobbyView
20 changes: 12 additions & 8 deletions dev/server/communicator.coffee
Expand Up @@ -11,7 +11,9 @@ message passing to and between clients.
class Communicator class Communicator
constructor: ( @app, @channel ) -> constructor: ( @app, @channel ) ->
self = @ self = @
_.extend @, Backbone.Events @com = {}

_.extend @com, Backbone.Events


@connected = 0 @connected = 0
@clients = {} @clients = {}
Expand Down Expand Up @@ -51,11 +53,13 @@ class Communicator
@clients[ client.getId() ] = client @clients[ client.getId() ] = client


socket.on 'message:add', ( data ) -> socket.on 'message:add', ( data ) ->
socket.broadcast.to( socket.getChannel() ).emit 'message:new', data socket.broadcast.to( client.getChannel() ).emit 'message:new', data


#client want to join a channel
socket.on 'join:channel', ( channel, fn ) -> socket.on 'join:channel', ( channel, fn ) ->
data = socket.handshake.session data = socket.handshake.session


#leave a channel user is currently conencted to
@clientLeaveChannel client @clientLeaveChannel client
@clientJoinChannel client, channel @clientJoinChannel client, channel


Expand All @@ -71,6 +75,8 @@ class Communicator


data.currentChannel = null data.currentChannel = null


setInterval @ping, 1000

clientLeaveChannel: ( client ) -> clientLeaveChannel: ( client ) ->
channel = client.getChannel() channel = client.getChannel()


Expand All @@ -90,16 +96,14 @@ class Communicator
clients = @sockets.clients() clients = @sockets.clients()


ping = ( client ) -> ping = ( client ) ->
data = socket.handshake.session
startTime = new Date() startTime = new Date()


client.volatile.emit 'ping', 1, -> client.emit 'ping'
client.once 'pong', ->


#compute the lag, save it #compute the lag
stopTime = new Data() stopTime = new Date()
lag = ( stopTime - startTime ) / 2 lag = ( stopTime - startTime ) / 2
data.lag = lag
data.save()


#inform client about the lag #inform client about the lag
client.emit 'lag', lag client.emit 'lag', lag
Expand Down

0 comments on commit ff9ad07

Please sign in to comment.