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));
}

最新文章

  1. ES6之数组扩展方法【一】(相当好用)
  2. ASP.NET MVC Routing学习笔记(一)
  3. webform membership2.0注入debug
  4. Leetcode: Evaluate Division
  5. 机器学习技法-AdaBoost元算法
  6. HDU 5285 wyh2000 and pupil
  7. delphi非IE内核浏览器控件TEmbeddedChrome下载|TEmbeddedChrome代码
  8. iOS中谓词的使用
  9. 基于QTcpSocket和QTcpServer的Tcp通讯以及QDataStream序列化数据
  10. ecma6的学习好网站
  11. 打印等腰三角形as3
  12. 什么是B-Tree
  13. 【Hibernate】版本错误 org/hibernate/Query : Unsupported major.minor version 52.0
  14. [物理学与PDEs]第1章第6节 电磁场的标势与矢势 6.2 电磁场的标势与矢势
  15. 史上最全 原生javascript的知识总结,适合新手及查资料用!
  16. Android线程
  17. android实现3D Gallery 轮播效果,触摸时停止轮播
  18. Charles做代理的Map Remote路径配置
  19. 4月第4周业务风控关注 | 网络犯罪经济每年1.5万亿美元 GDP居全球第12位
  20. JavaScript跨源资源共享

热门文章

  1. python之turtle使用:画一颗美美哒的树
  2. Python全栈工程师(Python3 所有基础内容 0-0)
  3. 如何保证MQ的顺序性?比如Kafka
  4. 冰多多团队-第三次Scrum会议
  5. [Gamma]Scrum Meeting#2
  6. js Map的使用
  7. Flask 学习(三)路由介绍
  8. EasyNVR网页摄像机无插件H5、谷歌Chrome直播方案中使用Onvif协议控制视频设备预置位转动
  9. phpspreadsheet 中文文档(五)节约内存+PHPExcel迁移
  10. DevExpress XtraReport - 动态加载报表布局模板