Skip to content

Commit

Permalink
Add examples/websocket-server-push.js
Browse files Browse the repository at this point in the history
  • Loading branch information
hns committed Apr 13, 2013
1 parent 1a753a8 commit fce5700
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions examples/websocket-server-push.js
@@ -0,0 +1,38 @@
// Simple websocket server demo
var response = require("ringo/jsgi/response");
var arrays = require("ringo/utils/arrays");

var connections = [];

// Schedule an interval function that periodically broadcasts the number of open connections
setInterval(function() {
connections.forEach(function(conn) {
conn.send((connections.length - 1) + " other connection(s) open");
});
}, 5000)

exports.app = function(req) {
return response.static(module.resolve("html/websocket.html"), "text/html");
};

function onconnect(conn) {
conn.addListener("open", function() {
connections.push(conn);
console.info("Opening connection, " + connections.length + " open");
});
conn.addListener("message", function(message) {
connections.forEach(function(conn) {
conn.send(message);
});
console.info("Sending message");
});
conn.addListener("close", function() {
arrays.remove(connections, conn);
console.info("Closing connection, " + connections.length + " remaining");
})
}

if (require.main == module) {
var server = require("ringo/httpserver").main(module.id);
server.getDefaultContext().addWebSocket("/websocket", onconnect);
}

0 comments on commit fce5700

Please sign in to comment.