// Proyecto Servidor ------------------>
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Cors;
using Microsoft.Owin.Hosting;
using Owin;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SignalRChat
{
public partial class PushSignalRServer : Form
{
private IDisposable SignalR { get; set; }
const string ServerURI ="https://+:8082"; //--> puerto de escucha del servidor
public List<ConnectedClients> ConnectedUsers = new List<ConnectedClients>();
internal PushSignalRServer()
{
InitializeComponent();
}
private void ButtonStart_Click(object sender, EventArgs e)
{
WriteToConsole("Starting
server..." + DateTime.Now.ToString("dd/MM/yyyy
HH:mm", CultureInfo.CreateSpecificCulture("es-ES")) );
ButtonStart.Enabled = false;
Task.Run(() => StartServer());
}
private void StartServer()
{
try
{
SignalR = WebApp.Start(ServerURI);
}
catch (TargetInvocationException)
{
WriteToConsole("Server
failed to start. A server is already running on " + ServerURI);
this.Invoke((Action)(() => ButtonStart.Enabled = true));
return;
}
this.Invoke((Action)(() => ButtonStop.Enabled = true));
WriteToConsole("Server
started at " + ServerURI);
notifyIconOff.Visible = false ;
notifyIconOn.Visible = true ;
}
internal void WriteToConsole(String message)
{
if
(RichTextBoxConsole.InvokeRequired)
{
this.Invoke((Action)(() =>
WriteToConsole(message)
));
return;
}
RichTextBoxConsole.AppendText(message + Environment.NewLine);
}
private void WinFormsServer_FormClosing(object sender, FormClosingEventArgs e)
{
if (SignalR != null)
{
SignalR.Dispose();
}
}
private void WinFormsServer_Load(object sender, EventArgs e)
{
WriteToConsole("Starting
server..." + DateTime.Now.ToString("dd/MM/yyyy
HH:mm", CultureInfo.CreateSpecificCulture("es-ES")));
ButtonStart.Enabled = false;
Task.Run(() => StartServer());
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
public class MyHub : Hub
{
//Función para enviar mensaje de
usuario emisor (nombre y compañia) a usuario receptor (nombre y compañia)
public void Send(string nameFrom, string companyFrom, string message, string nameTo, string companyTo)
{
if (nameTo.ToUpper() == "ALL" || nameTo.ToUpper() == "TODOS")
{
Send(nameFrom, message);
}
else
{
if (Program.MainForm.ConnectedUsers != null)
{
var exists =
from ConnectedClients e in Program.MainForm.ConnectedUsers
where e.Name == nameTo &&
e.Company == companyTo
select e;
if (exists.Count() == 0)
{
}
else
{
foreach (var UserExist in exists)
{
Clients.Client(UserExist.Id).addMessage(nameFrom,
message);
}
}
}
else
{
}
}
}
public override Task OnConnected()
{
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
var exists =
from ConnectedClients e in Program.MainForm.ConnectedUsers
where e.Id == Context.ConnectionId
select e;
if (exists.Count() == 0)
{
}
else
{
foreach (var UserExist in exists)
{
Program.MainForm.WriteToConsole("Client
disconnected: " + UserExist.Name);
}
}
Program.MainForm.ConnectedUsers.RemoveAll(u
=> u.Id == Context.ConnectionId);
return base.OnDisconnected(true);
}
//Función para identificar al usuario
en servidor (añadir al cliente a la lista ConnectedClients) -->
public void WhoIam(string MyName, string MyCompany)
{
if (string.IsNullOrEmpty(MyCompany))
{
}
else
{
if (MyCompany.Substring(MyCompany
.Length -1)==".")
{
MyCompany =
MyCompany.Substring(0, MyCompany.Length - 1);
}
}
if (Program.MainForm.ConnectedUsers != null)
{
if (Program.MainForm.ConnectedUsers.Count()>
0 )
{
var exists =
from ConnectedClients e in Program.MainForm.ConnectedUsers
where e.Name == MyName &&
e.Company == MyCompany && e.Id != Context.ConnectionId
select e;
if (exists.Count() > 0)
{
Program.MainForm.ConnectedUsers.RemoveAll(u
=> u.Name == MyName);
addConnectedUsers(MyName, MyCompany);
}
else
{
addConnectedUsers(MyName, MyCompany);
}
}
else
{
addConnectedUsers(MyName, MyCompany);
}
}
else
{
addConnectedUsers(MyName,
MyCompany);
}
}
private void addConnectedUsers(string Name, string Company)
{
ConnectedClients newUser = new ConnectedClients();
newUser.Id = Context.ConnectionId;
newUser.Name = Name;
newUser.Company = Company;
newUser.TimeStamp = DateTime.Now;
Program.MainForm.ConnectedUsers.Add(newUser);
Program.MainForm.WriteToConsole("Client
connected: " + Name);
}
public void Send(string name, string message)
{
Clients.All.addMessage(name,
message);
}
}
public class ConnectedClients
{
public string Id { get; set; }
public string Name { get; set; }
public string Company { get; set; }
public DateTime TimeStamp { get; set; }
}
}
//Proyecto Cliente ------------à
using Microsoft.AspNet.SignalR.Client;
using System;
using System.Net.Http;
using System.Windows.Forms;
namespace WinFormsClient
{
public partial class WinFormsClient : Form
{
private String UserName { get; set; }
private String CompanyName { get; set; }
private IHubProxy HubProxy { get; set; }
const string ServerURI = "https://miservidor.net:8082/signalr";
private HubConnection Connection { get; set; }
internal WinFormsClient()
{
InitializeComponent();
}
private async void ConnectAsync()
{
Connection = new HubConnection(ServerURI);
Connection.Closed +=
Connection_Closed;
HubProxy = Connection.CreateHubProxy("MyHub");
HubProxy.On<string, string>("AddMessage", (name, message) =>
this.Invoke((Action)(() =>
RichTextBoxConsole.AppendText(String.Format("{0}: {1}" + Environment.NewLine, name, message))
))
);
try
{
await Connection.Start();
}
catch (HttpRequestException)
{
StatusText.Text = "Unable to
connect to server: Start server before connecting clients.";
//No connection
return;
}
SignInPanel.Visible = false;
ChatPanel.Visible = true;
ButtonSend.Enabled = true;
TextBoxMessage.Focus();
RichTextBoxConsole.AppendText("Connected
to server at " + ServerURI + Environment.NewLine);
identify();
}
private void identify() //Me identifico (UserName +
CompanyName)
{
HubProxy.Invoke("WhoIam", UserName, CompanyName);
this.Text = UserName + "(" + CompanyName + ")";
}
private void Connection_Closed()
{
this.Invoke((Action)(() => ChatPanel.Visible = false));
this.Invoke((Action)(() => ButtonSend.Enabled = false));
this.Invoke((Action)(() => StatusText.Text = "You have
been disconnected."));
this.Invoke((Action)(() => SignInPanel.Visible = true));
}
//Este Botón permite conectarme al
servidor
private void SignInButton_Click(object sender, EventArgs e)
{
UserName = UserNameTextBox.Text;
CompanyName = CompanyTextbox.Text;
//Connect to server (use async
method to avoid blocking UI thread)
if (!String.IsNullOrEmpty(UserName)
&& !String.IsNullOrEmpty(CompanyName))
{
StatusText.Visible = true;
StatusText.Text = "Connecting
to server...";
ConnectAsync();
}
}
private void WinFormsClient_FormClosing(object sender, FormClosingEventArgs e)
{
if (Connection != null)
{
Connection.Stop();
Connection.Dispose();
}
}
//Enviar mensaje --->;
private void ButtonSend_Click(object sender, EventArgs e)
{
//Invocamos a Send identificándonos
(usuario y empresa) e identificando al receptor (usuario y empresa)
HubProxy.Invoke("Send", UserName, CompanyName,
TextBoxMessage.Text, txtPara.Text, txtCompany.Text.Trim());
TextBoxMessage.Text = String.Empty;
TextBoxMessage.Focus();
}
}