diff --git a/Assets/HOTK/HOTK_TrackedDeviceManager.cs b/Assets/HOTK/HOTK_TrackedDeviceManager.cs
index 25a7bbf..60de444 100644
--- a/Assets/HOTK/HOTK_TrackedDeviceManager.cs
+++ b/Assets/HOTK/HOTK_TrackedDeviceManager.cs
@@ -252,6 +252,6 @@ void Log(string text)
///
void LogWarning(string text)
{
- TwitchChatTester.Instance.AddSystemNotice(text, true);
+ TwitchChatTester.Instance.AddSystemNotice(text, TwitchIRC.NoticeColor.Yellow);
}
}
diff --git a/Assets/HOTK/Twitch/TwitchChatTester.cs b/Assets/HOTK/Twitch/TwitchChatTester.cs
index 9b98349..444a5af 100644
--- a/Assets/HOTK/Twitch/TwitchChatTester.cs
+++ b/Assets/HOTK/Twitch/TwitchChatTester.cs
@@ -67,13 +67,18 @@ public void ToggleConnect()
{
if (ChannelBox != null && ChannelBox.text != "")
{
+ if (ChannelBox.text.Contains(" "))
+ {
+ AddSystemNotice("Channel name invalid!", TwitchIRC.NoticeColor.Red);
+ return;
+ }
UsernameBox.interactable = false;
OAuthBox.interactable = false;
ChannelBox.interactable = false;
ConnectButtonText.text = "Press to Disconnect";
_connected = true;
- OnChatMsg(ToTwitchNotice(string.Format("Logging into #{0} as {1}!", ChannelBox.text, UsernameBox.text)));
+ OnChatMsg(TwitchIRC.ToTwitchNotice(string.Format("Logging into #{0} as {1}!", ChannelBox.text, UsernameBox.text)));
IRC.NickName = UsernameBox.text;
IRC.Oauth = OAuthBox.text;
IRC.ChannelName = ChannelBox.text.ToLower();
@@ -82,11 +87,11 @@ public void ToggleConnect()
IRC.MessageRecievedEvent.AddListener(OnChatMsg);
IRC.StartIRC();
}
- else OnChatMsg(ToTwitchNotice("Unable to Connect: Enter a Valid Channel Name!", true));
+ else AddSystemNotice("Unable to Connect: Enter a Valid Channel Name!", TwitchIRC.NoticeColor.Red);
}
- else OnChatMsg(ToTwitchNotice("Unable to Connect: Enter a Valid OAuth Key! http://www.twitchapps.com/tmi/", true));
+ else AddSystemNotice("Unable to Connect: Enter a Valid OAuth Key! http://www.twitchapps.com/tmi/", TwitchIRC.NoticeColor.Red);
}
- else OnChatMsg(ToTwitchNotice("Unable to Connect: Enter a Valid Username!", true));
+ else AddSystemNotice("Unable to Connect: Enter a Valid Username!", TwitchIRC.NoticeColor.Red);
}
else
{
@@ -98,7 +103,7 @@ public void ToggleConnect()
_connected = false;
IRC.MessageRecievedEvent.RemoveListener(OnChatMsg);
IRC.enabled = false;
- OnChatMsg(ToTwitchNotice("Disconnected!", true));
+ OnChatMsg(TwitchIRC.ToTwitchNotice("Disconnected!", TwitchIRC.NoticeColor.Red));
}
}
@@ -114,12 +119,16 @@ private void OnChatMsg(string msg)
switch (mode)
{
case "NOTICE":
+ // Compatability with real Twitch System messages
if (nickname == "tmi.twitch.tv")
{
nickname = "Twitch";
if (chat.StartsWith("Error"))
channel = "System-Red";
+ else if (chat == "Login unsuccessful")
+ channel = "System-Red";
}
+ // Convert Notice to Name Color
switch (channel)
{
case "System-Green":
@@ -129,7 +138,7 @@ private void OnChatMsg(string msg)
AddMsg(nickname, ColorToHex(new Color(1f, 0f, 0f)), chat);
break;
case "System-Blue":
- AddMsg(nickname, ColorToHex(new Color(0.2f, 0.4f, 1f)), chat);
+ AddMsg(nickname, ColorToHex(new Color(0f, 0.4f, 1f)), chat);
break;
case "System-Yellow":
AddMsg(nickname, ColorToHex(new Color(1f, 1f, 0f)), chat);
@@ -159,14 +168,9 @@ private void OnChatMsg(string msg)
}
}
- public void AddSystemNotice(string msgIn, bool warning = false)
- {
- OnChatMsg(string.Format(warning ? ":System NOTICE *System-Yellow :{0}" : ":System NOTICE *System-Blue :{0}", msgIn));
- }
-
- public static string ToTwitchNotice(string msgIn, bool error = false)
+ public void AddSystemNotice(string msgIn, TwitchIRC.NoticeColor colorEnum = TwitchIRC.NoticeColor.Blue)
{
- return string.Format(error ? ":Twitch NOTICE *System-Red :{0}" : ":Twitch NOTICE *System-Green :{0}", msgIn);
+ OnChatMsg(TwitchIRC.ToNotice("System", msgIn, colorEnum));
}
private void AddMsg(string nickname, string color, string chat)
diff --git a/Assets/HOTK/Twitch/TwitchIRC.cs b/Assets/HOTK/Twitch/TwitchIRC.cs
index b338d8d..dde2fd7 100644
--- a/Assets/HOTK/Twitch/TwitchIRC.cs
+++ b/Assets/HOTK/Twitch/TwitchIRC.cs
@@ -21,6 +21,9 @@ public class MsgEvent : UnityEngine.Events.UnityEvent { }
private readonly Queue _commandQueue = new Queue();
private readonly List _recievedMsgs = new List();
private System.Threading.Thread _inProc, _outProc;
+
+ private bool _connected;
+ private bool _loggedin;
public void StartIRC()
{
@@ -35,7 +38,9 @@ public void StartIRC()
var networkStream = sock.GetStream();
var input = new System.IO.StreamReader(networkStream);
var output = new System.IO.StreamWriter(networkStream);
-
+
+ _loggedin = false;
+ _connected = false;
//Send PASS & NICK.
output.WriteLine("PASS " + Oauth);
output.WriteLine("NICK " + NickName.ToLower());
@@ -47,7 +52,27 @@ public void StartIRC()
//input proc
_inProc = new System.Threading.Thread(() => IRCInputProcedure(input, networkStream));
_inProc.Start();
+
+ CancelInvoke("CheckConnection");
+ Invoke("CheckConnection", 5f);
+ }
+
+ private void CheckConnection()
+ {
+ if (_stopThreads) return;
+ lock (_recievedMsgs)
+ {
+ if (!_loggedin)
+ {
+ _recievedMsgs.Add(ToNotice("System", "Should be logged in by now.. are the username and oauth correct?", NoticeColor.Yellow));
+ }
+ else if (!_connected)
+ {
+ _recievedMsgs.Add(ToNotice("System", "Should be connected by now.. is the channel name correct?", NoticeColor.Yellow));
+ }
+ }
}
+
private void IRCInputProcedure(System.IO.TextReader input, System.Net.Sockets.NetworkStream networkStream)
{
while (!_stopThreads)
@@ -59,26 +84,38 @@ private void IRCInputProcedure(System.IO.TextReader input, System.Net.Sockets.Ne
}
_buffer = input.ReadLine();
-
if (_buffer == null) continue;
- if (_buffer.Contains("PRIVMSG #") || _buffer.Split(' ')[1] == "NOTICE")
+ var tokens = _buffer.Split(' ');
+ switch (tokens[1])
{
- lock (_recievedMsgs)
- {
- _recievedMsgs.Add(_buffer);
- }
- }
- else if (_buffer.StartsWith("PING "))
- {
- SendCommand(_buffer.Replace("PING", "PONG"));
- }
- else if (_buffer.Split(' ')[1] == "001")
- {
- lock (_recievedMsgs)
- {
- _recievedMsgs.Add(TwitchChatTester.ToTwitchNotice("Connected!"));
- }
- SendCommand("JOIN #" + ChannelName);
+ case "PRIVMSG":
+ case "NOTICE":
+ lock (_recievedMsgs)
+ {
+ _recievedMsgs.Add(_buffer);
+ }
+ break;
+ case "001":
+ lock (_recievedMsgs)
+ {
+ _recievedMsgs.Add(ToTwitchNotice("Logged in! Connecting to chat.."));
+ _loggedin = true;
+ }
+ SendCommand("JOIN #" + ChannelName);
+ break;
+ case "JOIN":
+ lock (_recievedMsgs)
+ {
+ _recievedMsgs.Add(ToTwitchNotice(string.Format("Connected to {0}!", tokens[2])));
+ _connected = true;
+ }
+ break;
+ default:
+ if (_buffer.StartsWith("PING "))
+ {
+ SendCommand(_buffer.Replace("PING", "PONG"));
+ }
+ break;
}
}
}
@@ -96,7 +133,7 @@ private void IRCOutputProcedure(System.IO.TextWriter output)
continue;
}
// https://github.com/justintv/Twitch-API/blob/master/IRC.md#command--message-limit
- //have enough time passed since we last sent a message/command?
+ //has enough time passed since we last sent a message/command?
if (stopWatch.ElapsedMilliseconds <= 1750)
{
Thread.Sleep(20);
@@ -135,10 +172,12 @@ public void OnEnable()
public void OnDisable()
{
_stopThreads = true;
+ CancelInvoke("CheckConnection");
}
public void OnDestroy()
{
_stopThreads = true;
+ CancelInvoke("CheckConnection");
}
public void Update()
{
@@ -152,4 +191,45 @@ public void Update()
_recievedMsgs.Clear();
}
}
+
+ public static string ToTwitchNotice(string msgIn, NoticeColor colorEnum = NoticeColor.Green)
+ {
+ return ToNotice("Twitch", msgIn, colorEnum);
+ }
+
+ public static string ToNotice(string nickname, string msgIn, NoticeColor colorEnum = NoticeColor.Green)
+ {
+ return string.Format(":{0} NOTICE {1} :{2}", nickname, NoticeColorToString(colorEnum), msgIn);
+ }
+
+ public static string NoticeColorToString(NoticeColor colorEnum)
+ {
+ switch (colorEnum)
+ {
+ case NoticeColor.Green:
+ return "*System-Green";
+ case NoticeColor.Red:
+ return "*System-Red";
+ case NoticeColor.Blue:
+ return "*System-Blue";
+ case NoticeColor.Yellow:
+ return "*System-Yellow";
+ case NoticeColor.Purple:
+ return "*System-Purple";
+ case NoticeColor.White:
+ return "*System-White";
+ default:
+ throw new ArgumentOutOfRangeException("colorEnum", colorEnum, null);
+ }
+ }
+
+ public enum NoticeColor
+ {
+ Green,
+ Red,
+ Blue,
+ Yellow,
+ Purple,
+ White
+ }
}