socket.io

https://socket.io/

https://socket.io/docs/

What Socket.IO is

Socket.IO is a library that enables real-time, bidirectional and event-based communication between the browser and the server. It consists of:

  • a Node.js server: Source | API
  • a Javascript client library for the browser (which can be also run from Node.js): Source | API

emit callback 用法

https://stackoverflow.com/questions/20337832/is-socket-io-emit-callback-appropriate

Recently I have been messing around with socket.io and found this interesting thing, that I can have emit function callback like this.

I start emitting on client side like this:

client.emit('eventToEmit', dataToEmit, function(error, message){
console.log(error);
console.log(message);
});

Then I can fire a callback from server-side like this:

client.on('eventToEmit', function(data, callback){
console.log(data);
callback('error', 'message');
});

Everything works fine with no errors, but I am interested if doing something like this is appropriate since I have not seen anything similar in the documentation or any example so far.

见老外的疑惑,也是本篇的主题, 为什么服务器端能够直接调用客户端设置的回调函数。

跨进程可以调用函数,真是稀奇。 类似RPC。

官方文档的解释

https://socket.io/docs/#Sending-and-getting-data-acknowledgements

Sending and getting data (acknowledgements)

Sometimes, you might want to get a callback when the client confirmed the message reception.

To do this, simply pass a function as the last parameter of .send or .emit. What’s more, when you use .emit, the acknowledgement is done by you, which means you can also pass data along:

Server (app.js)

var io = require('socket.io')(80);

io.on('connection', function (socket) {
socket.on('ferret', function (name, word, fn) {
fn(name + ' says ' + word);
});
});

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', 'woot', function (data) { // args are sent in order to acknowledgement function
console.log(data); // data will be 'tobi says woot'
});
});
</script>

client-代码跟踪

https://github.com/socketio/socket.io-client

以客户端源码为研究对象。

在socket.js文件中,存在emit实现:

如下代码中, 17-20行代码中, 会将callback函数存储到本地的acks数组中, 并将基数记为 packet.id,

