io.on('connect', onConnect);

function onConnect(socket){

  // 只发给sender。 sending to the client
socket.emit('hello', 'can you hear me?', , , 'abc'); // 发给所有人,除了sender。 sending to all clients except sender
socket.broadcast.emit('broadcast', 'hello friends!'); // 发给game房间所有人,除了sender。 sending to all clients in 'game' room except sender
socket.to('game').emit('nice game', "let's play a game"); // 发给game1和/或game2所有人,除了sender。 sending to all clients in 'game1' and/or in 'game2' room, except sender
socket.to('game1').to('game2').emit('nice game', "let's play a game (too)"); // 发给game房间所有人,包含sender。 sending to all clients in 'game' room, including sender
io.in('game').emit('big-announcement', 'the game will start soon'); // 发给域名myNamespacs所有人,包含sender。 sending to all clients in namespace 'myNamespace', including sender
io.of('myNamespace').emit('bigger-announcement', 'the tournament will start soon'); // 发给域名myNamespace里room房间的所有人,包含sender。 sending to a specific room in a specific namespace, including sender
io.of('myNamespace').to('room').emit('event', 'message'); // 发给某一个人 sending to individual socketid (private message)
io.to(`${socketId}`).emit('hey', 'I just met you'); // WARNING: `socket.to(socket.id).emit()` will NOT work, as it will send to everyone in the room
// named `socket.id` but the sender. Please use the classic `socket.emit()` instead. // sending with acknowledgement
socket.emit('question', 'do you think so?', function (answer) {}); // sending without compression
socket.compress(false).emit('uncompressed', "that's rough"); // sending a message that might be dropped if the client is not ready to receive messages
socket.volatile.emit('maybe', 'do you really need it?'); // specifying whether the data to send has binary data
socket.binary(false).emit('what', 'I have no binaries!'); // sending to all clients on this node (when using multiple nodes)
io.local.emit('hi', 'my lovely babies'); // sending to all connected clients
io.emit('an event sent to all connected clients'); };

socket.io emit 常见用法的更多相关文章

  1. socket.io emit callback调用探秘

    socket.io https://socket.io/ https://socket.io/docs/ What Socket.IO is Socket.IO is a library that e ...

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

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

  3. node基于express的socket.io

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

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

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

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

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

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

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

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

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

  8. Socket.IO基础教程

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

  9. SOCKET.IO 的用法 系统API,

    原文:http://www.cnblogs.com/xiezhengcai/p/3956401.html 1. 服务端 io.on('connection',function(socket)); 监听 ...

随机推荐

  1. Java中使用new Date()和System.currentTimeMillis()获取当前时间戳的区别(转)(Java进阶-性能提升)

    在开发过程中,通常很多人都习惯使用new Date()来获取当前时间,使用起来也比较方便,同时还可以获取与当前时间有关的各方面信息,例如获取小时,分钟等等,而且还可以格式化输出,包含的信息是比较丰富的 ...

  2. 在Debian下安装使用Windows下的字体

    转载:http://blog.163.com/lixiangqiu_9202/blog/static/53575037201251224553801/ Debian下的字体不太好看,没有windows ...

  3. Jmeter Summariser report及其可视化

    Jmeter summariser report的设置在:bin/jmeter.properties #------------------------------------------------ ...

  4. caffe版faster-RCNN环境搭建

    faster-rcnn提出论文: <Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks& ...

  5. yii框架:CDbConnection failed to open the DB connection: could not find driver的解决的方法

    这个问题是由于php中缺少pdo mysql造成的. 解决方法是为php加入此扩展.前往你最早的php安装文件,进入ext/pdo_mysql/文件夹下,然后./configure --with-ph ...

  6. 反射 type 的基本用法,动态加载插件

    这里介绍反射的简单实用 MyClass类 public class MyClass { public int Age { get; set; } public string Name { get; s ...

  7. vue2.0 + vux (三)MySettings 页

    1.MySettings.vue <!-- 我的设置 --> <template> <div> <img class="img_1" sr ...

  8. remote connect openshift mysql

    再虚拟机内 rhc port-forward <app-name> 此时,可以在本机 访问 127.0.0.1:8080  登陆 网页, 3306连接sql https://unix.st ...

  9. 在Fedora 23上安装多媒体解码器

    在Fedora 23上安装多媒体解码器 时间:2016-06-25来源:topspeedsnail.com 作者:斗大的熊猫   安装多媒体解码器允许你播放更多格式的音频和视频格式.大多数这些解码器都 ...

  10. LeetCode:二叉树的非递归中序遍历

    第一次动手写二叉树的,有点小激动,64行的if花了点时间,上传leetcode一次点亮~~~ /* inorder traversal binary tree */ #include <stdi ...