Skip to content

Commit

Permalink
almost working
Browse files Browse the repository at this point in the history
  • Loading branch information
Contra committed Feb 12, 2013
1 parent 2327ee9 commit 92d4d6f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/Channel.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class Channel

joinChannel: ->
if @socket
@joined = false
@socket.write
type: 'join'
channel: @name
Expand Down Expand Up @@ -55,6 +54,8 @@ class Channel
removeSocketListener: (listener) ->
return @ unless @listeners
@listeners = (l for l in @listeners when l isnt listener)
@emit 'unjoin', listener
@realEmit 'unjoin', listener
return @

removeListener: (event, listener) =>
Expand Down
3 changes: 1 addition & 2 deletions lib/Client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ client =
start: ->
@channels = {}
@on "reconnected", =>
for name, chan in @channels
console.log name, chan
for name, chan of @channels
chan.joinChannel()

channel: (name) ->
Expand Down
6 changes: 2 additions & 4 deletions lib/Server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ module.exports =
when 'join'
# TODO: Pass an eventemitter instead of socket
chan.listeners.push socket
socket.on 'close', ->
socket.on 'close', ->
chan.removeSocketListener socket
chan.realEmit 'unjoin', socket
chan.realEmit 'join', socket
socket.write
type: 'joined'
channel: msg.channel
when 'unjoin'
chan.removeSocketListener socket
chan.realEmit 'unjoin', socket
chan.removeSocketListener socket
40 changes: 36 additions & 4 deletions test/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ getClient = (server) ->
host: server.server.httpServer.address().address
port: server.server.httpServer.address().port
resource: server.options.resource
transports: ["websocket"]

describe 'Pulsar', ->
describe 'channels', ->
it 'should add', (done) ->
serv = getServer()
channel = serv.channel 'test'
should.exist channel
serv.destroy()
done()

it 'should emit join on server', (done) ->
Expand All @@ -31,6 +33,8 @@ describe 'Pulsar', ->
cchan = client.channel 'test'

channel.on 'join', (socket) ->
serv.destroy()
client.destroy()
done()

it 'should emit join on client', (done) ->
Expand All @@ -41,7 +45,10 @@ describe 'Pulsar', ->
client = getClient serv
cchan = client.channel 'test'

cchan.on 'join', -> done()
cchan.on 'join', ->
serv.destroy()
client.destroy()
done()

it 'should call ready on client', (done) ->
serv = getServer()
Expand All @@ -50,7 +57,10 @@ describe 'Pulsar', ->

client = getClient serv
cchan = client.channel 'test'
cchan.ready -> done()
cchan.ready ->
serv.destroy()
client.destroy()
done()

it 'should call', (done) ->
serv = getServer()
Expand All @@ -65,6 +75,8 @@ describe 'Pulsar', ->
cchan.emit 'ping', 2
cchan.on 'pong', (num) ->
num.should.equal 2
serv.destroy()
client.destroy()
done()

it 'should call reverse', (done) ->
Expand All @@ -73,6 +85,8 @@ describe 'Pulsar', ->
should.exist channel
channel.on 'pong', (num) ->
num.should.equal 2
serv.destroy()
client.destroy()
done()

client = getClient serv
Expand All @@ -93,6 +107,8 @@ describe 'Pulsar', ->
cchan.ready ->
channel.listeners.length.should.equal 1
channel.listeners[0].id.should.equal cchan.socket.id
serv.destroy()
client.destroy()
done()

it 'should unjoin on close', (done) ->
Expand All @@ -104,8 +120,10 @@ describe 'Pulsar', ->
cchan.ready ->
channel.listeners.length.should.equal 1
channel.listeners[0].id.should.equal cchan.socket.id
channel.listeners[0].on 'close', ->
channel.listeners[0].once 'close', ->
channel.listeners.length.should.equal 0
serv.destroy()
client.destroy()
done()
client.disconnect()

Expand All @@ -116,6 +134,7 @@ describe 'Pulsar', ->
channel2 = serv.channel 'test2'
should.exist channel
should.exist channel2
serv.destroy()
done()

it 'should call', (done) ->
Expand Down Expand Up @@ -144,6 +163,8 @@ describe 'Pulsar', ->
cchan2.emit 'ping', 3
cchan2.on 'pong', (num) ->
num.should.equal 3
serv.destroy()
client.destroy()
done()

describe 'multiple clients', ->
Expand All @@ -166,6 +187,8 @@ describe 'Pulsar', ->

cchan.on 'pong', (num) ->
num.should.equal 2
serv.destroy()
client.destroy()
done()

cchan.emit 'ping', 2
Expand All @@ -175,6 +198,7 @@ describe 'Pulsar', ->
serv = getServer()
channel = serv.channel 'test'
channel.use ->
serv.destroy()
done()

it 'should call', (done) ->
Expand All @@ -192,6 +216,8 @@ describe 'Pulsar', ->
channel.on 'ping', (num) ->
num.should.equal 2
called.should.be.true
serv.destroy()
client.destroy()
done()
client = getClient serv
cchan = client.channel 'test'
Expand All @@ -202,6 +228,7 @@ describe 'Pulsar', ->
serv = getServer()
channel = serv.channel 'test'
channel.use (emit, event, num) ->
console.log event
should.exist emit
should.exist event
should.exist num
Expand All @@ -212,6 +239,8 @@ describe 'Pulsar', ->
channel.on 'ping', (num) ->
num.should.equal 3
called.should.be.true
serv.destroy()
client.destroy()
done()
client = getClient serv
cchan = client.channel 'test'
Expand All @@ -238,15 +267,18 @@ describe 'Pulsar', ->
cchan = client.channel 'test'
cchan2 = client.channel 'test2'
client.once "connected", ->
client.disconnect()
cchan.ready ->
cchan2.ready ->
client.disconnect()

cchan.emit 'ping', 2
cchan.on 'pong', (num) ->
num.should.equal 2

cchan2.emit 'ping', 3
cchan2.on 'pong', (num) ->
num.should.equal 3
serv.destroy()
client.destroy()
done()

0 comments on commit 92d4d6f

Please sign in to comment.