websocket-sharp.clone,html
Version=1.0.2.39869web
WebSocket由方法調用事件改成實例化委託調用,兩種構造安全
1.構造函數websocket
第一種cookie
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);
參數說明:less
url:要鏈接websocket url鏈接字符串socket
cancellationToken:用於取消websocket操做,線程ide
fragmentSize:消息包大小函數
onOpen:創建鏈接時調用的委託url
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);
和第一種惟一區別多了一個身份驗證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代理