Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix

  • Loading branch information...
commit f15497ed70c569793e16e8c447c618617c421587 1 parent 72562f1
@digplan authored
View
2  js/j.js
@@ -8,6 +8,7 @@ u[o]&&(delete u[o],c?delete n[l]:typeof n.removeAttribute!==i?n.removeAttribute(
// jqhelpers.js
function _render(t, o, cb){
+ if(!t) throw Error('_render No template provided');
var ren = arguments.callee; cb = cb || console.log;
if(!t.match(/{{(#|\^)/)) cb(t.replace(/{{(.*?)}}/g, function(a,b){ return o[b] }));
@@ -83,6 +84,7 @@ $.fn.renderPost = function(t, url, dta){
$.fn.renderSocket = function(t, url){
var th = this;
t = t || th.html();
+ if(!t) t = th.attr('template');
_ws(url, function(e, r){
if(e) throw e;
$(th).render(t, r);
View
27 js/ws.js
@@ -0,0 +1,27 @@
+_render = function(t, o, cb){
+ var ren = arguments.callee; cb = cb || console.log;
+ if(!t.match(/{{(#|\^)/)) cb(t.replace(/{{(.*?)}}/g, function(a,b){ return o[b] }));
+
+ t.replace(/{{(.)(.*?)}}(.*?){{\/\2}}/g, function(match,sym,tag,inner){
+ if('function' === typeof(o[tag])) return o[tag]()(inner);
+ if(sym == '#'){
+ if(o[tag].length){
+ var s=''; o[tag].forEach(function(aritem){
+ s += inner.replace('{{.}}', aritem);
+ })
+ return cb(s);
+ }
+ ren(inner, o[tag]);
+ } else if(sym == '^'){
+ cb(o[tag] ? '' : inner);
+ }
+ })
+}
+
+_ws = function(url, cb){
+ var ws = new WebSocket(url);
+ ws.onmessage = function(m) {
+ var ret = JSON.parse(m.data);
+ cb(ret.e, ret.r, ws);
+ }
+}
View
3  package.json
@@ -19,6 +19,7 @@
"colors": "",
"hogan.js": "",
"socket-server": "",
+ "loghooks-node": "",
"underscore": ""
},
"repository": {
@@ -28,5 +29,5 @@
"bin": {
"quick-server": "quick-server.js"
},
- "version": "0.5.0"
+ "version": "0.5.1"
}
View
8 quick-server.js
@@ -111,7 +111,13 @@ function start(options, fn){
})
}
if(options.debug) console.log(JSON.stringify(debug, null, 4));
- if(options.logport) require('./logger/logger.js')({port: options.logport}, app);
+ if(options.log){
+ var svr = require('socket-server')({port: options.log});
+ require('loghooks-node').stdout(function(s){
+ svr.return(null, s);
+ }, AlsoPrintToStdout=true);
+ }
+ if(options.ws) app.ws = require('./ws.js')({port: options.ws}, app);
if(fn) fn(options, app);
}
View
3  test/index.html
@@ -1,5 +1,4 @@
<script src='j.js'></script>
-<script src='log'></script>
<link rel='stylesheet' href='http://tiny.cc/bootstrapcss'/>
@@ -9,6 +8,8 @@
<a href='/api/render'>Test render</a>
<a href='/api/return'>Test return</a>
<a href='/appcache'>Test appcache</a>
+<a href='/wsh'>Test WS</a>
+<a href='/logh'>Test log</a>
<script>
function _TEST(success, msg) {
View
6 test/logh.html
@@ -0,0 +1,6 @@
+<script>
+ var ws = new WebSocket('ws://localhost:8080');
+ ws.onmessage = function(m) {
+ console.log(JSON.parse(m.data).r);
+ }
+</script>
View
19 test/test.js
@@ -1,4 +1,5 @@
-require('../quick-server.js')({port: 80, logport: 8081}, function(options, app){
+require('../quick-server.js')({port: 80, log:8080, ws:8081}, function(options, app){
+ require('time-ago')(); var now = new Date();
/*
require('appcache-node')({files:[
'j.js', 'http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css'
@@ -8,6 +9,16 @@ require('../quick-server.js')({port: 80, logport: 8081}, function(options, app){
s.render('index.html');
})
+ app.all('/wsh', function(r, s){
+ setInterval(function(){ app.ws.return(null, { time: _timeago(now) }) }, 1000);
+ s.sendfile('wsh.html');
+ })
+
+ app.all('/logh', function(r, s){
+ setInterval(function(){ console.log('log from server OK') }, 4000);
+ s.render('logh.html');
+ })
+
app.all('/api/renderfile', function(r, s){
s.render('render.html', {hello: 'world'});
})
@@ -28,10 +39,4 @@ require('../quick-server.js')({port: 80, logport: 8081}, function(options, app){
s.return(null, r.body);
})
- var sock = require('socket-server')({port:8080});
- var obj = {e: null, r:{some:'thing'}};
-
- setInterval(function(){ sock.sendAll(obj) }, 5000);
-
- setInterval(function(){ console.log('log from server OK') }, 4000);
})
View
5 test/wsh.html
@@ -0,0 +1,5 @@
+<script src='j.js'></script>
+<script src='ws/display'></script>
+
+<div id='display' template='{{time}}'></div>
+
View
18 ws.js
@@ -0,0 +1,18 @@
+module.exports = function(o, a){
+ a.get('/ws/:id', function(r, s){
+ var p = r.param('id');
+ var sc = "$(document).ready(function(){$('#display').renderSocket(null, 'ws://"+r.headers.host+":"+o.port+"')})"
+ s.end(sc);
+ })
+ return require('socket-server')({port: o.port});
+}
+
+/*
+ var svr = require('socket-server')({port: options.port});
+ console.log = (function(of){
+ return function(s){
+ svr.sendAll(s);
+ of(s);
+ }
+ })(console.log)
+*/
Please sign in to comment.
Something went wrong with that request. Please try again.