Tag Archives: Web

socket.io的命名空间(namespace)和房间(room)

socket.io的命名空间(namespace)和房间(room)

socket.io支持命名空间和房间

默认是的命名空间是 '/'

没有默认的房间

socket, room, namespace三者关系

socket 一定是属于某个namespace,

room 一定是属于某个namespace,

socket可以在某个房间或者不在任何房间

使用of来创建命名空间:

var nsp = io.of('/my-namespace');
nsp.on('connection', function(socket){
  console.log('someone connected'):
});
nsp.emit('hi', 'everyone!');

客户端可以使用

var socket = io('/my-namespace');

链接到这个命名空间

通常使用默认命名空间,所以可以使用省去命名空间名字,

var socket = io('/');

或者

var socket = io();

房间(room)

创建和加入房间是同一个函数 join

socket.join('some room');

离开房间

socket.leave('some room');

继续阅读

socket.io建立的聊天程序

socket.io建立的聊天程序

nodejs的socket.io的功能如同websocket一样,是建立长链接,可以让服务器主动发消息给客户端(通常是指浏览器)

但是好处是服务端和客户端用的是同一种语言,都是javascript,学习成本比较低。

下面就一个简单的例子:

继续阅读

nodejs socket.io初探

nodejs socket.io初探

nodejs的socket.io的功能如同websocket一样,是建立长链接,可以让服务器主动发消息给客户端(通常是指浏览器)

但是好处是服务端和客户端用的是同一种语言,都是javascript,学习成本比较低。

下面就一个简单的例子:

建立简单的聊天的程序:

继续阅读

nodejs的hello world

nodejs的hello world

nodejs的hello world可以使用2种方式实现:

  • 使用express
  • 使用none-static

使用express

—- server.js文件

var express = require('express');
var app = express();

app.get('/', function (req, res) 
{
  res.send('Hello World!');  // 这里发送hello world 字符串
});

var server = app.listen(3000, function () {

  var host = server.address().address;
  var port = server.address().port;

  console.log('server listening at http://%s:%s', host, port);

});

继续阅读

Nodejs服务器的文件组织结构

Nodejs服务器的文件组织结构

nodejs服务器由下面几个文件组成

  • 服务器入点js文件
  • package.json
  • README.md

通常还需要html,和js,css文件。

服务器入点js文件,例如 server.js

这个文件需要传给nodejs,告诉它从那个文件启动,如:

# node server.js

例如:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io').listen(http);

继续阅读

Nodejs + socket.io创建web聊天程序

Nodejs + socket.io创建web聊天程序

通过本文创建一个聊天程序

后台采用Nodejs作为web服务器,前后台通信采用socket.io,

后台 index.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io').listen(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

继续阅读

nodejs安装使用mysql

nodejs安装使用mysql

要让nodejs能够使用mysql,需要先安装相应的驱动,安装方法如下:

全局安装:

npm install -g mysql

本地安装(只对当前工程有效):

1. 在package.json的dependencies添加:

"dependencies": {
    "mysql": "~2.3.2",
     ...

2. 运行下面的命令

run npm install

至于如何安装和启动msyql服务器不在本文讨论范围之内。

下面演示如何在nodejs中使用mysql



var http = require('http'),
   mysql = require('mysql');

var sqlInfo = {
   host: 'localhost',
   user: 'root', -----> 用户名
   password: 'urpass',   ------> 密码
   database: 'dbname'   -------> 数据库名字
}
client = mysql.createConnection(sqlInfo);

client.connect();  -----> 链接

继续阅读

使用sails.js

Sails.js Welcome

使用sails.js

下面文字转载自:

https://www.digitalocean.com/community/tutorials/how-to-create-an-node-js-app-using-sails-js-on-an-ubuntu-vps

The rest should mostly be copy-and-pastable.

What is Sails.js?

Sails.js makes it easy to build custom, enterprise-grade Node.js apps. It is designed to mimic the MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps: data-driven APIs with scalable, service-oriented architecture. It's especially good for building chat, realtime dashboards, or multiplayer games.

In other words: Sails.js allows you to easily create apps with Node.js using the Model-View-Controller pattern to organize your code so it is easier to maintain. Sails.js provides various commands to automate the creation of models and controllers, saving you time and allowing you to create the app faster. (Views have to be created manually in the /views directory in the template /views/:controller/:method.ejs). You are able to use various templating languages, however EJS is the default, and it would be safest and easiest to just stick with EJS.

Sails.js also has various "adapters", allowing you to use virtually any database you want to with your app. This gives you the maximum amount of flexibility, differing from other MVC frameworks that insist on you using MongoDB.

All of these and the fact that on deployment, all of your files are concatenated and minified means that you don't have to spend as much time setting the main framework up to build your app on top of, as it is all ready and easy to use.

 

Installing Node.js on an Ubuntu VPS

  1. Installing the pre-requisites:

    sudo apt-get install python-software-properties python g++ make

    If you are using Ubuntu 12.10, you will need to do the following as well:

    sudo apt-get install software-properties-common
  2. Add the PPA Repository, which is recommended by Joyent (the maintainers of Node.js):

    sudo add-apt-repository ppa:chris-lea/node.js
  3. Update the package list:

    sudo apt-get update
  4. Install Node.js:

    sudo apt-get install nodejs
 

Installing Sails.js

To install the latest stable release of sails, you will need to run:

sudo npm install sails -g

The -g flag ensures that sails is installed globally and can be used as a command-line tool.

 

Creating your Sails app

You will want to navigate to the directory in which you would like your app to be located, e.g. /var/www and then run:

sails new project-name

This will add the required files to your project, and create a directory named project-name.

 

Starting up the Sails.js server

To view your boilerplate app, you will need to change directory into the project directory and then start the server:

cd project-name

then:

sails lift

This will create a server running at 123.456.78.90:1337, and the page may look something like this (They have changed it a few times, so depending on when you are reading this article, it may be different):

 

Creating Controllers

Creating a controller is easy, the sails CLI does all the hard stuff for you. e.g. To create a controller called user with the methods "index, show, edit, delete", all you have to do is run the following command:

sails generate controller user index show edit delete

继续阅读