Hello, I’m making bot on C# and I can’t figure out, why it still disconnects me after PING, here is my response
if (raw.Contains("PING"))
{
SendIrcMessage("PONG :tmi.twitch.tv");
BeginInvoke(new Action(() => textBox1.Text += $"{DateTime.Now.Hour}:{DateTime.Now.Minute} ping {raw}\r\n"));
return;
}
public void SendIrcMessage(string message)
{
outputStream.WriteLine(message);
outputStream.Flush();
}
TcpClient tcpClient = new TcpClient("irc.chat.twitch.tv", 6667);
StreamWriter outputStream = new StreamWriter(tcpClient.GetStream());
I saw some topics with same problem so it seems that my response is correct but it doesn’t work for some reason.
Yes, I do it with winforms for my own reasons.
And I don’t want to use twitchlib (btw I can’t use it with wf) or any else lib.
I’m confused why you’re sending a PING message to twitch since those should only ever be received by a client, not sent. However your response message looks like it’s formatted correctly. I would check to make sure that StreamWriter properly appends \r\n since any other combination violates the IRC spec and might be seen as a garbage message. Are you able to successfully send other messages with your SendIrcMessage(...) function?
For reference, this is my writer which works as of last night. (Also in C#)
public void
Pong(string trailing = "")
{
string message = "PONG";
if (trailing.IsValid())
{
message += " :" + trailing;
}
Send(message);
}
public void
Send(string format, params object[] arguments)
{
ExceptionUtil.ThrowIfInvalid(format, nameof(format));
string message = !arguments.IsValid() ? format : string.Format(format, arguments);
if (!CanSend(message))
{
return;
}
byte[] bytes = Encoding.UTF8.GetBytes(message + "\r\n");
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
OnDataSent.Raise(this, new DataEventArgs(bytes, message));
}
If I can send Ping to twitch or message to twitch chat that makes my SendIrcMessage(...) function correct? stream.WriteLine() adds \r\n, I guess or I wouldn’t be able to connect.
And that works fine. I can send Ping (btw I don’t know the reason too, but moderator said that I should, I mean, I can check connection with twitch through pings).
Your stream is StreamWriter?
Is it possible, that my Brandmauer or anivirus blocks something? I just can’t understand the problem. And I don’t even know if twitch server received my pong.
The first message include what’s called a “prefix”. It’s not prohibited by the Spec, but per the IRC spec:
Clients should not use prefix when sending a message from
themselves; if they use a prefix, the only valid prefix is the
registered nickname associated with the client. If the source
identified by the prefix cannot be found from the server’s internal
database, or if the source is registered from a different link than
from which the message arrived, the server must ignore the message
silently.
So essentially if you do send a prefix, it must only ever be :your_nick_here COMMAND :message\r\n
To be safe, I would just omit the prefix entirely. Your PONG response would then just look like:
PONG :tmi.twitch.tv\r\n
In your case, the \r\n is covered by your StreamWriter.Writeline(...)
Nope, neither PONG :tmi.twitch.tv\r\n, not PONG works for me. Btw I have another bot that works fine but on Node.js with tmi.js so everything should be alrigth, but still disconnects the c# one.
I am wondering if you are not reading chat and then you are being disconnected because the “send message to client buffer” becomes too large and Twitch disconnects you.