我已經構建了一個託管(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);
}
}