码迷,mamicode.com
首页 > Web开发 > 详细

WebSocketSharp中WebSocket类

时间:2019-08-19 11:28:25      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:default   ges   null   been   长度   sub   memory   身份验证   pre   

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 = 102392, Func<Task> onOpen = null, Func<CloseEventArgs, Task> onClose = null, Func<MessageEventArgs, Task> onMessage = null, Func<ErrorEventArgs, Task> onError = null, params string[] protocols);
View Code

参数说明:

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 = 102392, Func<Task> onOpen = null, Func<CloseEventArgs, Task> onClose = null, Func<MessageEventArgs, Task> onMessage = null, Func<ErrorEventArgs, Task> onError = null, params string[] protocols);
View Code

和第一种唯一区别多了一个身份验证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类

标签:default   ges   null   been   长度   sub   memory   身份验证   pre   

原文地址:https://www.cnblogs.com/X-Jonney/p/11375878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!