SignalR Hubs Windows Form - newbie

369 Views Asked by At

I've built a hosted (OWIN) Windows Form Hub, so not only acting as a proxy it will be a client, as I want to have a small windows form that show what other clients connect.

The bit im struggling with is the host client "Listening" and how to log connected machines. I just want to write out the message to my textbox

So here is what I have done so far, im running client\hub on same form.

public partial class Form1 : Form
{
    private IDisposable SignalR { get; set; }
    private HubConnection hubConnection;
    private IHubProxy chat;
    const string URL = "http://localhost:8080";

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Task.Run(() => StartServer());
        //Task.Run(() => RegisterServerConnection());
    }

    private void StartServer()
    {
        try
        {
            SignalR = WebApp.Start(URL);
        }
        catch (TargetInvocationException)
        {

        }
        this.Invoke((Action) (() => richTextBox1.AppendText("Server running on " + URL)));


    }

    private async void RegisterServerConnection()
    {
        hubConnection = new HubConnection(URL);
        hubConnection.GroupsToken = "RoomA";

        chat = hubConnection.CreateHubProxy("chat");

        int timeout = 10000;
        var task = hubConnection.Start();
        if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
        {
            // await chat.Invoke<ConnectionModel>("clientConnected", connectionModel);
            this.Invoke((Action)(() => richTextBox1.Text+="Connected"));
            //  this.Hide();
        }
        else
        {
            this.Invoke((Action)(() => richTextBox1.AppendText("Unable to connect.")));
        }


        chat.Invoke<ChatMessage>("send", new ChatMessage() { Msg = "Host Running", GroupName = "Host" });
    }

    private void btnGo_Click(object sender, EventArgs e)
    {
        RegisterServerConnection();
    }



}

[HubName("chat")]
    public class ChatHub : Hub
    {
        public void SendMessage(string message)
        {
            var msg = String.Format(
                "{0}: {1}", Context.ConnectionId, message);
            Clients.All.newMessage(msg);
        }

        public override Task OnConnected()
        {
            return base.OnConnected();
        }



        public void Send(ChatMessage message)
        {
            // Call the addMessage method on all clients            
            Clients.All.addMessage(message.Msg);
            Clients.Group(message.GroupName).newMessage("Group Message " + message.Msg);
        }
}
1

There are 1 best solutions below

0
On

this is what I did, you may try:

  1. create an arraylist in form1 with the following structure: connectionID, loginID, ServerSideEncryptedLoginPW.

  2. during onconnected (when any client connected before login), add an element to the arraylist with (connectionID, empty, empty).

  3. add a login function in the hub. After client call this function, update the LoginID and the encrypted LoginPW in the arraylist for that particular connectionID. (check password in the database, if you have one)

  4. during ondisconnected, remove the element in the arraylist for that particular connectionID.

  5. For other functions in the Hub, clients must provide the LoginID and the encrypted password.

  6. In your Windows form, shows values in the arraylist when something changes, e.g. no. of clients connected, no. of clients logined, list of clients' LoginID, etc.

something like that!