第一步:安装node

git clone https://github.com/joyent/node.git

cd node

git checkout v0.10.33-release

./configure

make

make install

第二步:安装npm

mac下使用命令ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

第三步:安装redis

wget http://download.redis.io/releases/redis-2.8.17.tar.gz

cd redis-2.8.17

make

启动redis服务

src/redis-server

第四步:创建package.json

cd /data/project/current/

npm init

name: (20141201051425) chat-server

version: (1.0.0)

description: chat nodejs socket.io

entry point: (chat_server.js)

test command: chat-server

git repository: (https://github.com/monstar-lab/maturi.git)

keywords: chat nodejs socket.io

author: project

license: (ISC)

第五步:

npm install redis --save

npm install socket.io --save

npm install socket.io-redis --save

npm install fs --save

其他
==========================================
npm install -g node-gyp
npm install -g npm
npm install nan -save
cd node-posix
node-gyp configure
node-gyp build
 
var posix = require('node-posix’);
posix.setrlimit('nofile', { soft: 10000, hard: 10000 });
================================================
npm uninstall xxx -save
 
 
 
1
2
// これで通信中のIDが取得できる
var id = socket.id

メッセージを送信してきたClientへメッセージを返答

1
socket.emit('message', info);

接続しているClient全体にメッセージを送信

1
io.sockets.emit('message', info);

個別にデータを送信

1
io.sockets.socket(socket.id).emit('message', info);
  • socket.emit('message','the message to be transmitted'); //simple transmission
  • io.sockets.emit('message',"the message to be transmitted"); //send to all clients
  • socket.broadcast.emit('message',"this is a test"); //send to all except sender

Joining and leaving

You can call join to subscribe the socket to a given channel:

io.on('connection', function(socket){
socket.join('some room');
});

And then simply use to or in (they are the same) when broadcasting or emitting:

io.to('some room').emit('some event'):
 
To leave a channel you call leave in the same fashion as join.
 
var sockets = io.sockets.sockets;
 
 
io.sockets.on('connection', function(socket){
//console.log('コネクション数',socket.client.conn.server.clientsCount);

process.setMaxListeners(0);
   
    process.on('uncaughtException', function (err) {
        //打印出错误
        console.log('uncaughtException: ');
        console.log(err);
        //打印出错误的调用栈方便调试
        console.log(err.stack);

      });
})
 
socket连接数太少
sudo prlimit --nofile=10240  --pid 16728
pid修改成node的pid
lsof -i:8000
 
设定sudo prlimit --nofile=10240  --pid 16728
查看sudo prlimit --nofile  --pid node的pid
 
 
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
 ps H -eo user,pid,ppid,tid,time,%cpu,cmd --sort=%cpu

lsof -i:8000
lsof | grep TCP | grep 8000 | grep ESTABLISHED | wc -l
ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid' | grep node |  sort
netstat –apn | grep 8000
 
pgrep -l node 

NodeJS + Socket.io搭建聊天服务器的更多相关文章

  1. 使用socket.io搭建聊天室

    最近在学习nodejs,需要找一些项目练练手.找来找去发现了一个聊天室的教程,足够简单,也能从中学到一些东西.下面记录我练习过程中待一些笔记. nodeJS模块 共用到了2个模块,express和so ...

  2. nodejs+socket.io即时聊天实例

    在这之前你应该先安装好 Node.js,安装过程不再讲解 首先在你的电脑上创建一个新目录,姑且命名为 chat,然后在该目录创建两个文件,分别是 app.js 和 index.html. app.js ...

  3. Node+Express+MongoDB + Socket.io搭建实时聊天应用

    Node+Express+MongoDB + Socket.io搭建实时聊天应用 前言 本来开始写博客的时候只是想写一下关于MongoDB的使用总结的,后来觉得还不如干脆写一个node项目实战教程实战 ...

  4. Node+Express+MongoDB + Socket.io搭建实时聊天应用实战教程(二)--node解析与环境搭建

    前言 本来开始写博客的时候只是想写一下关于MongoDB的使用总结的,后来觉得还不如干脆写一个node项目实战教程实战.写教程一方面在自己写的过程中需要考虑更多的东西,另一方面希望能对node入门者有 ...

  5. Node+Express+MongoDB+Socket.io搭建实时聊天应用实战教程(一)--MongoDB入门

    前言 本文并不是网上流传的多少天学会MongoDB那种全面的教程,而意在总结这几天使用MongoDB的心得,给出一个完整的Node+Express+MongoDB+Socket.io搭建实时聊天应用实 ...

  6. nodejs + socket.io + redis 新手上路

    最近要更新网站架构了,决定转入 nodejs + socket.io + redis 方式. 战斗刚开始: 网上的文章太松散,我根据各个网友的分享进行整理 ,让大家可以方便上手. 进入node.js之 ...

  7. Node.js下基于Express + Socket.io 搭建一个基本的在线聊天室

    一.聊天室简单介绍 采用nodeJS设计,基于express框架,使用WebSocket编程之 socket.io机制.聊天室增加了 注册登录模块 ,并将用户个人信息和聊天记录存入数据库. 数据库采用 ...

  8. 使用nodejs引用socket.io做聊天室

    Server: var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs ...

  9. 使用Node.js+Socket.IO搭建WebSocket实时应用

    Web领域的实时推送技术,也被称作Realtime技术.这种技术要达到的目的是让用户不需要刷新浏览器就可以获得实时更新.它有着广泛的应用场景,比如在线聊天室.在线客服系统.评论系统.WebIM等. W ...

随机推荐

  1. HBase-配置说明

    转载自:http://www.aboutyun.com/thread-7914-1-1.html hbase.rootdir这个目录是region  server的共享目录,用来持久化Hbase.UR ...

  2. Linux 下memcache安装及使用

    memcache是高性能,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.据说官方所说,其用户包括twitter.digg.flickr等,都是些互联网大腕呀.目前用memca ...

  3. And Then There Was One

    http://poj.org/problem?id=3517 And Then There Was One Time Limit: 5000MS   Memory Limit: 65536K Tota ...

  4. Firefox便携版

    安装firefox后默认配置文件在C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox 可以在快捷方式里设置不同的配置文件,但是要注意参数的含义 ...

  5. 【极角排序、扫描线】UVa 1606 - Amphiphilic Carbon Molecules(两亲性分子)

    Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new class of ...

  6. js如何判断一个数组中是否有重复的值

    引自:http://bbs.tianya.cn/post-414-38497-1.shtml 方法一: var ary = new Array("111","22&quo ...

  7. 织梦dedecms源码安装方法

    织梦dedecms源码安装方法 第一步: 上传所有文件到空间 注意:(由于有很多人反应安装后首页样式都乱的,所以强烈要求安装到根目录,如:127.0.0.1 / www.xxx.com,或者二级域名也 ...

  8. JavaScript 三种绑定事件方式之间的区别

    JavaScript三种绑定事件的方式: 1. <div id="btn" onclick="clickone()"></div> // ...

  9. 利用jQuery实现选项卡

    /*Tab 选项卡 标签*/ $(function(){ var $div_li =$("div.tab_menu ul li"); $div_li.click(function( ...

  10. Slickflow.NET 开源工作流引擎基础介绍(五) -- 会签加签高级特性介绍

    前言:会签和加签是常见审批流程模式,在引擎中,对这两种流程模式做了分别定义和实现,其中也用到了Workflow Pattern的Multiple Instance(多实例) . 1. 会签和加签的定义 ...