Я пытаюсь создать twitch-chatbot. Я прочитал несколько руководств, и это то, что я закодировал до сих пор:TCP-клиент не подключается к IRC-адресу
namespace IRC_Client
{
class Program
{
public NetworkStream NetStream = null;
public StreamReader StreamRead = null;
public StreamWriter StreamWrite = null;
public System.Net.Sockets.TcpClient Socket_TcpClient;
static void Main(string[] args)
{
Program P = new Program();
P.IRC_Connect();
Console.Read();
}
public void IRC_Connect()
{
int port;
string nick, owner, server, oauth;
nick = "xxx";
Console.WriteLine("Bot Name: {0}", nick);
oauth = "oauth:xxxxxxxxxxxxxxxxxxx";
Console.WriteLine("Password Token: {0}", oauth);
owner = "xxx";
Console.WriteLine("Bot owner name: {0}", owner);
server = "irc.twitch.tv";
Console.WriteLine("Server name: {0}", server);
port = 6667;
Console.WriteLine("Portnummber: {0} ", port);
try
{
Socket_TcpClient = new TcpClient(server, port);
}
catch
{
Console.WriteLine("Failed to connect!");
return;
}
Console.WriteLine("Connected");
IRCWork();
try
{
NetStream = Socket_TcpClient.GetStream();
StreamRead = new StreamReader(NetStream);
StreamWrite = new StreamWriter(NetStream);
SendData("PASS", oauth);
SendData("NICK", nick);
}
catch
{
Console.WriteLine("Communication error");
throw;
}
}
public void SendData(string cmd, string param)
{
if (param == null)
{
StreamWrite.WriteLine(cmd);
StreamWrite.Flush();
Console.WriteLine(cmd);
}
else
{
StreamWrite.WriteLine(cmd + " " + param);
StreamWrite.Flush();
Console.WriteLine(cmd + " " + param);
}
}
public void IRCWork()
{
string[] Text;
string data;
bool shouldRun = true;
while (shouldRun)
{
data = StreamRead.ReadLine();
Console.WriteLine(data);
char[] charSeparator = new char[] { ' ' };
Text = data.Split(charSeparator, 5);
if (Text[0] == "PING")
{
SendData("PONG", Text[1]);
}
if (Text.Length > 4) //is the command received long enough to be a bot command?
{
string command = Text[3]; //grab the command sent
switch (command)
{
case ":!join":
SendData("JOIN", Text[4]);
//if the command is !join send the "JOIN" command to the server with the parameters set by the user
break;
case ":!say":
SendData("PRIVMSG", Text[2] + " " + Text[4]);
//if the command is !say, send a message to the chan (ex[2]) followed by the actual message (ex[4]).
break;
case ":!quit":
SendData("QUIT", Text[4]);
//if the command is quit, send the QUIT command to the server with a quit message
shouldRun = false;
//turn shouldRun to false - the server will stop sending us data so trying to read it will not work and result in an error. This stops the loop from running and we will close off the connections properly
break;
}
}
}
}
public void Dispose()
{
if (StreamRead != null)
StreamRead.Close();
if (StreamWrite != null)
StreamWrite.Close();
if (NetStream!= null)
NetStream.Close();
if (Socket_TcpClient != null)
Socket_TcpClient.Close();
}
}
}
Основная проблема заключается в подключении. Я не достаточно далеко, чтобы проверить остальные. Адрес сервера и порт должны быть правильными, но программа просто останавливается здесь, и ничего не происходит. (я использовал информацию с этого сайта: http://help.twitch.tv/customer/com/portal/articles/1302780-twitch-irc)
try
{
Socket_TcpClient = new TcpClient(server, port);
}
Я надеюсь, что кто-то знает ответ на мою проблему.
Любые исключения или программа просто останавливается? – Sybren
Хорошо, я был нетерпелив. Через несколько минут он выдает исключения за время ожидания. Но все-таки сервер и порт должны быть правильными. –
Можете ли вы добавить сообщения об исключениях на свой вопрос? – Sybren