websocket-sharp.clone,

Version=1.0.2.39869

WebSocket由方法调用事件改为实例化委托调用,两种构造

1.构造函数

第一种

  1. //
  2. // 摘要:
  3. // Initializes a new instance of the WebSocketSharp.WebSocket class with the specified
  4. // WebSocket URL and subprotocols.
  5. //
  6. // 参数:
  7. // url:
  8. // A System.String that represents the WebSocket URL to connect.
  9. //
  10. // cancellationToken:
  11. // The System.Threading.CancellationToken used to cancel websocket operations.
  12. //
  13. // fragmentSize:
  14. // Set the size of message packages. Smaller size equals less memory overhead when
  15. // sending streams.
  16. //
  17. // onOpen:
  18. // Delegate to be invoked when the WebSocket connection has been established.
  19. //
  20. // onClose:
  21. // Delegate to be invoked when the WebSocket connection has been closed.
  22. //
  23. // onError:
  24. // Delegate to be invoked when the WebSocketSharp.WebSocket gets an error.
  25. //
  26. // protocols:
  27. // An array of System.String that contains the WebSocket subprotocols if any. Each
  28. // value of protocols must be a token defined in http://tools.ietf.org/html/rfc2616#section-2.2.
  29. //
  30. // onMessage:
  31. // Delegate to be invoked when the WebSocketSharp.WebSocket receives a message.
  32. //
  33. // 异常:
  34. // T:System.ArgumentException:
  35. // url is invalid.
  36. // -or-
  37. // protocols is invalid.
  38. //
  39. // T:System.ArgumentNullException:
  40. // url is null.
  41. public WebSocket(string url, CancellationToken cancellationToken = default(CancellationToken), int fragmentSize = , Func<Task> onOpen = null, Func<CloseEventArgs, Task> onClose = null, Func<MessageEventArgs, Task> onMessage = null, Func<ErrorEventArgs, Task> onError = null, params string[] protocols);

参数说明:

url:要连接websocket url连接字符串

cancellationToken:用于取消websocket操作,线程

fragmentSize:消息包大小

onOpen:建立连接时调用的委托

onClose:连接关闭时调用的位图

onError:委托在WebSocketSharp时调用。WebSocket得到一个错误。

protocols:协议,每一个协议的值必须是在http://tools.ietf.org/html/rfc2616#section-2.2中定义的令牌

onMessage:WebSocket接收到一条消息

异常:

System.ArgumentException:URL无效或协议无效

System.ArgumentNullException:URL为空

第二种

  1. //
  2. // 摘要:
  3. // Initializes a new instance of the WebSocketSharp.WebSocket class with the specified
  4. // WebSocket URL and subprotocols.
  5. //
  6. // 参数:
  7. // url:
  8. // A System.String that represents the WebSocket URL to connect.
  9. //
  10. // sslAuthConfiguration:
  11. // A ClientSslAuthConfiguration for securing the connection.
  12. //
  13. // cancellationToken:
  14. // The System.Threading.CancellationToken used to cancel websocket operations.
  15. //
  16. // fragmentSize:
  17. //
  18. // onOpen:
  19. // Delegate to be invoked when the WebSocket connection has been established.
  20. //
  21. // onClose:
  22. // Delegate to be invoked when the WebSocket connection has been closed.
  23. //
  24. // onError:
  25. // Delegate to be invoked when the WebSocketSharp.WebSocket gets an error.
  26. //
  27. // protocols:
  28. // An array of System.String that contains the WebSocket subprotocols if any. Each
  29. // value of protocols must be a token defined in http://tools.ietf.org/html/rfc2616#section-2.2.
  30. //
  31. // 异常:
  32. // T:System.ArgumentException:
  33. // url is invalid.
  34. // -or-
  35. // protocols is invalid.
  36. //
  37. // T:System.ArgumentNullException:
  38. // url is null.
  39. public WebSocket(string url, ClientSslConfiguration sslAuthConfiguration, CancellationToken cancellationToken = default(CancellationToken), int fragmentSize = , Func<Task> onOpen = null, Func<CloseEventArgs, Task> onClose = null, Func<MessageEventArgs, Task> onMessage = null, Func<ErrorEventArgs, Task> onError = null, params string[] protocols);

和第一种唯一区别多了一个身份验证sslAuthConfiguration

2.属性

Cookies:获取websocket中http cookie

Extensions:获取websocket扩展

IsSecure:获取是否安全连接

Origin:获取或设置http标头

Protocol:获取子协议

ReadyState:获取联系状态,分别是Connecting、Open、Closing和Closed四个状态

WaitTime:获取设置ping和关闭响应时间,毫秒

3.方法

Close()、Close(CloseStatusCode code)、Close(CloseStatusCode code, string reason);:分为直接关闭、状态码关闭、状态码+关闭原因三种方式

bool Connect():建立连接

Dispose():关闭连接,条件是状态码是Away

Uri GetUrl():获取URL

bool IsAlive():是否活动状态

bool Ping()、bool Ping(string message):发送ping、发送指定ping消息

