Permalink
Browse files

Added browserchannel-based server frontend

  • Loading branch information...
1 parent c2c2d74 commit 35ab3d1cbbd7776e7ca198341bd4186c18f483f7 @josephg committed Nov 13, 2011
Showing with 405 additions and 12 deletions.
  1. +11 −8 src/server/browserchannel.coffee
  2. +5 −1 src/server/index.coffee
  3. +0 −1 src/server/rest.coffee
  4. +387 −0 test/browserchannel.coffee
  5. +1 −2 test/socketio.coffee
  6. +1 −0 tests.coffee
@@ -34,7 +34,7 @@ module.exports = (createClient, options) ->
options or= {}
browserChannel options, (session) ->
- console.log "New BC session from #{session.address} with id #{session.id}"
+ #console.log "New BC session from #{session.address} with id #{session.id}"
data =
headers: session.headers
remoteAddress: session.address
@@ -45,15 +45,15 @@ module.exports = (createClient, options) ->
# To save on network traffic, the client & server can leave out the docName with each message to mean
# 'same as the last message'
- lastSentDocName = null
- lastReceivedDocName = null
+ lastSentDoc = null
+ lastReceivedDoc = null
# Map from docName -> {queue, listener if open}
docState = {}
# We'll only handle one message from each client at a time.
handleMessage = (query) ->
- console.log "Message from #{session.id}", query
+ #console.log "Message from #{session.id}", query
# The client can specify null as the docName to get a random doc name.
if query.doc is null
@@ -111,7 +111,7 @@ module.exports = (createClient, options) ->
# Its invalid to send a message to a closed session. We'll silently drop messages if the
# session has closed.
if session.state isnt 'closed'
- console.log "Sending", response
+ #console.log "Sending", response
session.send response
# Open the given document name, at the requested version.
@@ -296,7 +296,7 @@ module.exports = (createClient, options) ->
# ...
#throw new Error 'No version specified' unless query.v?
- opData = {v:query.v, op:query.op}
+ opData = {v:query.v, op:query.op, meta:query.meta}
client.submitOp query.doc, opData, (error, appliedVersion) ->
msg = if error
@@ -316,9 +316,11 @@ module.exports = (createClient, options) ->
createClient data, (error, client_) ->
if error
# The client is not authorized, so they shouldn't try and reconnect.
- client.stop()
+ session.send {auth:null, error}
+ session.stop()
else
client = client_
+ session.send auth:client.id
# Ok. Now we can handle all the messages in the buffer. They'll go straight to
# handleMessage from now on.
@@ -328,7 +330,8 @@ module.exports = (createClient, options) ->
session.on 'message', handleMessage
session.on 'close', ->
- console.log "Client #{client.id} disconnected"
+ return unless client
+ #console.log "Client #{client.id} disconnected"
for docName, {listener} of docState
client.removeListener docName if listener
docState = null
View
@@ -43,7 +43,11 @@ create.attach = attach = (server, options, model = createModel(options)) ->
# done properly.
server.use rest(createClient, options.rest) if options.rest != null
socketio.attach(server, createClient, options.socketio or {}) if options.socketio != null
- server.use browserChannel(createClient, options.browserChannel) if options.browserChannel != null
+
+ if options.browserChannel != null
+ options.browserChannel ?= {}
+ options.browserChannel.server = server
+ server.use browserChannel(createClient, options.browserChannel)
server
View
@@ -4,7 +4,6 @@
http = require 'http'
sys = require 'sys'
-util = require 'util'
url = require 'url'
connect = require 'connect'
Oops, something went wrong.

0 comments on commit 35ab3d1

Please sign in to comment.