Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using ChannelSockets;
- using System.Net;
- using System.Threading;
- namespace ExampleClient
- {
- class Program
- {
- public struct Channels
- {
- public const byte UnreliableUpdate = 0;
- public const byte ReliableUpdate = 1;
- public const byte Chat = 2;
- public const byte Downloads = 3;
- }
- public static ChannelSocket socket = new ChannelSocket(IPAddress.Loopback, 30303);
- static void Main(string[] args)
- {
- socket.Add(Channels.UnreliableUpdate, ChannelType.Unreliable);
- socket.Add(Channels.ReliableUpdate, ChannelType.Reliable);
- socket.Add(Channels.Chat, ChannelType.Reliable);
- socket.Add(Channels.Downloads, ChannelType.Reliable);
- string username = "foo";
- string password = "bar";
- AutoResetEvent waitConnectEvent = new AutoResetEvent(false);
- socket.Connect(
- (p) =>
- {
- p.WriteString(username);
- p.WriteString(password);
- },
- () =>
- {
- // Success
- Console.WriteLine("Successfully connected");
- waitConnectEvent.Set();
- },
- () =>
- {
- // Failure
- Console.WriteLine("Failure connecting");
- waitConnectEvent.Set();
- });
- waitConnectEvent.WaitOne();
- // Client command line interface
- while (true)
- {
- Console.WriteLine("Enter command");
- Console.WriteLine("1. Read chat");
- Console.WriteLine("2. Send 'hello world' to server");
- Console.WriteLine("3. Disconnect");
- var key = Console.ReadKey().Key;
- Console.Clear();
- switch (key)
- {
- case ConsoleKey.D1:
- EndPointPacket endPointPacket;
- while (socket[Channels.Chat].Packets.TryDequeue(out endPointPacket))
- {
- ushort clientID;
- string message;
- if (endPointPacket.Packet.ReadUInt16(out clientID) && endPointPacket.Packet.ReadString(out message))
- {
- Console.WriteLine(clientID + ": " + message.Trim());
- }
- }
- break;
- case ConsoleKey.D2:
- var packet = socket[Channels.Chat].CreatePacket();
- packet.WriteUInt16(ushort.MaxValue); // Client ID, ushort.MaxValue is the Server
- packet.WriteString("hello world");
- socket[Channels.Chat].Send(packet, socket.EndPoint);
- Console.WriteLine("Message sent");
- break;
- case ConsoleKey.D3:
- AutoResetEvent waitDisconnectEvent = new AutoResetEvent(false);
- socket.Disconnect(() =>
- {
- Console.WriteLine("Completed");
- waitDisconnectEvent.Set();
- });
- waitDisconnectEvent.WaitOne();
- break;
- }
- Console.WriteLine();
- Console.WriteLine("Press any key...");
- Console.ReadKey(true);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement