static void Main(string[] args) { IConnectionProtocol protocol = new TCPConnectionProtocol("localhost", 3000); ConnectionManager manager = new ConnectionManager(protocol); Connection connection = manager.GetConnection(); // the job specifier that we are sending over byte id = 0; while (true) { // send out a message without a payload Message msg = new Message(id, null); connection.WriteMessage(msg); // increment the job specifier, skipping SUBSCRIBE and UNSUBSCRIBE // for simplicity ++id; if (id == 100) { id += 2; } // delay so we don't kill the server Thread.Sleep(500); } }
private void Listen() { Server.Start(); while (Listening) { TcpClient client = Server.AcceptTcpClient(); IConnectionProtocol protocol = new TCPConnectionProtocol(client); ConnectionManager manager = new ConnectionManager(protocol); Connection connection = manager.GetConnection(); connection.SubscribeToAll(); Connections.Add(connection); } Server.Stop(); }
static void Main(string[] args) { // establish a connection to the remote application IConnectionProtocol protocol = new TCPConnectionProtocol("localhost", 3000); ConnectionManager manager = new ConnectionManager(protocol); Connection conn = manager.GetConnection(); // we want all messages that come our way conn.SubscribeToAll(); // start out tracking evens byte mode = 0; Message m = new Message(SUBSCRIBE, new byte[] { mode }); conn.WriteMessage(m); while (true) { // if there are no messages waiting to be read then poll for input while (conn.PendingMessages == 0) { if (Console.KeyAvailable && Console.ReadKey().Key == ConsoleKey.Spacebar) { // unsubscribe from current byte[] payload = { mode }; m = new Message(UNSUBSCRIBE, payload); conn.WriteMessage(m); // subscribe to other if (mode == 0) { mode = 1; } else { mode = 0; } payload[0] = mode; m = new Message(SUBSCRIBE, payload); conn.WriteMessage(m); } } // display the contents of the received message m = conn.ReadMessage(); Console.WriteLine(String.Format("Received message with job specifier of {0}", m.JobSpecifier)); } }