如何将集线器内容从任何服务器带到*另一台服务器*项目集线器?
How to bring Hub content from Any server to *another server* project Hub?
代码的排列和随意的地方有点乱,希望能找到解释如下:
当我有一个服务器“https://localhost:48009/”并且应用程序配备了 Signal-R 的所有要求并且 Hub 存在于它上面。
在文件夹 Hubs 中有一个 Hub Class ChatHub.cs
using System;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRChat
{
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the addNewMessageToPage method to update clients.
Clients.All.addNewMessageToPage(name, message);
}
}
}
并在 Chat.cshtml
@{
ViewBag.Title = "Chat";
}
<h2>Chat</h2>
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion">
</ul>
</div>
@section scripts {
<!--Script references. -->
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.1.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
<script>
$(function () {
// Reference the auto-generated proxy for the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call back to display messages.
chat.client.addNewMessageToPage = function (name, message) {
// Add the message to the page.
$('#discussion').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
// This optional function html-encodes messages for display in the page.
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
</script>
}
当您在多个浏览器上打开页面时一切正常
此处所有操作都发生在服务器“https://localhost:48009/”
我碰巧在另一台服务器上有另一个项目例如“http://localhost:18098/”
在index.cshtml
@{
ViewBag.Title = "index";
}
<h2>Chat</h2>
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion">
</ul>
</div>
@section scripts {
<!--Script references. -->
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.1.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
///////////////////gotohub here
<script src="https://localhost:48009//signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
<script>
$(function () {
// Reference the auto-generated proxy for the hub.
///////Link to the other server
var chat = $.connection.chatHub.url="https://localhost:48009/";
// Create a function that the hub can call back to display messages.
chat.client.addNewMessageToPage = function (name, message) {
// Add the message to the page.
$('#discussion').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
// This optional function html-encodes messages for display in the page.
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
</script>
}
这里我好像也套用了类似问题的解决方法。这个方法对吗?
知道另一个服务器 "http://localhost:18098/" 也装了Signal-R
我想将 Hub 内容从那里带到 另一台服务器 Hub。
1.Add Nuget 包 SignalR
2.Add "Startup" Class 至 App_Start
namespace YourProjectname.App_Start {
public class Startup {
public void Configuration(IAppBuilder app) {
app.MapSignalR("/signalr",new Microsoft.AspNet.SignalR.HubConfiguration());
}
}
}
4.Change 你的 web.config 如下
<appSettings>
<add key="owin:AppStartup" value="YourProjectName.App_Start.Startup"/>
</appSettings>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://192.168.43.174:9090" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Methods" value="true" />
</customHeaders>
</httpProtocol>
</system.webServer>
*如果你有很多客户端允许每个请求如上
在您的项目下添加 Hub 文件夹
修改集线器 Class 如下所示
namespace YourProjectName.ChatHub {
[HubName("ChatHub")]
public class ChatHub : Hub {
[HubMethodName("Sendchat")]
public void Send(String Message,String Touser) {
Clients.Client(Touser).GotMessages(Message);//send messages to specific user
//Clients.All.GotMessages(Message); //seln messages to all user
String CName = Message;
}
[HubMethodName("hubconnect")]
public void Get_Connect(String Name) {
Clients.All.GotMessages(Name +" Connected Connection Id is "+ this.Context.ConnectionId);
String CName = Name;
}
public override System.Threading.Tasks.Task OnConnected() {
return base.OnConnected();
}
public override System.Threading.Tasks.Task OnReconnected() {
return base.OnReconnected();
}
public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled) {
return base.OnDisconnected(stopCalled);
}
}
}
你只想安装客户端,安装 SignalR 客户端
将您的 Cshtml 更改为 Bellow
<h2>Chat</h2>
<div class="container">
<input type="text" id="name" placeholder="tousername"/>
<input type="text" id="message" placeholder="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion"></ul>
</div>
@section scripts{
<script src="~/Scripts/jquery.signalR-2.3.0.min.js"></script>
<script>
$(function () {
//Create Hub Connection
var connection = $.hubConnection("http://192.168.43.174:9092", { useDefaultath: false });
//Add your Server above IP Addess
//Create Chat Connection
var proxy = connection.createHubProxy("ChatHub");
$('#displayname').val(prompt('Enter your name:', ''));
//Connection stablished
connection.start().done(function () {
try {
proxy.invoke("hubconnect", $('#displayname').val());
} catch (e) {
alert(e.message);
}
});
$('#sendmessage').on('click', function () {
//Send Messages to specific User
proxy.invoke("Sendchat", $('#message').val(), $('#name').val());
});
//Receive Messages from User
proxy.on("GotMessages", function (Message) {
alert(Message);
$('#discussion').append('<div>' + Message + '</br></div>');
});
});
</script>
}
代码的排列和随意的地方有点乱,希望能找到解释如下:
当我有一个服务器“https://localhost:48009/”并且应用程序配备了 Signal-R 的所有要求并且 Hub 存在于它上面。
在文件夹 Hubs 中有一个 Hub Class ChatHub.cs
using System;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRChat
{
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the addNewMessageToPage method to update clients.
Clients.All.addNewMessageToPage(name, message);
}
}
}
并在 Chat.cshtml
@{
ViewBag.Title = "Chat";
}
<h2>Chat</h2>
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion">
</ul>
</div>
@section scripts {
<!--Script references. -->
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.1.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
<script>
$(function () {
// Reference the auto-generated proxy for the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call back to display messages.
chat.client.addNewMessageToPage = function (name, message) {
// Add the message to the page.
$('#discussion').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
// This optional function html-encodes messages for display in the page.
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
</script>
}
当您在多个浏览器上打开页面时一切正常
此处所有操作都发生在服务器“https://localhost:48009/”
我碰巧在另一台服务器上有另一个项目例如“http://localhost:18098/”
在index.cshtml
@{
ViewBag.Title = "index";
}
<h2>Chat</h2>
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion">
</ul>
</div>
@section scripts {
<!--Script references. -->
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.1.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
///////////////////gotohub here
<script src="https://localhost:48009//signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
<script>
$(function () {
// Reference the auto-generated proxy for the hub.
///////Link to the other server
var chat = $.connection.chatHub.url="https://localhost:48009/";
// Create a function that the hub can call back to display messages.
chat.client.addNewMessageToPage = function (name, message) {
// Add the message to the page.
$('#discussion').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
// This optional function html-encodes messages for display in the page.
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
</script>
}
这里我好像也套用了类似问题的解决方法。这个方法对吗?
知道另一个服务器 "http://localhost:18098/" 也装了Signal-R
我想将 Hub 内容从那里带到 另一台服务器 Hub。
1.Add Nuget 包 SignalR
2.Add "Startup" Class 至 App_Start
namespace YourProjectname.App_Start {
public class Startup {
public void Configuration(IAppBuilder app) {
app.MapSignalR("/signalr",new Microsoft.AspNet.SignalR.HubConfiguration());
}
}
}
4.Change 你的 web.config 如下
<appSettings>
<add key="owin:AppStartup" value="YourProjectName.App_Start.Startup"/>
</appSettings>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://192.168.43.174:9090" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Methods" value="true" />
</customHeaders>
</httpProtocol>
</system.webServer>
*如果你有很多客户端允许每个请求如上
在您的项目下添加 Hub 文件夹
修改集线器 Class 如下所示
namespace YourProjectName.ChatHub { [HubName("ChatHub")] public class ChatHub : Hub { [HubMethodName("Sendchat")] public void Send(String Message,String Touser) { Clients.Client(Touser).GotMessages(Message);//send messages to specific user //Clients.All.GotMessages(Message); //seln messages to all user String CName = Message; } [HubMethodName("hubconnect")] public void Get_Connect(String Name) { Clients.All.GotMessages(Name +" Connected Connection Id is "+ this.Context.ConnectionId); String CName = Name; } public override System.Threading.Tasks.Task OnConnected() { return base.OnConnected(); } public override System.Threading.Tasks.Task OnReconnected() { return base.OnReconnected(); } public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled) { return base.OnDisconnected(stopCalled); } } }
你只想安装客户端,安装 SignalR 客户端
将您的 Cshtml 更改为 Bellow
<h2>Chat</h2>
<div class="container">
<input type="text" id="name" placeholder="tousername"/>
<input type="text" id="message" placeholder="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion"></ul>
</div>
@section scripts{
<script src="~/Scripts/jquery.signalR-2.3.0.min.js"></script>
<script>
$(function () {
//Create Hub Connection
var connection = $.hubConnection("http://192.168.43.174:9092", { useDefaultath: false });
//Add your Server above IP Addess
//Create Chat Connection
var proxy = connection.createHubProxy("ChatHub");
$('#displayname').val(prompt('Enter your name:', ''));
//Connection stablished
connection.start().done(function () {
try {
proxy.invoke("hubconnect", $('#displayname').val());
} catch (e) {
alert(e.message);
}
});
$('#sendmessage').on('click', function () {
//Send Messages to specific User
proxy.invoke("Sendchat", $('#message').val(), $('#name').val());
});
//Receive Messages from User
proxy.on("GotMessages", function (Message) {
alert(Message);
$('#discussion').append('<div>' + Message + '</br></div>');
});
});
</script>
}