Skip to content

Commit

Permalink
Output to a List of rest servers. URLEncode and Decode the message.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack Piispanen committed Nov 7, 2013
1 parent e22798d commit a6b1be4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
5 changes: 3 additions & 2 deletions MultiServerChat/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

namespace MultiServerChat
{
class ConfigFile
internal class ConfigFile
{
public string RestURL = "http://127.0.0.1:7878/msc";
public List<string> RestURLs = new List<string> {"http://127.0.0.1:7878/msc"};

public string Token = "abcdef";
public string ChatFormat = "[{0}]{2}{3}{4}: {5}";
public bool SendChat = true;
Expand Down
36 changes: 21 additions & 15 deletions MultiServerChat/MultiServerChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using HttpServer;
using Newtonsoft.Json;
using Rests;
Expand Down Expand Up @@ -79,7 +80,8 @@ private object RestChat(RestVerbs verbs, IParameterCollection parameters, Secure

if (!string.IsNullOrWhiteSpace(parameters["message"]))
{
var bytes = Convert.FromBase64String(parameters["message"]);
var decoded = HttpUtility.UrlDecode(parameters["message"]);
var bytes = Convert.FromBase64String(decoded);
var str = Encoding.UTF8.GetString(bytes);
var message = Message.FromJson(str);
TShock.Utils.Broadcast(message.Text, message.Red, message.Green, message.Blue);
Expand Down Expand Up @@ -127,22 +129,26 @@ private void OnChat(ServerChatEventArgs args)

var bytes = Encoding.UTF8.GetBytes(message.ToString());
var base64 = Convert.ToBase64String(bytes);

var uri = String.Format("{0}?message={1}&token={2}", Config.RestURL, base64, Config.Token);

try
{
var request = (HttpWebRequest)WebRequest.Create(uri);
using (var res = request.GetResponse())
{}
failure = false;
}
catch (Exception)
var encoded = HttpUtility.UrlEncode(base64);
foreach (var url in Config.RestURLs)
{
if (!failure)
var uri = String.Format("{0}?message={1}&token={2}", url, encoded, Config.Token);

try
{
var request = (HttpWebRequest) WebRequest.Create(uri);
using (var res = request.GetResponse())
{
}
failure = false;
}
catch (Exception)
{
Log.Error("Failed to make request to other server, server is down?");
failure = true;
if (!failure)
{
Log.Error("Failed to make request to other server, server is down?");
failure = true;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions MultiServerChat/MultiServerChat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down

0 comments on commit a6b1be4

Please sign in to comment.