Node.js Quick Start

来自Jack's Lab
跳转到: 导航, 搜索

目录

1 Setup

下载安装包:http://nodejs.cn/download/

Linux x86_64 下 node-xx-linux-x64.tar.gz

comcat@jackslab:/work/nodejs$ sudo tar --strip-components 1 -zxf node-v0.12.7-linux-x64.tar.gz -C /usr/local/
comcat@jackslab:/work/nodejs$ node -v
v0.12.7



2 Try

comcat@jackslab:/work/nodejs$ npm install coap-cli -g 
coap-cli@0.3.0 node_modules/coap-cli
├── commander@2.0.0
├── through2@0.4.2 (xtend@2.1.2, readable-stream@1.0.33)
└── coap@0.5.4 (lru-cache@2.5.2, coap-packet@0.1.12, backoff@2.3.0, bl@0.6.0)

comcat@jackslab:/work/nodejs$ coap

  Usage: coap [command] [options] url

  Commands:

    get                    performs a GET request
    put                    performs a PUT request
    post                   performs a POST request
    delete                 performs a DELETE request

  Options:

    -h, --help               output usage information
    -V, --version            output the version number
    -o, --observe            Observe the given resource
    -n, --no-new-line        No new line at the end of the stream
    -p, --payload <payload>  The payload for POST and PUT requests
    -q, --quiet              Do not print status codes of received packets

comcat@jackslab:/work/nodejs$ coap get coap://vs0.inf.ethz.ch/
(2.05)	************************************************************
CoA



3 libcoap

#$ git clone git://git.code.sf.net/p/libcoap/code libcoap-code
$ git clone git://github.com/obgm/libcoap.git libcoap-code
$ cd libcoap-code
$ ./autogen.sh
$ ./configure --disable-examples
$ make
$ sudo make install


Build in other platform:


Contiki

If you have cloned the sources via git, you need to run ./autogen.sh to generate the build scripts. This step is optional when a source archive is used.

Next, say ./configure --with-contiki to prepare the Makefiles required to build libcoap as Contiki app. To make this work, libcoap usually is unpacked in the directory contiki/apps.


LwIP

Using libcoap with LwIP requires a hand-crafted Makefile. Please refer to examples/lwip/Makefile to see how this is done.


TinyOS

The usage of libcoap with TinyOS is documented in the TinyOS Wiki.



4 MQTT

4.1 Server

https://github.com/mcollina/mosca/wiki/MQTT-over-Websockets

comcat@jackslab:/work/mqtt$ npm install mosca bunyan -g
comcat@jackslab:/work/mqtt$ mosca -v --http-port 3000 --http-bundle --http-static ./ | bunyan
comcat@jackslab:/work/mqtt$ cat server.js 
#!/usr/bin/env node

var mosca = require('mosca')

var moscaSettings = {
  port: 1883
};

var server = new mosca.Server(moscaSettings);

server.on('ready', setup);

server.on('clientConnected', function(client) {
    console.log('client connected', client.id);     
});

// fired when a message is received
server.on('published', function(packet, client) {
  console.log('Published', packet.payload);
});

// fired when the mqtt server is ready
function setup() {
  console.log('Mosca server is up and running')
}

4.2 Client

$ npm install MQTTClient
$ cat sub.js 
#! /usr/bin/env node

var MQTTClient = require('MQTTClient').Client;
var client = new MQTTClient('101.200.202.247', 1883, options);

client.connect(function () {
	// do something if connect success 
	console.log("connect ok!");
	var subopt = {
		dup_flag:   0,
		qos_level:  0
	}
	client.subscribe('/app2dev/gh_95fae', subopt, function (topic, qos_level) {
		console.log("subscribe dev2app ok! Topic: " + topic + '  Qos: ' + qos_level);
	});

});

client.on('error', function(err) {
	console.log(err);
});

client.on('publish', function(topic, payload) {
	console.log('[' + topic + ']\t' + payload);
});

comcat@jackslab:/work/mqtt$ cat pub.js 
#! /usr/bin/env node

var MQTTClient = require('MQTTClient').Client;
var client = new MQTTClient('101.200.202.247', 1883);

client.connect(function () {
	console.log("connect ok!");
	var pubopt = {
		qos_level:  2
	}
	client.publish('/app2dev/gh_95fae', 'Hello World!', pubopt, function (message_id) {
		// do something if success 
		console.log("public ok! message_id = " + message_id);
	});
});

client.on('error', function(err) {
	console.log(err);
});

client.on('publish', function(topic, payload) {
	console.log('[' + topic + ']\t' + payload);
});

















个人工具
名字空间

变换
操作
导航
工具箱