-
Notifications
You must be signed in to change notification settings - Fork 16
/
TwitchIRC.cs
235 lines (219 loc) · 6.95 KB
/
TwitchIRC.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
public class TwitchIRC : MonoBehaviour
{
public string Oauth;
public string NickName;
public string ChannelName;
private const string Server = "irc.twitch.tv";
private const int Port = 6667;
//event(buffer).
public class MsgEvent : UnityEngine.Events.UnityEvent<string> { }
public MsgEvent MessageRecievedEvent = new MsgEvent();
private string _buffer = string.Empty;
private bool _stopThreads;
private readonly Queue<string> _commandQueue = new Queue<string>();
private readonly List<string> _recievedMsgs = new List<string>();
private System.Threading.Thread _inProc, _outProc;
private bool _connected;
private bool _loggedin;
public void StartIRC()
{
_stopThreads = false;
var sock = new System.Net.Sockets.TcpClient();
sock.Connect(Server, Port);
if (!sock.Connected)
{
Debug.Log("Failed to connect!");
return;
}
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());
output.Flush();
//output proc
_outProc = new System.Threading.Thread(() => IRCOutputProcedure(output));
_outProc.Start();
//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)
{
if (!networkStream.DataAvailable)
{
Thread.Sleep(20);
continue;
}
_buffer = input.ReadLine();
if (_buffer == null) continue;
var tokens = _buffer.Split(' ');
switch (tokens[1])
{
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;
}
}
}
private void IRCOutputProcedure(System.IO.TextWriter output)
{
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
while (!_stopThreads)
{
lock (_commandQueue)
{
if (_commandQueue.Count <= 0)
{
Thread.Sleep(20);
continue;
}
// https://github.com/justintv/Twitch-API/blob/master/IRC.md#command--message-limit
//has enough time passed since we last sent a message/command?
if (stopWatch.ElapsedMilliseconds <= 1750)
{
Thread.Sleep(20);
continue;
}
//send msg.
output.WriteLine(_commandQueue.Peek());
output.Flush();
//remove msg from queue.
_commandQueue.Dequeue();
//restart stopwatch.
stopWatch.Reset();
stopWatch.Start();
}
}
}
public void SendCommand(string cmd)
{
lock (_commandQueue)
{
_commandQueue.Enqueue(cmd);
}
}
public void SendMsg(string msg)
{
lock (_commandQueue)
{
_commandQueue.Enqueue("PRIVMSG #" + ChannelName + " :" + msg);
}
}
public void OnEnable()
{
_stopThreads = false;
}
public void OnDisable()
{
_stopThreads = true;
CancelInvoke("CheckConnection");
}
public void OnDestroy()
{
_stopThreads = true;
CancelInvoke("CheckConnection");
}
public void Update()
{
lock (_recievedMsgs)
{
if (_recievedMsgs.Count <= 0) return;
foreach (var t in _recievedMsgs)
{
MessageRecievedEvent.Invoke(t);
}
_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
}
}