2015-06-25 138 views
0

我已经构建了一个托管(OWIN)的Windows窗体集线器,所以不仅充当代理,它将作为客户端,因为我想要一个小窗口窗体,显示其他客户端连接。SignalR集线器Windows窗体 - 新手

我正在努力的位是主机客户端“侦听”以及如何记录连接的机器。 我只想写出消息到我的文本框

因此,这是我迄今为止所做的,即时通讯在相同的窗体上运行client \ hub。

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); 
     } 
} 

回答

0

这是我做的,你可以尝试:

  1. 创建在Form1具有以下结构的ArrayList:ConnectionId之外,登录ID,ServerSideEncryptedLoginPW。

  2. 在连接期间(当任何客户端在登录之前连接时),使用(连接ID,空,空)将元素添加到数组列表。

  3. 在集线器中添加登录功能。客户端调用此函数后,更新该特定连接ID的数组列表中的LoginID和加密的LoginPW。 (检查数据库中的密码,如果有的话)

  4. ondisconnected时,删除该特定connectionID的数组列表中的元素。

  5. 对于集线器中的其他功能,客户端必须提供LoginID和加密的密码。

  6. 在Windows窗体中,当某些内容发生变化时,在arraylist中显示值。没有。的客户连接,没有。的客户登录,客户登录ID列表等。

这样的事情!