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 ...
随机推荐
- "jobTracker is not yet running"(hadoop 配置)
今天自己尝试做配置了一下hadoop,环境是ubuntu13.10+jdk1.7.0_51+hadoop version1.2.1. 主要过程主要参考http://blog.csdn.net/hitw ...
- java_字符
例一:数据类型转换 package javaweb1200; public class 字符就是数字吗 { public static void main(String[] args) { Syste ...
- Stupid Tower Defense
Problem Description FSF is addicted to a stupid tower defense game. The goal of tower defense games ...
- Flask与Ajax
这篇短文使用jquery. Flask提供一个很简单的方法来处理Ajax请求——在视图函数中用request的属性is_xhr来判断,如果是true则是异步请求. Jquery的$.getJSON() ...
- Javascript与Ajax
不使用jquery来处理ajax请求该怎么做? 首先要明确html中的某些数据需要从服务端获得,也就是客户端向服务端请求(request)数据,服务端就响应(response)这个请求,把客户端要的数 ...
- x264命令参数与代码中变量的对应关系
帧类型选项: -I/--keyint i_keyint_max 最大IDR帧间距,默认为250 -i/--min-keyint i_keyint_min 最小IDR帧间距,默认为25 --sce ...
- 关于JDK中正则表达式
正则表达式的构造摘要 构造 匹配 字符 x 字符 x \\ 反斜线字符 \0n 带有八进制值 0 的字符 n (0 <= n <= 7) \0nn 带有八进制值 0 的字符 nn ...
- Share_memory
共享内存是允许多个进程共享一块内存,由此来达到交换信息的进程通信机制:它很快没有中间介质,唯一的不足就是需要一定的同步机制控制多个进程对同一块内存的读/写,,它的原理如下: 每个共享内存段都有一个sh ...
- poj 2987 最大闭合子图
思路: 这题考的是最大闭权图.只要知道怎么求最大闭权图就知道怎么做.但好像有点卡模版,要高效的模版才行. #include <iostream> #include <stdio.h& ...
- Nginx - Windows 环境安装 Nginx
1. 访问 http://nginx.org/en/download.html,下载 Windows 版本的安装包 2. 解压安装包,双击 nginx.exe,启动 nginx 3. 访问 http: ...