/// <summary> /// Sends the specified message. /// </summary> /// <param name="message">The message.</param> /// <returns></returns> public async Task Send(GodotMessage message) { try { if (_webSocket.State == WebSocketState.Open) { await _webSocket.SendAsync(new ArraySegment <byte>(message.Buffer, 0, message.WritePosition), WebSocketMessageType.Binary, true, CancellationToken.None); } } catch (WebSocketException) { await Close(); } }
/// <summary> /// Reads a message sent by the client. /// </summary> /// <param name="message">The message that was read.</param> /// <returns>true if a message was received, otherwise false.</returns> public async Task <bool> Read(GodotMessage message) { if (_isClosed || _webSocket.State == WebSocketState.Aborted || _webSocket.State == WebSocketState.Closed) { return(false); } message.ResetPosition(); Status = await _webSocket.ReceiveAsync(new ArraySegment <byte>(message.Buffer, 0, message.Buffer.Length), CancellationToken.None); if (Status.CloseStatus.HasValue) { return(false); } LastMessageReceivedAt = DateTime.Now; return(true); }
/// <inheritdoc/> public async Task <bool> HandleMessage(IPlayerSession session, GodotMessage message) { await session.Connection.BuildAndSend(OutgoingMessages.Pong, reply => { }); return(true); }