Skip to content

Instantly share code, notes, and snippets.

mocheng / storm with publish
Last active Aug 29, 2015
storm mosca server
View storm with publish
var mqtt = require('mqtt');
var port = 1883;
var host = 'localhost';
var options = {
username: 'my_username',
password: 'my_password',
clientId: 'my_client_id'
var client = mqtt.createClient(port, host, options);
View gist:aa87a28adfb00220f169
mkdir -p ~/Downloads
cd ~/Downloads
sudo yum -y install zlib-devel openssl-devel cpio expat-devel gettext-devel curl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
wget -O v1.8.1.2.tar.gz
tar -xzvf ./v1.8.1.2.tar.gz
cd git-
mocheng / 0_reuse_code.js
Last active Aug 29, 2015
Here are some things you can do with Gists in GistBox.
View 0_reuse_code.js
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
mocheng / getHost.js
Created Feb 13, 2011
With the help of regular expression, host name can be extracted from URL string in two lines.
View getHost.js
* extract host name from URL string.
function getHost(url) {
var m = url.match(/^https?:\/\/([^\/:\d]+)/);
return m && m[1];
mocheng / template.js
Created Feb 14, 2011
Simple template function to replace pattern in string with object properties or function returned value.
View template.js
* Simple template function.
* For each pattern in argument template, it is replaced with actual property from argument data.
* t('{greeting} world', {greeting: 'hello'}) === 'hello world'
* Sequence of property in argument data doesn't matter:
* t('{g} {x}', {g: '{o}', o: 'x'})) === '{o} {x}'
* t('{g} {x}', {o: 'x', g: '{o}'})) === '{o} {x}'
View algorithm.php
$arr = array(
'Color' => array('Red', 'Blue'),
'Size' => array('Regular', 'Large'),
'Material' => array('Metalic', 'Nylon')
function magic_algorithm($arr){
function cartesian_product($arrays) {
mocheng / demoMouseOut.html
Created Feb 28, 2011
In YUI pre-2.8.0, there is no "mouseleave" event. So, it is pretty awkward to detect whether mouse is moving out of a element with nested children nodes.
View demoMouseOut.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="/yui/assets/yui.css?v=3" >
<script src=""</script>
<!-- Browser History Manager source file -->
<style type="text/css" media="screen">
mocheng / escapeHTML.1.js
Created May 10, 2011
Different Implementation of escapeHTML
View escapeHTML.1.js
function escapeHTML(s) {
return s.replace(/&/g, '&amp;').replace(/>/g, '&gt;').replace(/</g,'&lt;').replace(/"/g, '&quot').replace(/'/g,'&#x27;').replace(/\//g,'&#x2F;');
mocheng / index.html
Created Jun 9, 2011
Simple MVC demo in Node.js
View index.html
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>MVC Demo</title>
{{name}} is {{title}}.
mocheng / override
Created Jun 22, 2011
override object method and recover it.
View override
var foo =; = function() {
// do something = foo;;