public async Task Send(byte[] data) { var array = new ArraySegment <byte>(data); await Ws.SendAsync(array, WebSocketMessageType.Binary, false, cancel.Token); Logger?.Invoke("Sended [" + data.Length + "] Byte Data."); }
private void SendSeen(string id) { if (Ws.State == WebSocketState.Open) { Ws.SendAsync(new ArraySegment <byte>(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(new { seen = id, id = FileManager.CreateInstance().Fetch("token") }))) , WebSocketMessageType.Text, true, CancellationToken.None); } }
protected override void SendAuthenticationToken() { string id = FileManager.CreateInstance().Fetch("token"); OutgoingBuffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new { id, username }))); Ws.SendAsync(OutgoingBuffer, WebSocketMessageType.Text, true, default).GetAwaiter().OnCompleted(() => { Console.WriteLine("Connected.."); Console.WriteLine("token has been Send.."); Thread.Sleep(500); OnMessage(); }); }
public Task Send(byte [] bytes, CancellationToken token) { pending.Add(bytes); if (pending.Count == 1) { if (current_send != null) { throw new Exception("WTF, current_send MUST BE NULL IF THERE'S no pending send"); } //Console.WriteLine ("sending {0} bytes", bytes.Length); current_send = Ws.SendAsync(new ArraySegment <byte> (bytes), WebSocketMessageType.Text, true, token); return(current_send); } return(null); }
public Task Pump(CancellationToken token) { current_send = null; pending.RemoveAt(0); if (pending.Count > 0) { if (current_send != null) { throw new Exception("WTF, current_send MUST BE NULL IF THERE'S no pending send"); } //Console.WriteLine ("sending more {0} bytes", pending[0].Length); current_send = Ws.SendAsync(new ArraySegment <byte> (pending [0]), WebSocketMessageType.Text, true, token); return(current_send); } return(null); }
public Task Pump(CancellationToken token) { current_send = null; pending.RemoveAt(0); if (pending.Count > 0) { if (current_send != null) { throw new Exception("current_send MUST BE NULL IF THERE'S no pending send"); } current_send = Ws.SendAsync(new ArraySegment <byte>(pending[0]), WebSocketMessageType.Text, true, token); return(current_send); } return(null); }
public bool TryPumpIfCurrentCompleted(CancellationToken token, [NotNullWhen(true)] out Task?sendTask) { sendTask = null; if (current_send?.IsCompleted == false) { return(false); } current_send = null; if (pending.TryDequeue(out byte[]? bytes)) { current_send = Ws.SendAsync(new ArraySegment <byte>(bytes), WebSocketMessageType.Text, true, token); sendTask = current_send; } return(sendTask != null); }
private void SendMessage() { Console.Write(":"); Message outgoing = new Message { From = Me.Username, MessageType = "text", To = username, Token = new Random(DateTime.Now.Second).Next(999, 10000) + "", Body = Console.ReadLine() }; _messages.Add(outgoing); string id = FileManager.CreateInstance().Fetch("token"); OutgoingBuffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new { id, message = outgoing.Body, username = outgoing.To, token = outgoing.Token }))); Ws.SendAsync(OutgoingBuffer, WebSocketMessageType.Text, true, default).GetAwaiter().OnCompleted(() => { Scene(); }); Thread.Sleep(3000); }