Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

working example

  • Loading branch information...
commit 1a6a712de507f25cd552bef4cb08d68d29031564 0 parents
@substack authored
18 LICENSE
@@ -0,0 +1,18 @@
+This software is released under the MIT license:
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 README.markdown
@@ -0,0 +1,23 @@
+shoe
+====
+
+Streaming sockjs for node and the browser.
+
+example
+=======
+
+stream all the things
+---------------------
+
+``` js
+
+```
+
+then with [browserify](https://github.com/substack/node-browserify), generate a
+bundle you can plop into a script tag:
+
+```
+$ browserify client.js -o bundle.js
+```
+
+
48 browser.js
@@ -0,0 +1,48 @@
+var Stream = require('stream');
+var sockjs = require('sockjs-client');
+
+module.exports = function (uri, cb) {
+ if (/^\/\/[^\/]+\//.test(uri)) {
+ uri = window.location.protocol + uri;
+ }
+ else if (!/^https?:\/\//.test(uri)) {
+ uri = window.location.protocol + '//'
+ + window.location.host
+ + (/^\//.test(uri) ? uri : '/' + uri)
+ ;
+ }
+
+ var stream = new Stream;
+ stream.readable = true;
+ stream.writable = true;
+
+ var ready = false;
+ var buffer = [];
+
+ var sock = sockjs(uri);
+ stream.sock = sock;
+
+ stream.write = function (msg) {
+ if (!ready) buffer.push(msg)
+ else sock.send(msg)
+ };
+
+ sock.onopen = function () {
+ if (typeof cb === 'function') cb();
+ ready = true;
+ buffer.forEach(function (msg) {
+ sock.send(msg);
+ });
+ buffer = [];
+ };
+ sock.onmessage = function (e) {
+ stream.emit('data', e.data);
+ };
+ sock.onclose = function () {
+ stream.emit('end');
+ stream.writable = false;
+ stream.readable = false;
+ };
+
+ return stream;
+};
2  example/invert/Makefile
@@ -0,0 +1,2 @@
+all:
+ node_modules/.bin/browserify client.js -o static/bundle.js
14 example/invert/client.js
@@ -0,0 +1,14 @@
+var shoe = require('../../');
+var domready = require('domready');
+var es = require('event-stream');
+
+domready(function () {
+ var result = document.getElementById('result');
+
+ var stream = shoe('/invert');
+ var s = es.mapSync(function (msg) {
+ result.appendChild(document.createTextNode(msg));
+ return String(Number(msg)^1);
+ });
+ s.pipe(stream).pipe(s);
+});
11 example/invert/package.json
@@ -0,0 +1,11 @@
+{
+ "name" : "shoe-stream-example",
+ "version" : "0.0.0",
+ "dependencies" : {
+ "browserify" : "~1.13.0",
+ "ecstatic" : "~0.1.6",
+ "domready" : "~0.2.11",
+ "event-stream" : "~2.0.4"
+ },
+ "private" : true
+}
21 example/invert/server.js
@@ -0,0 +1,21 @@
+var http = require('http');
+var ecstatic = require('ecstatic')(__dirname + '/static');
+var shoe = require('../../');
+var es = require('event-stream');
+
+var server = http.createServer(ecstatic);
+server.listen(9999);
+
+var sock = shoe.createServer();
+sock.on('connection', function (stream) {
+ var iv = setInterval(function () {
+ stream.write(Math.floor(Math.random() * 2));
+ }, 250);
+
+ stream.on('end', function () {
+ clearInterval(iv);
+ });
+
+ stream.pipe(process.stdout, { end : false });
+});
+sock.installHandlers(server, { prefix : '/invert' });
2  example/invert/static/index.html
@@ -0,0 +1,2 @@
+<script src="/bundle.js"></script>
+<div id="result"></div>
1  index.js
@@ -0,0 +1 @@
+module.exports = require('sockjs');
31 package.json
@@ -0,0 +1,31 @@
+{
+ "name" : "shoe",
+ "version" : "0.0.0",
+ "description" : "streaming sockjs for node and the browser",
+ "main" : "index.js",
+ "directories" : {
+ "example" : "example"
+ },
+ "dependencies" : {
+ "sockjs" : "~0.3.1",
+ "sockjs-client" : "git://github.com/substack/sockjs-client.git#browserify-npm"
+ },
+ "repository" : {
+ "type" : "git",
+ "url" : "git://github.com/substack/shoe.git"
+ },
+ "homepage" : "https://github.com/substack/shoe",
+ "keywords" : [
+ "websocket",
+ "stream",
+ "sock",
+ "browserify"
+ ],
+ "author" : {
+ "name" : "James Halliday",
+ "email" : "mail@substack.net",
+ "url" : "http://substack.net"
+ },
+ "license" : "MIT",
+ "engine" : { "node" : ">=0.6" }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.