about summary refs log tree commit diff
path: root/README.md
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2020-04-28 21:07:23 -0400
committerBen Harris <ben@tilde.team>2020-04-28 21:07:23 -0400
commit8403fd8d0fb0ac3c69e90f530fda331ea2ab639b (patch)
tree6504febcc485c6335b8cf5e333fa0437229b0704 /README.md
parent6a0b8dc22fc86a5de37278231f3f8418afc9b836 (diff)
Fix example
Diffstat (limited to 'README.md')
-rw-r--r--README.md29
1 files changed, 19 insertions, 10 deletions
diff --git a/README.md b/README.md
index 8b15cbc..f04b443 100644
--- a/README.md
+++ b/README.md
@@ -29,38 +29,41 @@ see the full example in [Sample/Client.cs](Sample/Client.cs)
 
     public class Client
     {
-        private readonly Socket _socket;
+        private readonly byte[] _bytes;
         private readonly StatefulDecoder _decoder;
         private readonly StatefulEncoder _encoder;
-        private readonly byte[] _bytes;
+        private readonly Socket _socket;
 
         public Client()
         {
             _decoder = new StatefulDecoder();
             _encoder = new StatefulEncoder();
-            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
-            _bytes = new byte[1024];
+            _socket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
+            _bytes   = new byte[1024];
         }
 
         public void Start()
         {
             _socket.Connect("127.0.0.1", 6667);
+            while (!_socket.Connected) Thread.Sleep(1000);
 
-            Send(new Line {Command = "USER", Params = new List<string> {"username", "0", "*", "real name"}});
-            Send(new Line {Command = "NICK", Params = new List<string> {"statefulbot"}});
+            Send(new Line {Command = "NICK", Params = new List<string> {"tokensbot"}});
+            Send(new Line {Command = "USER", Params = new List<string> {"tokensbot", "0", "*", "real name"}});
 
             while (true)
             {
                 var bytesReceived = _socket.Receive(_bytes);
-                var lines = _decoder.Push(_bytes);
 
-                if (lines.Count == 0)
+                if (bytesReceived == 0)
                 {
                     Console.WriteLine("! disconnected");
                     _socket.Shutdown(SocketShutdown.Both);
+                    _socket.Close();
                     break;
                 }
 
+                var lines = _decoder.Push(_bytes, bytesReceived);
+
                 foreach (var line in lines)
                 {
                     Console.WriteLine($"< {line.Format()}");
@@ -71,7 +74,13 @@ see the full example in [Sample/Client.cs](Sample/Client.cs)
                             Send(new Line {Command = "PONG", Params = line.Params});
                             break;
                         case "001":
-                            Send(new Line {Command = "JOIN", Params = new List<string> {"#channel"}});
+                            Send(new Line {Command = "JOIN", Params = new List<string> {"#test"}});
+                            break;
+                        case "PRIVMSG":
+                            Send(new Line
+                            {
+                                Command = "PRIVMSG", Params = new List<string> {line.Params[0], "hello there"}
+                            });
                             break;
                     }
                 }
@@ -83,7 +92,7 @@ see the full example in [Sample/Client.cs](Sample/Client.cs)
             Console.WriteLine($"> {line.Format()}");
             _encoder.Push(line);
             while (_encoder.PendingBytes.Length > 0)
-                _encoder.Pop(_socket.Send(_encoder.PendingBytes));
+                _encoder.Pop(_socket.Send(_encoder.PendingBytes, SocketFlags.None));
         }
     }