NodeJS + Socket.io搭建聊天服务器
第一步:安装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
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'):
leave
in the same fashion as join
.//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);
最新文章
- Android 下拉列表框、文本框、菜单
- 【BZOJ】2286: [Sdoi2011消耗战
- OC self和super
- Mac查看端口占用情况
- 简单回顾C++中的字符串
- SignalR2.0开发实例之——私聊
- codevs1009
- SQL 修改排序规则的问题 sql_latin1_general_cp1_ci_as
- 在windows上搭建ipv6代理
- 内核调试神器SystemTap — 更多功能与原理(三)
- Neural style transfer
- ABAP-2-会计凭证批量数据导入本地ACCESS
- Android 7.0 新增功能和api
- powerviot install in sharepoint 2013
- jmeter测试计划配置
- C++ string 类
- [bzoj3995] [SDOI2015]道路修建 线段树
- 【PTA 天梯赛】L2-028 秀恩爱分得快(模拟)
- DNS测试工具的使用(了解)
- springboot设置日志级别时报错
热门文章
- extern的困惑
- 关于File.getPath,File.getAbsolutePath,File.getCanonicalPath的区别
- Recommended you 3 most popular Nissan pincode calculators
- 2012第二届GIS制图大赛——公开课技术问题&;答疑(珍贵资源哦!)(http://blog.csdn.net/arcgis_all/article/details/8216984)
- Advanced Installer
- 实验教学管理系统 c语言程序代写源码下载
- linux压缩解压缩
- Adobe Edge Animate –修改Edge Commons Spotlight功能,使之能支持播放中国网站视频
- 封装SqliteHelper类--Sqlite数据库
- unity3d所要知道的基础知识体系大纲,可以对照着学习,不定期更新