然后packet作为数据整体,传送的服务器端。

 Socket.prototype.emit = function (ev) {
if (events.hasOwnProperty(ev)) {
emit.apply(this, arguments);
return this;
} var args = toArray(arguments);
var packet = {
type: (this.flags.binary !== undefined ? this.flags.binary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT,
data: args
}; packet.options = {};
packet.options.compress = !this.flags || false !== this.flags.compress; // event ack callback
if ('function' === typeof args[args.length - 1]) {
debug('emitting packet with ack id %d', this.ids);
this.acks[this.ids] = args.pop();
packet.id = this.ids++;
} if (this.connected) {
this.packet(packet);
} else {
this.sendBuffer.push(packet);
} this.flags = {}; return this;
};

服务器端处理完数据后, 调用callback接口后,服务器端调用的接口为包装接口, 包装了数据为packet, 并将id打在packet上, 表示此packet为emit时候的packet对应。

服务器端数据到来后, 根据packet.id定位到 callback函数, 并将packet.data作为参数传递到callback中。

/**
* Called upon a server acknowlegement.
*
* @param {Object} packet
* @api private
*/ Socket.prototype.onack = function (packet) {
var ack = this.acks[packet.id];
if ('function' === typeof ack) {
debug('calling ack %s with %j', packet.id, packet.data);
ack.apply(this, packet.data);
delete this.acks[packet.id];
} else {
debug('bad ack %s', packet.id);
}
};

server-代码跟踪

https://github.com/socketio/socket.io

socket.js中 在onevent中 在数据的args数组之后, 添加了 acknowledge 回调函数

 /**
* Called upon event packet.
*
* @param {Object} packet object
* @api private
*/ Socket.prototype.onevent = function(packet){
var args = packet.data || [];
debug('emitting event %j', args); if (null != packet.id) {
debug('attaching ack callback to event');
args.push(this.ack(packet.id));
} this.dispatch(args);
}; /**
* Produces an ack callback to emit with an event.
*
* @param {Number} id packet id
* @api private
*/ Socket.prototype.ack = function(id){
var self = this;
var sent = false;
return function(){
// prevent double callbacks
if (sent) return;
var args = Array.prototype.slice.call(arguments);
debug('sending ack %j', args); self.packet({
id: id,
type: hasBin(args) ? parser.BINARY_ACK : parser.ACK,
data: args
}); sent = true;
};
};

在 dispatch 负责调用 emitter 原生接口 on 绑定的 事件处理函数:

/**
 * `EventEmitter#emit` reference.
 */

var emit = Emitter.prototype.emit;

/**
* Dispatch incoming event to socket listeners.
*
* @param {Array} event that will get emitted
* @api private
*/ Socket.prototype.dispatch = function(event){
debug('dispatching an event %j', event);
var self = this;
function dispatchSocket(err) {
process.nextTick(function(){
if (err) {
return self.error(err.data || err.message);
}
emit.apply(self, event);
});
}
this.run(event, dispatchSocket);
}; /**
* Sets up socket middleware.
*
* @param {Function} middleware function (event, next)
* @return {Socket} self
* @api public
*/ Socket.prototype.use = function(fn){
this.fns.push(fn);
return this;
}; /**
* Executes the middleware for an incoming event.
*
* @param {Array} event that will get emitted
* @param {Function} last fn call in the middleware
* @api private
*/
Socket.prototype.run = function(event, fn){
var fns = this.fns.slice(0);
if (!fns.length) return fn(null); function run(i){
fns[i](event, function(err){
// upon error, short-circuit
if (err) return fn(err); // if no middleware left, summon callback
if (!fns[i + 1]) return fn(null); // go on to next
run(i + 1);
});
} run(0);
};

至此实现上彻底明了了。

socket.io emit callback调用探秘的更多相关文章

  1. socket.io emit 常见用法

    io.on('connect', onConnect); function onConnect(socket){ // 只发给sender. sending to the client socket. ...

  2. 使用 Socket.IO 开发聊天室

    前言 Socket.IO 是一个用来实现实时双向通信的框架,其本质是基于 WebSocket 技术. 我们首先来聊聊 WebSocket 技术,先设想这么一个场景: · 用户小A,打开了某个网站的充值 ...

  3. 关于Socket.IO的知识点记录

    最近因为项目的需要,开始学习nodejs,本着js的那点儿功底,nodejs学习起来还是挺快能上手的.随着深入学习,知道了express框架并那它写了一个小功能,作为一个php程序员哈,在expres ...

  4. AngularJS+Node.js+socket.io 开发在线聊天室

    所有文章搬运自我的个人主页:sheilasun.me 不得不说,上手AngularJS比我想象得难多了,把官网提供的PhoneCat例子看完,又跑到慕课网把大漠穷秋的AngularJS实战系列看了一遍 ...

  5. node基于express的socket.io

    前一段事件,我一个同学给他们公司用融云搭建了一套web及时通信系统,然后之前我的公司也用过环云来实现web及时通信,本人对web及时通信还是非常感兴趣的.私下读了融云和环信的开发文档,然后发现如果注册 ...

  6. socket.io的基本使用

    服务端: 1.监听客户端连接: io.on("connection",socket=>{ }); 不分组数据传输:传输对象为当前socket 2.1给该socket所有客户端 ...

  7. 利用socket.io实现消息实时推送

    最近在写的项目中存在着社交模块,需要实现这样的一个功能:当发生了用户被点赞.评论.关注等操作时,需要由服务器向用户实时地推送一条消息.最终完成的项目地址为:socket-message-push,这里 ...

  8. Socket.IO基础教程

    什么是Socket.IO Socket.IO是一个库,可用于在浏览器和服务器之间进行实时,双向和基于事件的通信.它包括: 使Node.js服务器:来源 | API 为浏览器(可从Node.js的也运行 ...

  9. 【socket.io研究】1.官网的一些相关说明,概述

    socket.io是什么? 官网的解释是一个实时的,基于事件的通讯框架,可以再各个平台上运行,关注于效率和速度. 在javascript,ios,android,java中都实现了,可以很好的实现实时 ...

随机推荐

  1. zabbix利用SNMPTrap接收交换机主动告警

    zabbix接收trap的工作流程: snmptrapd 收到trap snmptrapd将trap传递给SNMPTT或调用Perl接收器 SNMPTT或Perl trap接收器解析,格式化并将tra ...

  2. Docker: Jenkins里的pipeline编写基本技巧

    Jenkins里,先新建一个pipeline项目 Pipeline Syntax 在Sample Step里选择需要的插件,如果不存在,就去系统管理,插件管理里,进行安装. 如果源码管理工具用的是gi ...

  3. ES6的Map如何遍历

    projectMap.forEach(function (value, key, map) { //value和key就是map的key,value,map是map本身 });

  4. HTML基础-------HTML标签(1)

    HTML标签(1) h系列(容器级双标签) h系列标签分为六个等级(h1,h2,h3,h4,h5,h6) 语义:给文本添加一个标题 标题重要程度逐级递减,一个页面只能有一个h1级的标签,并且大多数时候 ...

  5. Linux:Day20(上) openssh和CA

    ssh:secure shell protocol,22/tcp,安全的远程登陆 OpenSSH:ssh协议的开源实现: dripbear:另一个开源实现: SSH协议版本 v1:基于CRC-32做M ...

  6. Elasticsearch 通关教程(六): 自动发现机制 - Zen Discoveryedit

    发现方式 Zen discovery是内建的.默认的.用于Elasticsearch的发现模块.它提供了单播和基于文件的发现,可以通过插件扩展到支持云环境和其他形式的发现. Zen Discovery ...

  7. nginx配置默认首页(index.htnl index.htm)全流程(包含遇到问题的解决)

    需求: 自己有个域名,原来直接扔在了服务器的文件夹里(根据客服人员指导),自己玩了一遍nginx的安装部署等操作之后,域名的指向发生了改变,到了nginx成功的界面. 自己抱着极大的好奇心来配置ngi ...

  8. codeforces#1136E. Nastya Hasn't Written a Legend(二分+线段树)

    题目链接: http://codeforces.com/contest/1136/problem/E 题意: 初始有a数组和k数组 有两种操作,一,求l到r的区间和,二,$a_i\pm x$ 并且会有 ...

  9. opencv : imread()的应用

    概述: imread()是opencv中用于读取图片的一个工具.怎么读取图片看似一个很简单的工作,但实际上也有一些细节需要我们注意,以避免在后续的操作中出现bug. 函数原型: 函数原型: Mat i ...

  10. CSS3动画效果transition

    1.transition的浏览器支持情况 IE10+支持,IE6\7\8\9都不支持!目前,其他浏览器最新版本都支持,不需要再加前缀 -webkit- 之类的了 2. 还是一步一步说说怎么用trans ...