-
Notifications
You must be signed in to change notification settings - Fork 1
/
Twitch_chat_Unity
94 lines (75 loc) · 2.59 KB
/
Twitch_chat_Unity
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
using System;
using UnityEngine;
using System.Net.Sockets;
using System.IO;
using System.Collections.Generic;
public class Twitch_chat_Unity : MonoBehaviour {
[SerializeField] GameObject gameManager;
[SerializeField] GameObject lobby;
TcpClient _twitchTcpClient;
StreamReader _streamReader;
StreamWriter _streamWriter;
SC_Viewers _scViewers;
const string URL = "irc.chat.twitch.tv";
const int Port = 6667;
string _user;
string _oAuth;
float _pingCounter;
private void ConnectToTwitch() {
_twitchTcpClient = new TcpClient(URL, Port);
_streamReader = new StreamReader(_twitchTcpClient.GetStream());
_streamWriter = new StreamWriter(_twitchTcpClient.GetStream());
_streamWriter.WriteLine("PASS "+_oAuth);
_streamWriter.WriteLine("NICK "+_user.ToLower());
_streamWriter.WriteLine("JOIN #"+_user.ToLower());
_streamWriter.WriteLine("CAP REQ :twitch.tv/commands twitch.tv/tags");
_streamWriter.Flush();
}
private void Start() {
_user = PlayerPrefs.GetString("username").ToLower();
_oAuth = PlayerPrefs.GetString("oauth");
ConnectToTwitch();
_scViewers = new SC_Viewers();
// TODO get user id from Twitch API via the server
_scViewers.AddViewer(_user, "0000", null);
}
private void Update() {
try {
// TODO detect if connection is closed
// try to reconnect
_pingCounter += Time.deltaTime;
if (_pingCounter > 60) {
_streamWriter.WriteLine("PING " + URL);
_streamWriter.Flush();
_pingCounter = 0;
}
if (!_twitchTcpClient.Connected) {
ConnectToTwitch();
}
if (_twitchTcpClient.Available > 0) {
string message = _streamReader.ReadLine();
if (message != null && message.Contains("PRIVMSG")) {
Dictionary<string, string> parsedMessage = SC_TMI_Message_Parser.ParsedMessage(message.Substring(1));
Debug.Log(parsedMessage["IRC-message"]);
Dictionary<string, string> irCmessage = SC_TMI_Message_Parser.ParsedPrivmsg(parsedMessage["IRC-message"]);
string userID = parsedMessage["user-id"];
string username = irCmessage["username"];
if (username.Equals(PlayerPrefs.GetString("username"))) {
_scViewers.AddViewer(username, userID, null);
Debug.Log("player " + username + " added");
}
if (irCmessage["message"].Equals("!play") && SC_Game_Manager.GameState == SC_Game_State.LOBBY) {
if (!_scViewers.IsViewerExist(userID)) {
_scViewers.AddViewer(username, userID, null);
lobby.GetComponent<SC_Lobby>().Initial_Spawn_IA(username, userID);
}
}
}
}
}
catch (IOException ioException) {
Debug.Log("DISCONNECTED, RECONNECT");
ConnectToTwitch();
}
}
}