From 447c3f408d8c3071c4871840612a0f88317283ba Mon Sep 17 00:00:00 2001 From: Nicholas Westerhausen Date: Wed, 14 Dec 2022 16:57:42 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=9A=91=EF=B8=8F=20revert=20change?= =?UTF-8?q?=20to=20web=20requests,=20too=20unreliable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Metadata/README.md | 6 ++++++ Metadata/manifest.json | 2 +- Metadata/thunderstore.toml | 2 +- README.md | 2 +- docs/changelog.md | 6 ++++++ src/DiscordApi.cs | 42 +++++++++++++++++++++++++++++++------- src/PluginInfo.cs | 2 +- 7 files changed, 51 insertions(+), 11 deletions(-) diff --git a/Metadata/README.md b/Metadata/README.md index f0cdd85..787024a 100644 --- a/Metadata/README.md +++ b/Metadata/README.md @@ -35,6 +35,12 @@ See the [current roadmap](https://github.com/nwesterhausen/valheim-discordconnec ## Changelog +### Version 2.1.6 + +Changes: + +- Regress web request async changes until more reliable method is determined. + ### Version 2.1.5 Fixes: diff --git a/Metadata/manifest.json b/Metadata/manifest.json index ec388fc..400d3ee 100644 --- a/Metadata/manifest.json +++ b/Metadata/manifest.json @@ -1,6 +1,6 @@ { "name": "DiscordConnector", - "version_number": "2.1.5", + "version_number": "2.1.6", "website_url": "https://discordconnector.valheim.nwest.games/", "description": "Connects your Valheim server to a Discord webhook. Works for both dedicated and client-hosted servers.", "dependencies": ["denikson-BepInExPack_Valheim-5.4.1901"] diff --git a/Metadata/thunderstore.toml b/Metadata/thunderstore.toml index d3976d6..c2dcef1 100644 --- a/Metadata/thunderstore.toml +++ b/Metadata/thunderstore.toml @@ -4,7 +4,7 @@ schemaVersion = "0.0.1" [package] namespace = "nwesterhausen" name = "DiscordConnector" -versionNumber = "2.1.5" +versionNumber = "2.1.6" description = "Connects your Valheim server to a Discord webhook. Works for both dedicated and client-hosted servers." websiteUrl = "https://discordconnector.valheim.nwest.games/" containsNsfwContent = false diff --git a/README.md b/README.md index 706b26d..efb398f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![Build](https://github.com/nwesterhausen/valheim-discordconnector/actions/workflows/dotnet.yml/badge.svg)](https://github.com/nwesterhausen/valheim-discordconnector/actions/workflows/dotnet.yml) [![Publish](https://github.com/nwesterhausen/valheim-discordconnector/actions/workflows/publish.yml/badge.svg)](https://github.com/nwesterhausen/valheim-discordconnector/actions/workflows/publish.yml) [![GitHub release (latest by date)](https://img.shields.io/github/v/release/nwesterhausen/valheim-discordconnector?label=Github%20Release&style=flat&labelColor=%2332393F)](https://github.com/nwesterhausen/valheim-discordconnector/releases/latest) -[![Thunderstore.io](https://img.shields.io/badge/Thunderstore.io-2.1.5-%23375a7f?style=flat&labelColor=%2332393F)](https://valheim.thunderstore.io/package/nwesterhausen/DiscordConnector/) +[![Thunderstore.io](https://img.shields.io/badge/Thunderstore.io-2.1.6-%23375a7f?style=flat&labelColor=%2332393F)](https://valheim.thunderstore.io/package/nwesterhausen/DiscordConnector/) [![NexusMods](https://img.shields.io/badge/NexusMods-2.0.6-%23D98F40?style=flat&labelColor=%2332393F)](https://www.nexusmods.com/valheim/mods/1551/) Connect your Valheim server to Discord. ([See website for installation or configuration instructions](https://discordconnector.valheim.nwest.games/)). This plugin is largely based on [valheim-discord-notifier](https://github.com/aequasi/valheim-discord-notifier), but this plugin supports randomized messages, muting players, and Discord message embeds. diff --git a/docs/changelog.md b/docs/changelog.md index 3992a0e..d4b9928 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -2,6 +2,12 @@ A full changelog of changes, dating all the way back to the first release. +## Version 2.1.6 + +Changes: + +- Regress web request async changes until more reliable method is determined. + ## Version 2.1.5 Fixes: diff --git a/src/DiscordApi.cs b/src/DiscordApi.cs index a2669d6..a2f8407 100644 --- a/src/DiscordApi.cs +++ b/src/DiscordApi.cs @@ -117,20 +117,48 @@ private static void SendSerializedJson(string serializedJson) return; } + // Responsible for sending a JSON string to the webhook. + byte[] byteArray = Encoding.UTF8.GetBytes(serializedJson); + // Create a web request to send the payload to discord WebRequest request = WebRequest.Create(Plugin.StaticConfig.WebHookURL); request.Method = "POST"; request.ContentType = "application/json"; + request.ContentLength = byteArray.Length; - request.GetRequestStreamAsync().ContinueWith(requestStreamTask => + // Dispatch the request to discord and the response processing to an async task + Task.Run(() => { - // Write JSON payload into request - using StreamWriter writer = new(requestStreamTask.Result); - writer.WriteAsync(serializedJson).ContinueWith(_ => + // We have to write the data to the request + Stream dataStream = request.GetRequestStream(); + dataStream.Write(byteArray, 0, byteArray.Length); + dataStream.Close(); + + // Wait for a response to the web request + WebResponse response = request.GetResponse(); + if (Plugin.StaticConfig.DebugHttpRequestResponse) { - request.GetResponseAsync(); - }); - }); + Plugin.StaticLogger.LogDebug($"Request Response Short Code: {((HttpWebResponse)response).StatusDescription}"); + } + + // Get the stream containing content returned by the server. + // The using block ensures the stream is automatically closed. + using (dataStream = response.GetResponseStream()) + { + // Open the stream using a StreamReader for easy access. + StreamReader reader = new StreamReader(dataStream); + // Read the content. + string responseFromServer = reader.ReadToEnd(); + // Display the content. + if (Plugin.StaticConfig.DebugHttpRequestResponse) + { + Plugin.StaticLogger.LogDebug($"Full response: {responseFromServer}"); + } + } + + // Close the response. + response.Close(); + }).ConfigureAwait(false); } } diff --git a/src/PluginInfo.cs b/src/PluginInfo.cs index 85fbe52..a96b26c 100644 --- a/src/PluginInfo.cs +++ b/src/PluginInfo.cs @@ -17,7 +17,7 @@ internal static class PluginInfo { public const string PLUGIN_ID = "games.nwest.valheim.discordconnector"; public const string PLUGIN_NAME = "Valheim Discord Connector"; - public const string PLUGIN_VERSION = "2.1.5"; + public const string PLUGIN_VERSION = "2.1.6"; public const string PLUGIN_REPO_SHORT = "github: nwesterhausen/valheim-discordconnector"; public const string PLUGIN_AUTHOR = "Nicholas Westerhausen"; public const string SHORT_PLUGIN_ID = "discordconnector";