【socket.io研究】1.官网的一些相关说明,概述
socket.io是什么?
官网的解释是一个实时的,基于事件的通讯框架,可以再各个平台上运行,关注于效率和速度。
在javascript,ios,android,java中都实现了,可以很好的实现实时的交流沟通,很好用。
一般是用nodejs作为服务端,但不是说服务端和客户端进行沟通交流,而是说作为一个服务器,接受注册,转发消息,用户可以使用不同的客户端,进行交流沟通,很类似于QQ微信等实时通信工具。
支持的通信方式:根据浏览器的支持程度,自动选择使用哪种技术进行通信
WebSocket
Flash Socket
AJAX long-polling
AJAX multipart Stream
Forever IFrame
JSONP polling
copy一点官网的东西:
如果你不愿意看,那就看如下几行,后面的代码部分不用看了,是具体的解释,文末有原文链接。
1.使用了Node http servr
2.使用了Express 3/4
3.使用了Express framework
4.根据不同事件执行不同操作
5.不同的用户在不同的namespace下,可以实现消息隔离。
6.在客户端网络情况不好的情况下,发送易变(volatile)数据,在网络情况不好或者是客户端处于长连接建立阶段,客户端没连线,服务器丢弃发送失败的数据。
7.发送和获取消息,确认收到,当客户端收到消息时,会得到回调函数。
8.广播消息
9.就像一个跨浏览器的websocket一样使用它
1.使用了Node http server
Server (app.js)
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs'); app.listen(80); function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
} res.writeHead(200);
res.end(data);
});
} io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Client (index.html)
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
2.使用了Express 3/4
Server (app.js)
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server); server.listen(80); app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
}); io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Client (index.html)
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
3.使用了Express framework
Server (app.js)
var app = require('express').createServer();
var io = require('socket.io')(app); app.listen(80); app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
}); io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Client (index.html)
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
3.发送和接收事件(
Sending and receiving events)
不仅仅包含connect,message,disconnect,你可以定义自己的事件。
Server
// note, io(<port>) will create a http server for you
var io = require('socket.io')(80); io.on('connection', function (socket) {
io.emit('this', { will: 'be received by everyone'}); socket.on('private message', function (from, msg) {
console.log('I received a private message by ', from, ' saying ', msg);
}); socket.on('disconnect', function () {
io.emit('user disconnected');
});
});
//发送给对应客户端
socket.emit('message', "this is a test"
);
//发送给所有客户端
io.sockets.emit.('message', "this ia a test");
//发送给指定socketid客户端
io.sockets.socket(socketid).emit('message', "for this socket only");
//发送自定义事件
socket.emit('my-event', "this is a test");
//事件接收
socket.on("enent name", function(data){
//data为接收到的数据
});
4.如果可以控制应用程序发出的所有消息和事件,使用默认的命名空间,如果想利用第三方代码生成代码,或者是与他人分享代码,使用命名空间namespace,可以实现消息的隔离。这也有力于多路复用。
Server (app.js)
var io = require('socket.io')(80);
var chat = io
.of('/chat')
.on('connection', function (socket) {
socket.emit('a message', {
that: 'only'
, '/chat': 'will get'
});
chat.emit('a message', {
everyone: 'in'
, '/chat': 'will get'
});
}); var news = io
.of('/news')
.on('connection', function (socket) {
socket.emit('item', { news: 'item' });
});
Client (index.html)
<script>
var chat = io.connect('http://localhost/chat')
, news = io.connect('http://localhost/news'); chat.on('connect', function () {
chat.emit('hi!');
}); news.on('news', function () {
news.emit('woot');
});
</script>
5.发送易变(volatile)消息
因为网络不好等原因,可能需要发送voiatile消息,以防止收不到所有的反馈而造成的一些问题
Server
var io = require('socket.io')(80); io.on('connection', function (socket) {
var tweets = setInterval(function () {
getBieberTweet(function (tweet) {
socket.volatile.emit('bieber tweet', tweet);
});
}, 100); socket.on('disconnect', function () {
clearInterval(tweets);
});
});
6.在发送和接收数据时使用回调函数,需要将函数作为.send或者是.emit的最后一个参数.
Server (app.js)
var io = require('socket.io')(80); io.on('connection', function (socket) {
socket.on('ferret', function (name, fn) {
fn('woot');
});
});
Client (index.html)
<script>
var socket = io(); // TIP: io() with no args does auto-discovery
socket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too!
socket.emit('ferret', 'tobi', function (data) {
console.log(data); // data will be 'woot'
});
});
</script>
7.发送广播消息
在send或者是emit前加上broadcast就可以了
Server
var io = require('socket.io')(80); io.on('connection', function (socket) {
socket.broadcast.emit('user connected');
});
8.跨浏览器的问题
设置send发送数据,监听message事件
Server (app.js)
var io = require('socket.io')(80); io.on('connection', function (socket) {
socket.on('message', function () { });
socket.on('disconnect', function () { });
});
Client (index.html)
<script>
var socket = io('http://localhost/');
socket.on('connect', function () {
socket.send('hi'); socket.on('message', function (msg) {
// my msg
});
});
</script>
9.房间
相当于为指定的一些客户端提供一个命名空间,所在房间里的广播和通信不会影响房间意外的其他用户。
//加入房间io.on('connection', function(socket){
socket.join('some room');
});//离开房间socket.leave('some room');//向房间中发送信息io.to('some room').emit('some event');//在某个房间中发送信息io.to('spme room').emit('some event');//to()方法用于在指定的房间中,对除了当前socket的其他的socket发送消息socket.broadcast.to('game').emit('message', 'nice game');//in()方法用于在指定的房间中,为房间中的所有的socket发送消息io.socket.in('game').emit('message', 'cool game');
//向my room广播一个事件,提交者会被排除在外(即不会收到消息)
io.sockets.on('connection', function (socket) {//注意:和下面对比,这里是从客户端的角度来提交事件
socket.broadcast.to('my room').emit('event_name', data);
}
//向another room广播一个事件,在此房间所有客户端都会收到消息
//注意:和上面对比,这里是从服务器的角度来提交事件io.sockets.in('another room').emit('event_name', data);
//向所有客户端广播
io.sockets.emit('event_name', data);
//获取所有房间的信息
//key为房间名,value为房间名对应的socket ID数组io.sockets.manager.rooms
//获取particular room中的客户端,返回所有在此房间的socket实例
io.sockets.clients('particular room')//通过socket.id来获取此socket进入的房间信息
io.sockets.manager.roomClients[socket.id]
有些东西解释的不好,请通过文末的原文链接进入原文查看。
原文:http://socket.io/docs/
参考:http://blog.csdn.net/baby97/article/details/49385259
参考:http://www.cnblogs.com/shijiaqi1066/p/3826251.html
【socket.io研究】1.官网的一些相关说明,概述的更多相关文章
- 官网下载java相关资源
官网下载java相关资源 官网地址:http://www.oracle.com 一.下载JDK 1.首先进入Downloads >> Java For Developers,如图 2.点击 ...
- 【socket.io研究】3.手机网页间聊天核心问题
前面我们已经说了服务器相关的一些内容,且又根据官网给出的一个例子写了一个可以聊天的小程序,但是这还远远不够呀,这只能算是应用前的准备工作.接下来,一起来考虑完善一个小的聊天程序吧. 首先,修改服务器的 ...
- 【socket.io研究】2.小试牛刀
1.建立个项目,也就是文件夹,这里使用testsocket 2.创建文件package.json,用于描述项目: { "name":"testsocket", ...
- 【socket.io研究】0.前提准备
WebSocket出现之前,web实时推送,一般采用轮询和Comet技术(可细分为长轮询机制和流技术两种),需要大量http请求,服务器受不了.HTML5定义了WebSocket协议,基于TCP协议, ...
- 基于node.js+socket.io+html5实现的斗地主游戏(1)概述
一.游戏描述 说是斗地主游戏,其实是寝室自创的"捉双A",跟很多地方的捉红10.打红A差不多,大概规则是: 1.基础牌型和斗地主一样,但没有大小王,共52张牌,每人13张,这也是为 ...
- 基于 nodejs 的 webSockt (socket.io)
基于 nodejs 的 webSockt (socket.io) 理解 本文的业务基础是在基于 nodejs 的 socket.io 的直播间聊天室(IM)应用来的. 项目中具体的 框架如下 expr ...
- webSocket协议和Socket.IO
一.Http无法轻松实现实时应用: ● HTTP协议是无状态的,服务器只会响应来自客户端的请求,但是它与客户端之间不具备持续连接. ● 我们可以非常轻松的捕获浏览器上发生的事件(比如用户点击了盒子), ...
- Spring 官网jar下载
1,首先输入http://spring.io/进入Spring官网 2,点击project 栏,找到Spring framwork 3,点击reference 4,找到Distribution Zip ...
- 0. 资料官网【从零开始学Spring Boot】
[视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...
随机推荐
- uva10020 贪心
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- C++拾遗(五)语句相关
前缀格式与后缀格式 对于表达式:后缀如 i++ 表达式的值仍是 i,在遇到下一个顺序点后再将 i 加1.前缀 ++i 表达式的值就是(i+1),先计算表达式的值,不需要等待 顺序点. 对于类:前缀函数 ...
- XMLHttpRequest state以及readystate的对应值
status状态值长整形标准http状态码,定义如下: Number Description 100 Continue101 Switching PRotocols200 OK201 Create ...
- MySQL递归查询所有子节点,树形结构查询
--表结构 CREATE TABLE `address` ( `id` int(11) NOT NULL AUTO_INCREMENT, `code_value` varchar(32) DEFAUL ...
- 1.2 Coin 项目
自2009年起,Coin便是Java 7(和Java 8)中一个开源的子项目.创建Coin项目是为了反映Java语言中的微小变动: 修改Java语言,按不同的修改方式及其复杂度依次分为:类库.工具提供 ...
- 国内开源html5游戏引擎全收录
本文引自<国内开源html5游戏引擎全收录> 游戏开发这潭水太深,英文水平太差,不敢看国外的, 而且这几年国内技术水平也挺高了不少,特别是JS方面.(我个人感觉) 最近看了几个国产的js游 ...
- po 和 mo 的互相转换
反编译 mo 文件成 po 文件 msgunfmt test.mo -o test.po 编码 po 文件为 mo 文件 msgfmt -o test.mo test.po 记着备用.
- Unity3D 经验记录
1.using UnityEngine.SceneManagement; 当在01场景调用02场景时,再载入回01场景时,代码保存的变量不会初始化,预制物体脚本内的变量会初始化. 2.当子物体太多时, ...
- Cisco C2900XL
http://docstore.mik.ua/univercd/cc/td/doc/product/lan/c2900xl/c2900sa4/sa4cr/macintr.htm#xtocid10160 ...
- 转: Python 运算符与用法
+加两个对象相加 3 + 5得到8.'a' + 'b'得到'ab'. (注意:6+'a'这样是错误的,但在PHP里这样是可以运行的) -减得到负数或是一个数减去另一个数 -5.2得到一个负数.50 - ...