public SendAsync ( |
||
fileInfo |
/// A |
|
completed | Action |
/// An |
Результат | void |
public void Send(byte[] bytes) { if (ws != null && ws.IsConnected) { ws.SendAsync(bytes, null); statsSumary.AddBytesSent(bytes.Length); } else { DebugClient("not connected"); } }
void Start() { ws = new WebSocket("ws://echo.websocket.org"); ws.OnOpen += OnOpenHandler; ws.OnMessage += OnMessageHandler; ws.OnClose += OnCloseHandler; stateMachine.AddHandler(State.Running, () => { new Wait(this, 3, () => { ws.ConnectAsync(); }); }); stateMachine.AddHandler(State.Connected, () => { stateMachine.Transition(State.Ping); }); stateMachine.AddHandler(State.Ping, () => { new Wait(this, 3, () => { ws.SendAsync("This WebSockets stuff is a breeze!", OnSendComplete); }); }); stateMachine.AddHandler(State.Pong, () => { new Wait(this, 3, () => { ws.CloseAsync(); }); }); stateMachine.Run(); }
protected override void Run() { if (ws.ReadyState != WebSocketState.Open || !connection.IsConnected) { return; } string message; while (queue.TryDequeue(out message)) { ws.SendAsync(message, b => { }); } }
public override Task SendAsync(ArraySegment <byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken) { if (m_state != WebSocketState.Open) { throw new InvalidOperationException(); } var sendTCS = new TaskCompletionSource <object>(); var data = new byte[buffer.Count]; for (int i = 0; i < buffer.Count; i++) { data[i] = buffer.Array[i + buffer.Offset]; } try { m_ws.SendAsync(data, sent => { if (sent) { sendTCS.SetResult(null); } else { sendTCS.SetException(new WebSocketException(WebSocketNativeError_FailedToSend)); } }); } catch (Exception ex) { sendTCS.SetException(new WebSocketException(WebSocketNativeError_Unknown, ex)); } return(sendTCS.Task); }
private NetworkStatus send() { switch (connect()) { case NetworkStatus.Error: { return(_sender.Result(NetworkStatus.Error, _apiCmd, _error, _repeater)); } case NetworkStatus.Ok: { if (_sender.send.in_progress) { if (!_sender.result.sended && _error == null) { if (DateTime.Now.Subtract(_sender.send.retry_defer) > _sendTimeout) { _sender.result = new SendResult(); _sender.send.retry_defer = DateTime.MaxValue; _error = "Timeout, while sending message"; } return(NetworkStatus.InProgress); } else { if (_error != null) { Debug.Log("Send Error: " + _sender.send.retry_count.ToString() + " " + _error); _sender.send.in_progress = false; if (_sender.send.retry_count >= _retryCount) { Debug.Log("Send Exit"); unregister(); return(_sender.Result(NetworkStatus.Error, _apiCmd, _error, _repeater)); } else { Debug.Log("Send Retry: " + _sender.send.retry_count.ToString()); _sender.send.retry_count += 1; _sender.send.retry_defer = DateTime.Now; return(NetworkStatus.InProgress); } } else { if (_sender.result.sended) { //Debug.Log("Send OK"); return(_sender.Result(NetworkStatus.Ok, _apiCmd, _error, _repeater)); } else { Debug.Log("Send ERROR"); unregister(); return(_sender.Result(NetworkStatus.Error, _apiCmd, _error, _repeater)); } } } } if (_sender.send.retry_count == 1 || DateTime.Now.Subtract(_sender.send.retry_defer) > _senderRetryDelay) { if (!_sender.currentMsg.Dirty) { Debug.Log("Send Send: " + _sender.send.retry_count + ", id: " + _sender.currentMsg.Id + ", cmd: " + _sender.currentMsg.Cmd); _error = null; _sender.result = new SendResult(); _sender.send.in_progress = true; _sender.send.retry_defer = DateTime.Now; _socket.SendAsync(_sender.currentMsg.GetString(), _sender.result.isSent); return(NetworkStatus.InProgress); } else { Debug.Log("Send Already received: " + _sender.send.retry_count + ", id: " + _sender.currentMsg.Id + ", cmd: " + _sender.currentMsg.Cmd); return(_sender.Result(NetworkStatus.Ok, _apiCmd, _error, _repeater)); } } break; } case NetworkStatus.InProgress: { break; } } return(NetworkStatus.InProgress); }
public void SendMsg_1(MessageBase msg) { byte[] data = SerializeMsg(msg); sock.SendAsync(data, null); }
/// <summary> /// Send message /// </summary> /// <param name="buffer"></param> public void Send(byte[] buffer) { socket.SendAsync(buffer, null); }