Skip to content

Commit

Permalink
fix: 🚑️ revert change to web requests, too unreliable
Browse files Browse the repository at this point in the history
  • Loading branch information
nwesterhausen committed Dec 14, 2022
1 parent 5b1a121 commit 447c3f4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
6 changes: 6 additions & 0 deletions Metadata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Metadata/manifest.json
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion Metadata/thunderstore.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
42 changes: 35 additions & 7 deletions src/DiscordApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/PluginInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 447c3f4

Please sign in to comment.