Created
March 9, 2020 15:37
-
-
Save EsteveSegura/a6813e3c2f09d1d8fc6374d72f8d5b99 to your computer and use it in GitHub Desktop.
Unity + twitch chat.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System; | |
using System.ComponentModel; | |
using System.Net.Sockets; | |
using System.IO; | |
public class TwitchIntegration : MonoBehaviour{ | |
public Ball player; | |
private TcpClient twitchClient; | |
private StreamReader reader; | |
private StreamWriter writer; | |
public string username; | |
public string password; | |
public string channel; | |
void Start(){ | |
player = GameObject.FindWithTag("Player").GetComponent<Ball>(); | |
Connect(); | |
} | |
void Update(){ | |
if(!twitchClient.Connected){ | |
Connect(); | |
} | |
ReadChat(); | |
} | |
private void Connect(){ | |
twitchClient = new TcpClient("irc.chat.twitch.tv", 6667); | |
reader = new StreamReader(twitchClient.GetStream()); | |
writer = new StreamWriter(twitchClient.GetStream()); | |
writer.WriteLine("PASS "+password); | |
writer.WriteLine("NICK "+username); | |
writer.WriteLine("USER "+username+" 8 * :"+username); | |
writer.WriteLine("JOIN #"+ channel); | |
writer.Flush(); | |
} | |
private void ReadChat(){ | |
if(twitchClient.Available > 0){ | |
string message = reader.ReadLine(); | |
if(message.Contains(":!jump")){ | |
player.Jump(); | |
} | |
if(message.Contains(":!forward")){ | |
player.Forward(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment