Skip to content

Commit

Permalink
Merge branch 'release/0.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownmoon committed Oct 20, 2016
2 parents 942fc88 + 6237013 commit 55937b6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,15 @@ In the example below, `__debug` and `__log` are registered to the logger, with d
__debug(location);
__log(location);
</scirpt>
```

Newly add `from` in query, for providing an id other than the random socket id. (Note that socket id will be used if `from` is ommitted.)

```html
<script src="http://localhost:9528/api/v0/socket-inject?func=__debug&func=__log&from=alice"></script>
<scirpt>
// how to use the client logging functions
__debug(location);
__log(location);
</scirpt>
```
21 changes: 19 additions & 2 deletions client-demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,25 @@
</section>
<footer>by Unknown Moon</footer>
</article>
<script src="http://localhost:9528/api/v0/socket-inject?func=__debug&func=__log"></script>
<script>
+ function() {
var url = 'http://localhost:9528/api/v0/socket-inject?func=__debug&func=__log&from=r-' + Math.round(100 * Math.random());
var scriptElm = document.createElement('script');
scriptElm.src = url;
document.body.appendChild(scriptElm);
scriptElm.addEventListener('load', function() {
var sendBtn = document.getElementById('send-msg-btn');
var msgField = document.getElementById('message');
sendBtn.addEventListener('click', function(evt) {
evt.preventDefault();
__log(msgField.value);
});
});
}();
</script>
<!--<script src="http://localhost:9528/api/v0/socket-inject?func=__debug&func=__log&from=l24"></script>-->
<!--<script src="http://localhost:9528/api/v0/socket-inject?func=__debug&func=__log"></script>-->
<!--<script>
+ function() {
var sendBtn = document.getElementById('send-msg-btn');
var msgField = document.getElementById('message');
Expand All @@ -47,7 +64,7 @@
__log(msgField.value);
});
}();
</script>
</script>-->
</body>

</html>
16 changes: 10 additions & 6 deletions lib/client-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,36 @@ function genClientInjectScript(opts) {
const host = opts.host || 'localhost';
const func = opts.func;
const funcSnippets = _.map(func, function(fnName) {
return genFuncSnippets(fnName);
return genFuncSnippets(fnName, opts);
}).join(';');

return `
${genSetupSnippets(host, func)}
${genSetupSnippets(host, func, opts)}
${funcSnippets}
`;
}

function genSetupSnippets(host, func) {
function genSetupSnippets(host, func, opts = {}) {
const who = opts['from'] ? '\'' + opts['from'] + '\'' : 'socket.id';
return `
var socket = io('${host}');
socket.on('connect', function() {
socket.emit('setup', {
func: JSON.parse('${JSON.stringify(func)}')
func: JSON.parse('${JSON.stringify(func)}'),
'from': ${who},
time: Date.now()
});
});
`;
}

function genFuncSnippets(funcName) {
function genFuncSnippets(funcName, opts = {}) {
const who = opts['from'] ? '\'' + opts['from'] + '\'' : 'socket.id';
return `
window['${funcName}'] = function (msgOrObj) {
socket.emit('${funcName}', {
'from': socket.id,
'from': ${who},
time: Date.now(),
msg: JSON.stringify(msgOrObj)
});
Expand Down
16 changes: 13 additions & 3 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ function apiHander(req, res) {
res.writeHead(200);
res.end(clientScript.genScript({
host,
func: [].concat(parsedQuery.func || '__debug')
func: [].concat(parsedQuery.func || '__debug'),
'from': parsedQuery['from'] === undefined ? parsedQuery['from'] : '' + parsedQuery['from']
}));

} else {
Expand All @@ -76,7 +77,12 @@ function ioConnectionHandler(socket) {

socket.emit('who r u');
socket.on('setup', function(data) {
console.log(chalk.underline(`[${_u.getTimeStr(Date.now())}][${socket.id}] on setup: `));
// colourful hightlight the `from`
let render = _cr.getRender(socket.id);
let who = data['from'] ? data['from'] : socket.id;
let time = _u.getTimeStr(data.time || Date.now());
console.log(chalk.underline(`[${time}][${chalk.bold(render(who))}] on setup: `));
render.destroy();

if (data && _.isArray(data.func)) {
_.each(data.func, function(funcName) {
Expand Down Expand Up @@ -115,7 +121,11 @@ function setupSocketHandler(funcName, socket, renderFn) {
parsedMsg = msg;
}
console.log(renderFn(`[${_u.getTimeStr(time)}][${who}][${funcName}]:`));
_u.inspect(parsedMsg);
if (typeof parsedMsg === 'object') {
_u.inspect(parsedMsg);
} else {
console.log(renderFn(parsedMsg));
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tool-socketio-logger",
"version": "0.0.1",
"version": "0.0.2",
"description": "A simple as-it-is message logger powered by socket.io",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 55937b6

Please sign in to comment.