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);
NodeJS + Socket.io搭建聊天服务器的更多相关文章
- 使用socket.io搭建聊天室
最近在学习nodejs,需要找一些项目练练手.找来找去发现了一个聊天室的教程,足够简单,也能从中学到一些东西.下面记录我练习过程中待一些笔记. nodeJS模块 共用到了2个模块,express和so ...
- nodejs+socket.io即时聊天实例
在这之前你应该先安装好 Node.js,安装过程不再讲解 首先在你的电脑上创建一个新目录,姑且命名为 chat,然后在该目录创建两个文件,分别是 app.js 和 index.html. app.js ...
- Node+Express+MongoDB + Socket.io搭建实时聊天应用
Node+Express+MongoDB + Socket.io搭建实时聊天应用 前言 本来开始写博客的时候只是想写一下关于MongoDB的使用总结的,后来觉得还不如干脆写一个node项目实战教程实战 ...
- Node+Express+MongoDB + Socket.io搭建实时聊天应用实战教程(二)--node解析与环境搭建
前言 本来开始写博客的时候只是想写一下关于MongoDB的使用总结的,后来觉得还不如干脆写一个node项目实战教程实战.写教程一方面在自己写的过程中需要考虑更多的东西,另一方面希望能对node入门者有 ...
- Node+Express+MongoDB+Socket.io搭建实时聊天应用实战教程(一)--MongoDB入门
前言 本文并不是网上流传的多少天学会MongoDB那种全面的教程,而意在总结这几天使用MongoDB的心得,给出一个完整的Node+Express+MongoDB+Socket.io搭建实时聊天应用实 ...
- nodejs + socket.io + redis 新手上路
最近要更新网站架构了,决定转入 nodejs + socket.io + redis 方式. 战斗刚开始: 网上的文章太松散,我根据各个网友的分享进行整理 ,让大家可以方便上手. 进入node.js之 ...
- Node.js下基于Express + Socket.io 搭建一个基本的在线聊天室
一.聊天室简单介绍 采用nodeJS设计,基于express框架,使用WebSocket编程之 socket.io机制.聊天室增加了 注册登录模块 ,并将用户个人信息和聊天记录存入数据库. 数据库采用 ...
- 使用nodejs引用socket.io做聊天室
Server: var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs ...
- 使用Node.js+Socket.IO搭建WebSocket实时应用
Web领域的实时推送技术,也被称作Realtime技术.这种技术要达到的目的是让用户不需要刷新浏览器就可以获得实时更新.它有着广泛的应用场景,比如在线聊天室.在线客服系统.评论系统.WebIM等. W ...
随机推荐
- Linux内核加载全流程
无论是Linux还是Windows,在加电后的第一步都是先运行BIOS(Basic Input/Output System)程序——不知道是不是所以的电脑系统都是如此.BIOS保存在主板上的一个non ...
- nodejs的mysql模块学习(四)断开数据库连接
断开连接有两种方式 end()函数 在这种情况下 所有先前排队的查询 仍然可以继续继续发送到服务器,但是如果在执行到断开连接的命令之前发生了致命的错误,那么end()将不会被执行 connection ...
- MYSQL基础笔记(三)-表操作基础
数据表的操作 表与字段是密不可分的. 新增数据表 Create table [if not exists] 表名( 字段名 数据类型, 字段名 数据类型, 字段n 数据类型 --最后一行不需要加逗号 ...
- MSSQL系统表常用操作
1:获取当前数据库中的所有用户表 select Name from sysobjects where xtype='u' and status>=0 2:获取某一个表的所有字段 select n ...
- webservice简单总结
一:webservice定义 webservice是一种基于xml,xsd封装格式,通过http协议通信的一种服务,支持跨平台.跨语言的 远程调用. 二:webservice优点 1:跨平台,无论是w ...
- [改善Java代码]异步运算考虑使用Callable接口
多线程有两种实现方式: 一种是实现Runnable接口,另一种是继承Thread类,这两种方式都有缺点,run方法没有返回值,不能抛出异常(这两个缺点归根到底是Runable接口的缺陷,Thread也 ...
- T-SQL使用JOIN执行UPDATE语句
问题: In SQL Server, it's possible to insert into a table using a SELECT statement: INSERT INTO Table ...
- 钓鱼 贪心 end
#include<iostream> int m,n; int *p; int dis=0; int peo=0; int data[3][2]; int b[3][2]; int da[ ...
- 【Knockout】四、绑定上下文
Binding context binding context是一个保存数据的对象,你可以在你的绑定中引用它.当应用绑定的时候,knockout自动创建和管理binding context的继承关系. ...
- java调用peoplesoft提供的webservice接口
使用到了soapui,apache-cxf,eclipse 1.使用soapui测试提供的地址有效性(如果没有soapui也可以直接在cmd命令行中使用wsdl2java命令生成客户端代码) wsdl ...