boo Send():发送数据分为二进制、数据流、字符串以及数据流+长度四种参数

SetCookie(Cookie cookie):设置cookie

SetCredentials(string username, string password, bool preAuth):设置HTTP身份验证用户名、密码和认证

SetProxy(string url, string username, string password):设置HTTP代理

WebSocketSharp中WebSocket类的更多相关文章

  1. 如何在WebSocket类中访问Session

    我最近正在做一个基于websocket的webQQ,最后代码会开源带github上,所以过程中我就不贴所有的代码啦~就贴问题的关键. 我在WebSocket里发消息的时候需要用到session,因为在 ...

  2. java中websocket的应用

    在上一篇文章中,笔者简要介绍了websocket的应用场景及优点,戳这里 这篇文章主要来介绍一下在java项目中,特别是java web项目中websocket的应用. 场景:我做了一个商城系统,跟大 ...

  3. Spring Boot WebFlux-08——WebFlux 中 WebSocket 实现通信

    第08课:WebFlux 中 WebSocket 实现通信 前言 WebFlux 该模块中包含了对反应式 HTTP.服务器推送事件和 WebSocket 的客户端和服务器端的支持.这里我们简单实践下 ...

  4. Python中的类、对象、继承

    类 Python中,类的命名使用帕斯卡命名方式,即首字母大写. Python中定义类的方式如下: class 类名([父类名[,父类名[,...]]]): pass 省略父类名表示该类直接继承自obj ...

  5. 基础知识(05) -- Java中的类

    Java中的类 1.类的概念 2.类中的封装 3.对象的三大特征 4.对象状态 5.类与类之间的关系 ------------------------------------------------- ...

  6. Objective-C中的类目,延展,协议

    Objective-C中的类目(Category),延展(Extension),协议(Protocol)这些名词看起来挺牛的,瞬间感觉OC好高大上.在其他OOP语言中就没见过这些名词,刚看到这三个名词 ...

  7. CSS中伪类及伪元素用法详解

    CSS中伪类及伪元素用法详解   伪类的分类及作用: 注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的 ...

  8. Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法

    Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法 如下面所示,同时导入这两个,会提示其中一个与另一个产生冲突. 1i ...

  9. C#中timer类的用法

    C#中timer类的用法 关于C#中timer类  在C#里关于定时器类就有3个   1.定义在System.Windows.Forms里   2.定义在System.Threading.Timer类 ...

随机推荐

  1. iOS 如何判断一个点在某个指定区域中

    在iOS 开发中会遇到 判断位置的情况 iOS 自己都有函数实现的这些功能. 判断一个点是否在这个rect区域中 bool CGRectContainsPoint(CGRect rect,CGPoin ...

  2. 使用SAP Cloud Platform Leonardo机器学习提取图片的特征向量

    选中一个需要进行测试的Leonardo机器学习服务,点击Configure Environments: 因为我不想使用sandbox环境,所以我选择了eu10这个region: 维护clientid和 ...

  3. LNMP环境搭建BBS论坛及伪静态

    我们在mysql备份 LNMP环境中的数据库迁移为独立的服务器的基础上搭建BBS论坛:  [root@bqh-117 ~]# mysql -uroot -p123456 Welcome to the ...

  4. java基本数据类型包装

    1. 2. 左边的是对象,自动装箱为对象,右边的是基本的数据类型. 3. 如果m,n换成128就超出范围,结果就不一样. 是因为把在这区间内的值都放在了常量池里面. Integer m = Integ ...

  5. IDEA实用教程(三)

    4. JDK环境的配置 1) 进入JDK配置界面 2) 创建JDK环境 3) 选择本地JDK的安装位置 4) 保存配置 点击右下角的Apply后,再点击OK保存配置

  6. jquery基础知识2

    1.js和jquery对象的转换 js==>jquery对象 $(js对象) jquery==>js jq对象[index] jq对象.get(index) <!DOCTYPE ht ...

  7. ASP.NET 下配置请求大小、请求时间等参数

    在邮件发送系统或者其他一些传送文件的网站中,用户传送文件的大小是有限制的,因为这样不但可以节省服务器的空间,还可以提高传送文件的速度.下面介绍如何在Web.Config文件中配置限制上传文件大小与时间 ...

  8. JSOI2012 玄武密码 和 HDU2222 Keywords Search

    玄武密码 给若干模式串和一个文本串.求每个模式串在文本串上能匹配的最大前缀长度. N<=10^7,M<=10^5,每一段文字的长度<=100. jklover的题解 将模式串建成一个 ...

  9. Apache查看连接数和限制当前的连接数

    在wamp环境下查看apche连接数和限制当前的连接数 httpd_mpm.conf文件在你apache安装上当的\\conf\\extra中,还在就是在要apache httpd.conf中把#In ...

  10. ElementUI2.0组件库el-table表格组件如何自定义表头?

    效果图: npm run dev 编译项目之后,报错,要使用jsx语法需要先安装编译插件 1.安装下列安装包 npm install babel-plugin-syntax-jsx --save-de ...