I need that the client will send a message to the server and that the server will push a message to the client.
I'm trying to do a small example before I'll add it to the web app that I'm working on.
I read some tutorials that I found and saw some examples but I still can't make it work.
Server:
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(TestServer.Startup))]
namespace TestServer
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
namespace TestServer
{
[HubName("ServerHub")]
class ServerHub : Hub
{
public void send(string message)
{
Clients.All.serverUpdate(message);
}
}
}
Client: I want to change Label2 text when a message from the server is pushed.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="TestClient.MainPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
</form>
<script src="Scripts/jquery-2.0.3.min.js"></script>
<script src="Scripts/jquery-ui-1.10.3.min.js"></script>
<script src="Scripts/jquery.signalR-2.0.1.min.js"></script>
<script src="/signalr/hubs"></script>
<script>
var hub = $.connection.ServerHub;
hub.client.serverUpdate = function (str) {
$("#Label2").text(str);
};
</script>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Client.Hubs;
using Microsoft.AspNet.SignalR.Client;
namespace TestClient
{
public partial class MainPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var hubConnection = new HubConnection("http://localhost/signalr");
IHubProxy ClientHubProxy = hubConnection.CreateHubProxy("ServerHub");
hubConnection.Start().Wait();
}
protected void Button1_Click(object sender, EventArgs e)
{
ClientHub ch = new ClientHub();
ch.sendUpdate("button clicked");
}
}
}
Do I need to add something in the servers main that is related to Signalr?
In the client I'm not sure what url to put in the next line:
var hubConnection = new HubConnection("http://localhost/signalr");
not sure that the way I sent the message to the server (after button click) is the right way.