websocket-shap 函数Broadcast的使用方法
Broadcast:在websocket-shap函数的定义是:向WebSocket服务中的每个客户端发送数据,类似于广播的效果
如果要使用异步发送,可使用BroadcastAsync函数。
在源码中的代码片段如下:
//向WebSocket服务中的每个客户端发送string数据
public void Broadcast (string data)
{
if (_state != ServerState.Start) {
var msg = "管理器的当前状态为“未启动”|The current state of the manager is not Start.";
throw new InvalidOperationException (msg);
} if (data == null)
throw new ArgumentNullException ("data"); byte[] bytes;
if (!data.TryGetUTF8EncodedBytes (out bytes)) {
var msg = "无法进行utf-8编码|It could not be UTF-8-encoded.";
throw new ArgumentException (msg, "data");
} if (bytes.LongLength <= WebSocket.FragmentLength)
broadcast (Opcode.Text, bytes, null);
else
broadcast (Opcode.Text, new MemoryStream (bytes), null);
}
//向WebSocket服务中的每个客户端发送stream数据
public void Broadcast (Stream stream, int length)
{
if (_state != ServerState.Start) {
var msg = "The current state of the manager is not Start.";
throw new InvalidOperationException (msg);
} if (stream == null)
throw new ArgumentNullException ("stream"); if (!stream.CanRead) {
var msg = "It cannot be read.";
throw new ArgumentException (msg, "stream");
} if (length < ) {
var msg = "Less than 1.";
throw new ArgumentException (msg, "length");
} var bytes = stream.ReadBytes (length); var len = bytes.Length;
if (len == ) {
var msg = "No data could be read from it.";
throw new ArgumentException (msg, "stream");
} if (len < length) {
_log.Warn (
String.Format (
"Only {0} byte(s) of data could be read from the stream.",
len
)
);
} if (len <= WebSocket.FragmentLength)
broadcast (Opcode.Binary, bytes, null);
else
broadcast (Opcode.Binary, new MemoryStream (bytes), null);
}
//c# 调用方式:
private void Broadcast(string msg, string type = "")
{
var data = new JsonDto() { content = msg, type = type, name = "" };
//发送广播数据,每个在线的客户端都可以收到信息
Sessions.Broadcast(Newtonsoft.Json.JsonConvert.SerializeObject(data));
}
websocket-shap 函数Broadcast的使用方法的更多相关文章
- Vue框架Element的事件传递broadcast和dispatch方法分析
前言 最近在学习饿了么的Vue前端框架Element,发现其源码中大量使用了$broadcast和$dispatch方法,而Element使用的是Vue2.0版本,众所周知在Vue 1.0升级到2.0 ...
- ES5——函数,对象,方法,this
JS由表达式和语句组成 表达式:计算出一个值,但并不进行任何操作,不改变计算机运行状态 语句:包括 声明语句,赋值语句,控制结构 函数,对象,方法,this 数组和对象:是两个非常重要的数据类型 函数 ...
- ThinkPHP3.2 G函数代码及 使用方法
ThinkPHP3.2 G函数代码及 使用方法 代码: // 内存是否可调用 define('MEMORY_LIMIT_ON',function_exists('memory_get_usage')) ...
- 利用Objective-C运行时hook函数的三种方法
版权声明:转载请注明出处:http://blog.csdn.net/hursing 方法一,hook已有公开头文件的类: 首先写一个Utility函数: #import <objc/runtim ...
- PHP使用feof()函数读文件的方法
这篇文章主要介绍了PHP使用feof()函数读文件的方法,以实例形式对比了正确与错误的用法,阐明了feof()函数的使用技巧,需要的朋友可以参考下 本文实例讲述了PHP使用feof()函数读文件的方法 ...
- [Effective JavaScript 笔记]第28条:不要信赖函数对象的toString方法
js函数有一个非凡的特性,即将其源代码重现为字符串的能力. (function(x){ return x+1 }).toString();//"function (x){ return x+ ...
- php不使用copy()函数复制文件的方法
本文实例讲述了php不使用copy()函数复制文件的方法.分享给大家供大家参考.具体如下:下面的代码不使用php内置的copy函数,直接通过文件读取写入的操作方式复制文件 <?php funct ...
- C++向main函数传递参数的方法(实例已上传至github)
通常情况下,我们定义的main函数都只有空形参列表: int main(){...} 然而,有时我们确实需要给mian传递实参,一种常见的情况是用户设置一组选项来确定函数所要执行的操作.例如,假定ma ...
- 【JS】<a>标签调用js中函数的几种方法
我们常用的在a标签中有点击事件: a href="javascript:js_method();" 这是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问题,而 ...
随机推荐
- [Gamma]Scrum Meeting#3
github 本次会议项目由PM召开,时间为5月28日晚上10点30分 时长10分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写博客,组织例会 撰写博客,组织例会 swoip 前端显示屏幕,翻译 ...
- IntelliJ IDEA 调试 Apache RocketMQ 源码
克隆源码 mvn clean install 执行命令,跳过测试.我在执行测试的时候有时候卡住,所以干脆就跳过了. mvn clean install -DskipTests 准备环境 在 D 盘创建 ...
- Spring 事务小结
@Override@Transactionalpublic void add() { this.in();} public void in(){ NyOrder nyOrder=new NyOrder ...
- 解析prototxt文件的python库 prototxt-parser(使用parsy自定义文件格式解析)
解析prototxt文件的python库 prototxt-parser https://github.com/yogin16/prototxt_parser https://test.pypi.or ...
- 通过mitmproxy爬取APP的数据
安装: https://mitmproxy.org/ 小米安装证书 设置->系统安全->从存储设备安装->选择*.pem文件 模拟器安装证书 (请从C:\Users\John\.mi ...
- 万恶技术系列笔记-jupyter工作路径和源文件打开方式
万恶技术系列笔记-jupyter工作路径和源文件打开方式 脚本文件,ipynb的正确打开姿势: ipynb不能直接打开,需要复制到工作路径.例如 10_monkeys_model_1.ipynb ...
- 【转载】 os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ["CUDA_VISIBLE_DEVICES"] = "0" (---------tensorflow中设置GPU可见顺序和选取)
原文地址: https://blog.csdn.net/Jamesjjjjj/article/details/83414680 ------------------------------------ ...
- 泡泡一分钟:Robust Attitude Estimation Using an Adaptive Unscented Kalman Filter
张宁 Robust Attitude Estimation Using an Adaptive Unscented Kalman Filter 使用自适应无味卡尔曼滤波器进行姿态估计链接:https: ...
- window 安装使用npm
[安装步骤] 一.安装node.js 1.前往node.js官网下载并安装工具,这里安装路径选到D盘,D:\nodejs 安装完毕后在命令行输入以下命令测试是否安装成功,正确会出现版本号 npm -v ...
- Web自动化遇到shadowDOM节点操作(还没试)
近期有同学在做web自动化的时候,发现页面上有些元素,在selenium中无法通过xpath来定位,各种原因找了半天,都没找到解决方案. 最后发现元素在一个叫做shadow-root的节点下面. 如下 ...