emitter-io / csharp

Client library for emitter.io compatible with .Net, .Net MicroFramework and WinRT
http://emitter.io
Eclipse Public License 1.0
29 stars 21 forks source link

How to dynamically generate key for a channel and publish? #16

Open cashberry opened 5 years ago

cashberry commented 5 years ago

Hi, We wrote the following code to do dynamic key generation for a channel and publish message to this channel using key generated. But publish is not working...

Code looks as follows,

using Emitter;
using System;
using System.Linq;
using System.Text;

namespace EmitterSample
{
    class Program
    {
        private static readonly string _secretKey = "LhlUB9xti-UGMuvGw83Rv0SEdYD3r2dy";
        private static Connection _connection;
        static void Main(string[] args)
        {
            _connection = new Connection("127.0.0.1", 8080, "");
            _connection.Error += _connection_Error;
            _connection.Disconnected += _connection_Disconnected;
            _connection.Connect();
            _connection.GenerateKey(_secretKey, "chat/", Emitter.Messages.EmitterKeyType.ReadWrite, 1200, response =>
             {
                 CheckMillionMessage(response.Key, response.Channel);
             });
            Console.WriteLine(_connection.IsConnected);

            Console.WriteLine("Press any key to continue....");
            Console.ReadKey();
            _connection.Disconnect();
        }

        private static void _connection_Disconnected(object sender, EventArgs e)
        {
            Console.WriteLine("Disconnected");
        }

        private static void _connection_Error(object sender, Exception e)
        {
            Console.WriteLine(e);
        }

        private static void CheckMillionMessage(string key, string chName)
        {
            int received = 0;
            _connection.On(key, chName, (channel, msg) =>
            {
                received++;
                Console.WriteLine($"{Encoding.UTF8.GetString(msg)} - {received}");
            });

            for (int i = 0; i < 10; i++)
            {
                _connection.Publish(key, chName, "Hello from client", 1200);
            }
        }
    }
}

Please let us know what will be the problem.