Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:sockjs/sockjs-node

  • Loading branch information...
commit f77bb1e67d87f4c4e548b14edadb4858cbede9b9 2 parents 1e0f856 + 4c34285
Marek majek authored
34 README.md
View
@@ -23,10 +23,11 @@ var http = require('http');
var sockjs = require('sockjs');
var echo = new sockjs.Server(sockjs_opts);
-echo.on('open', function(conn) {
- conn.on('message', function(e) {
- conn.send(e.data);
+echo.on('connection', function(conn) {
+ conn.on('message', function(message) {
+ conn.send(message);
});
+ conn.on('close', function() {});
});
var server = http.createServer();
@@ -168,7 +169,7 @@ constructor.
and emits following event:
<dl>
-<dt>open(connection)</dt>
+<dt>connection(connection)</dt>
<dd>A new connection has been successfully opened.</dd>
</dl>
@@ -200,10 +201,10 @@ A `Connection` instance is also an
and emits following events:
<dl>
-<dt>message(event)</dt>
-<dd>A message arrived on the connection. Data is available at 'event.data'.</dd>
+<dt>message(message)</dt>
+<dd>A message arrived on the connection.</dd>
-<dt>close(event)</dt>
+<dt>close()</dt>
<dd>Connection was closed. This event is triggered exactly once for
every connection.</dd>
</dl>
@@ -211,14 +212,14 @@ and emits following events:
For example:
```javascript
-sjs.on('open', function(conn) {
- console.log('open' + conn);
- conn.on('close', function(e) {
- console.log('close ' + conn, e);
+sjs.on('connection', function(conn) {
+ console.log('connection' + conn);
+ conn.on('close', function() {
+ console.log('close ' + conn);
});
- conn.on('message', function(e) {
+ conn.on('message', function(message) {
console.log('message ' + conn,
- e.data);
+ message);
});
});
```
@@ -236,10 +237,11 @@ var sockjs_opts = {sockjs_url:
"http://sockjs.github.com/sockjs-client/sockjs-latest.min.js"};
var sjs = new sockjs.Server(sockjs_opts);
-sjs.on('open', function(conn) {
- conn.on('message', function(e) {
- conn.send(e.data);
+sjs.on('connection', function(conn) {
+ conn.on('message', function(message) {
+ conn.send(message);
});
+ conn.on('close', function() {});
});
var server = http.createServer();
2  examples/echo/index.html
View
@@ -39,7 +39,7 @@
</form>
<script>
var log;
- var sockjs_url = 'http://127.0.0.1:9999/echo';
+ var sockjs_url = '/echo';
var sockjs = new SockJS(sockjs_url);
sockjs.onopen = function() {
log(' [*] Connected (using: '+sockjs.protocol+')');
6 examples/echo/server.js
View
@@ -6,9 +6,9 @@ var node_static = require('node-static');
var sockjs_opts = {sockjs_url: "http://sockjs.github.com/sockjs-client/sockjs-latest.min.js"};
var sjs_echo = new sockjs.Server(sockjs_opts);
-sjs_echo.on('open', function(conn) {
- conn.on('message', function(e) {
- conn.send(e.data);
+sjs_echo.on('connection', function(conn) {
+ conn.on('message', function(message) {
+ conn.send(message);
});
});
19 src/sockjs.coffee
View
@@ -105,20 +105,21 @@ class ServerInstance extends events.EventEmitter
@app.options = @options
@app.emit = => @emit.apply(@, arguments)
@webjs_handler = webjs.generateHandler(@app, dispatcher)
+ @path_regexp = new RegExp('^' + @options.prefix + '([/].+|[/]?)$')
+
+ handler: (req, res, extra) ->
+ # All urls that match the prefix must be handled by us.
+ if not req.url.match(@path_regexp)
+ return false
+ @webjs_handler(req, res, extra)
+ return true
installHandlers: (http_server) ->
@app.log('debug', 'SockJS v' + sockjs_version() + ' ' +
'bound to ' + JSON.stringify(@options.prefix))
- path_regexp = new RegExp('^' + @options.prefix + '([/].+|[/]?)$')
- handler = (req, res, extra) =>
- # All urls that match the prefix must be handled by us.
- if not req.url.match(path_regexp)
- return false
- @webjs_handler(req, res, extra)
- return true
- utils.overshadowListeners(http_server, 'request', handler)
- utils.overshadowListeners(http_server, 'upgrade', handler)
+ utils.overshadowListeners(http_server, 'request', (a,b,c) => @handler(a,b,c))
+ utils.overshadowListeners(http_server, 'upgrade', (a,b,c) => @handler(a,b,c))
return true
8 src/transport.coffee
View
@@ -29,7 +29,7 @@ class Session extends events.EventEmitter
@to_tref = setTimeout(@timeout_cb, @disconnect_delay)
@emit_open = =>
@emit_open = null
- server.emit('open', @)
+ server.emit('connection', @)
register: (recv) ->
if @recv
@@ -89,20 +89,20 @@ class Session extends events.EventEmitter
if @recv
throw Error('RECV_STILL_THERE')
@readyState = Transport.CLOSED
- @emit('close', {status: 1001, reason: "Session timed out"})
+ @emit('close')
if @session_id
delete MAP[@session_id]
@session_id = null
didMessage: (payload) ->
if @readyState is Transport.OPEN
- @emit('message', {data: payload})
+ @emit('message', payload)
return
send: (payload) ->
if @readyState isnt Transport.OPEN
throw Error('INVALID_STATE_ERR')
- @send_buffer.push( payload )
+ @send_buffer.push('' + payload)
if @recv
@tryFlush()
Please sign in to comment.
Something went wrong with that request. Please try again.