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.官网的一些相关说明,概述的更多相关文章

  1. 官网下载java相关资源

    官网下载java相关资源 官网地址:http://www.oracle.com 一.下载JDK 1.首先进入Downloads >> Java For Developers,如图 2.点击 ...

  2. 【socket.io研究】3.手机网页间聊天核心问题

    前面我们已经说了服务器相关的一些内容,且又根据官网给出的一个例子写了一个可以聊天的小程序,但是这还远远不够呀,这只能算是应用前的准备工作.接下来,一起来考虑完善一个小的聊天程序吧. 首先,修改服务器的 ...

  3. 【socket.io研究】2.小试牛刀

    1.建立个项目,也就是文件夹,这里使用testsocket 2.创建文件package.json,用于描述项目: { "name":"testsocket", ...

  4. 【socket.io研究】0.前提准备

    WebSocket出现之前,web实时推送,一般采用轮询和Comet技术(可细分为长轮询机制和流技术两种),需要大量http请求,服务器受不了.HTML5定义了WebSocket协议,基于TCP协议, ...

  5. 基于node.js+socket.io+html5实现的斗地主游戏(1)概述

    一.游戏描述 说是斗地主游戏,其实是寝室自创的"捉双A",跟很多地方的捉红10.打红A差不多,大概规则是: 1.基础牌型和斗地主一样,但没有大小王,共52张牌,每人13张,这也是为 ...

  6. 基于 nodejs 的 webSockt (socket.io)

    基于 nodejs 的 webSockt (socket.io) 理解 本文的业务基础是在基于 nodejs 的 socket.io 的直播间聊天室(IM)应用来的. 项目中具体的 框架如下 expr ...

  7. webSocket协议和Socket.IO

    一.Http无法轻松实现实时应用: ● HTTP协议是无状态的,服务器只会响应来自客户端的请求,但是它与客户端之间不具备持续连接. ● 我们可以非常轻松的捕获浏览器上发生的事件(比如用户点击了盒子), ...

  8. Spring 官网jar下载

    1,首先输入http://spring.io/进入Spring官网 2,点击project 栏,找到Spring framwork 3,点击reference 4,找到Distribution Zip ...

  9. 0. 资料官网【从零开始学Spring Boot】

    [视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...

随机推荐

  1. [转载] 与WIN不同,linux替换文件夹会删除原文件夹下的全部内容!

    今天差点把源码给覆盖掉了><...555... 虚惊一场!!看了一篇博客分析这种情况.我的环境是CentOS5.5,不会出现文件夹直接被覆盖的情况,但是在Linux下不要用Win下的一些直 ...

  2. 转载:Struts2.3.15.1升级总结

    转载网址:http://blog.csdn.net/amosryan/article/details/10350481 由于大家都懂的原因,涉struts2的项目需要将struts2相关包升级至2.3 ...

  3. c#使用UIA进行模拟点击操作

    之前,我写过一篇c#使用spy进行模拟操作的文章,有朋友在留言中提到了UIA进行操作,今天也使用UIA重新实现一次对vnc窗体的控制测试. 实现目标 在server框内填入192.168.2.200 ...

  4. 两表关联更新,用于update 回滚

    create table test1 as select * from dba_objects; create table test2 as select * from dba_objects; cr ...

  5. 【转】随身HiFi 安卓OTG功能在音频上的妙用

    原文网址:http://article.pchome.net/content-1745467.html 随身HiFi 安卓OTG功能在音频上的妙用 [PChome电脑之家音频频道原创]说起Androi ...

  6. 【李婶小教程】(SE_lab3)

    额,今天说一下这个SE_lab3那几个东西都是怎么装的. 啊--其实技术含量貌似不是很高的. 1.先说最简单的一个叫做:Findbugs 点这个Eclipse Marketplace,这是一个神奇的东 ...

  7. HDU_2034——集合A-B

    Problem Description 参加过上个月月赛的同学一定还记得其中的一个最简单的题目,就是{A}+{B},那个题目求的是两个集合的并集,今天我们这个A-B求的是两个集合的差,就是做集合的减法 ...

  8. 你应该了解的 7个Linux ls 命令技巧

    在前面我们系列报道的两篇文章中,我们已经涵盖了关于‘ls’命令的绝大多数内容.本文时‘ls命令’系列的最后一部分.如果你还没有读过该系列的其它两篇文章,你可以访问下面的链接. 15 个‘ls’命令的面 ...

  9. 形形色色的软件生命周期模型(4)——MSF、实用型

    摘要: 读大学时,我们曾经学习过不少软件生命周期模型,当时还不是很懂软件开发,你可能会觉得这些东西很新奇.在实际工作中,你会发现这些模型其实很难应用,与此同时你会接触到RUP.MSF等权威软件公司的生 ...

  10. Java语言实现简单FTP软件------>上传下载队列窗口的实现(七)

    1.首先看一下队列窗口的界面 2.看一下上传队列窗口的界面 3.看一下下载队列窗口的界面 4.队列窗口的实现 package com.oyp.ftp.panel.queue; import stati ...