Permalink
Browse files

Merge pull request #97 from nelsonsilva/sockjs

Added sockjs support
  • Loading branch information...
2 parents c366f99 + 12d72de commit 109b0b90f8e22da2232717ca82ef91ecf4d23fd1 @wmertens wmertens committed Aug 14, 2012
View
@@ -11,6 +11,7 @@
"sharejs-exampleserver": "bin/exampleserver"
},
"dependencies": {
+ "sockjs": ">= 0.3.1",
"socket.io": "~0.8",
"socket.io-client": "~0.8",
"connect": "~1",
@@ -22,7 +23,8 @@
"hat": "*"
},
"devDependencies": {
- "nodeunit": "*"
+ "nodeunit": "*",
+ "websocket": "*"
},
"engine": "node >= 0.6",
"scripts": {
@@ -13,8 +13,7 @@
if WEB?
types = exports.types
- throw new Error 'Must load browserchannel before this library' unless window.BCSocket
- {BCSocket} = window
+ {BCSocket, SockJS} = window
else
types = require '../types'
{BCSocket} = require 'browserchannel'
@@ -32,9 +31,15 @@ class Connection
# - 'disconnected': The connection is closed, but it will not reconnect automatically.
# - 'stopped': The connection is closed, and will not reconnect.
@state = 'connecting'
- @socket = new BCSocket host, reconnect:true
+ @socket =
+ if useSockJS?
+ new SockJS(host)
+ else
+ new BCSocket(host, reconnect:true)
+
@socket.onmessage = (msg) =>
+ msg = JSON.parse(msg.data) if useSockJS?
if msg.auth is null
# Auth failed.
@lastError = msg.error # 'forbidden'
@@ -99,6 +104,7 @@ class Connection
@lastSentDoc = docName
#console.warn 'c->s', data
+ data = JSON.stringify(data) if useSockJS?
@socket.send data
disconnect: ->
@@ -132,6 +138,11 @@ class Connection
open: (docName, type, callback) ->
return callback 'connection closed' if @state is 'stopped'
+ # Wait for the connection to open
+ if @state is 'connecting'
+ @on 'handshaking', -> @open(docName, type, callback)
+ return
+
if typeof type is 'function'
callback = type
type = 'text'
View
@@ -10,7 +10,13 @@
# Note that anything declared in the global scope here is shared with other files
# built by closure. Be careful what you put in this namespace.
-unless WEB?
+
+if WEB?
+ hasBCSocket = window.BCSocket isnt undefined
+ hasSockJS = window.SockJS isnt undefined
+ throw new Error 'Must load socks or browserchannel before this library' unless hasBCSocket or hasSockJS
+ useSockJS = hasSockJS and !hasBCSocket
+else
Connection = require('./connection').Connection
# Open a document with the given name. The connection is created implicitly and reused.
@@ -25,7 +31,9 @@ exports.open = do ->
getConnection = (origin) ->
if WEB?
location = window.location
- origin ?= "#{location.protocol}//#{location.host}/channel"
+ # default to browserchannel
+ path = if useSockJS then 'sockjs' else 'channel'
+ origin ?= "#{location.protocol}//#{location.host}/#{path}"
unless connections[origin]
c = new Connection origin
Oops, something went wrong.

0 comments on commit 109b0b9

Please sign in to comment.