第一步:安装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. 高级I/O之非阻塞I/O

    http://www.cnblogs.com/nufangrensheng/p/3515035.html中曾将系统调用分成“低速”系统调用和其他系统调用两类.低速系统调用是可能会使进程永远阻塞的一类系 ...

  2. Linux内核加载全流程

    无论是Linux还是Windows,在加电后的第一步都是先运行BIOS(Basic Input/Output System)程序——不知道是不是所以的电脑系统都是如此.BIOS保存在主板上的一个non ...

  3. python实现之极简stack和queue

    用python实现一个极简的stack和queue,那是so easy的事情了,简洁易懂,适合小白~ 直接上代码吧: node: class LinkNode: def __init__( self, ...

  4. Redis 集合(Set)

      Redis的Set是string类型的无序集合.集合成员是唯一的,这就意味着集合中不能出现重复的数据. Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1). 集合中最 ...

  5. BootStrap2学习日记7---表格

    基本表 代码: <div class="container"> <h1 class="page-header">基本表</h1&g ...

  6. 一个开源音乐播放器,低仿QQ音乐!

    有暇,弄了个音乐播放器,页面效果整体上参考了QQ音乐,相关API使用了易源数据提供的相关接口(https://www.showapi.com/api/lookPoint/213),在此表示感谢.先来看 ...

  7. Android网络请求与解析

    1.Volley和Gson结合使用——Volley适用于小型数据,多次的请求,使用Gson解析时,服务器数据的键值不能包含常用的标识符如:class.....等,这些就需要与服务端小伙伴商量 这样也可 ...

  8. SQLServer-镜像配置

    实验环境:三台服务器分别为主服务器,镜像服务器,见证服务器,都加入域sql.com 1. 分别在三台服务器上安装SQL 2008 R2,安装数据库引擎和管理工具两个组件即可. 2. 镜像前准备工作. ...

  9. Oracle开发专题之:%TYPE 和 %ROWTYPE

    1. 使用%TYPE 在许多情况下,PL/SQL变量可以用来存储在数据库表中的数据.在这种情况下,变量应该拥有与表列相同的类型.例如,students表的first_name列的类型为VARCHAR2 ...

  10. 关于简明Vim练级攻略

    本文装载于:http://coolshell.cn/articles/5426.html vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你 ...